Ajout champ Etat pour actif/inactif/etc...
authorPA Parent <paparent@paparent.me>
Wed, 20 Jun 2012 18:29:18 +0000 (14:29 -0400)
committerPA Parent <paparent@paparent.me>
Wed, 20 Jun 2012 18:29:18 +0000 (14:29 -0400)
src/qbe/django_qbe/forms.py
src/qbe/django_qbe/templates/qbe.html

index fb20a5e..eb71e7a 100644 (file)
@@ -32,10 +32,20 @@ SORT_CHOICES = (
     ("desc", _("Descending")),
 )
 
+STATUS_CHOICES = (
+    ("", "Tous"),
+    ("inactive", "Inactifs"),
+    ("active", "Actifs"),
+    ("futur", "Futurs"),
+    ("unknown", "Inconnus"),
+)
+
 
 class QueryByExampleForm(forms.Form):
     show = forms.BooleanField(label=_("Show"), required=False)
     model = forms.CharField(label=_("Model"))
+    status = forms.ChoiceField(label=_("Statut"), choices=STATUS_CHOICES,
+                             required=False)
     field = forms.CharField(label=_("Field"))
     criteria = forms.CharField(label=_("Criteria"), required=False)
     sort = forms.ChoiceField(label=_("Sort"), choices=SORT_CHOICES,
@@ -79,6 +89,7 @@ class BaseQueryByExampleFormSet(BaseFormSet):
     _selects = []
     _froms = []
     _wheres = []
+    _statuses = []
     _sorts = []
     _params = []
     _models = {}
@@ -120,7 +131,7 @@ class BaseQueryByExampleFormSet(BaseFormSet):
             # Don't bother validating the formset unless each form is valid on
             # its own
             return
-        selects, froms, wheres, sorts, params = self.get_query_parts()
+        selects, froms, wheres, sorts, params, statuses = self.get_query_parts()
         if not selects:
             validation_message = _(u"At least you must check a row to get.")
             raise forms.ValidationError, validation_message
@@ -129,6 +140,7 @@ class BaseQueryByExampleFormSet(BaseFormSet):
         self._wheres = wheres
         self._sorts = sorts
         self._params = params
+        self._statuses = statuses
 
     def translate_model_to_db_table(self, model_name):
         """
@@ -152,6 +164,7 @@ class BaseQueryByExampleFormSet(BaseFormSet):
         wheres = []
         sorts = []
         params = []
+        statuses = []
         app_model_labels = None
         lookup_cast = self._db_operations.lookup_cast
         qn = self._db_operations.quote_name
@@ -176,6 +189,7 @@ class BaseQueryByExampleFormSet(BaseFormSet):
             show = data["show"]
             criteria = data["criteria"]
             sort = data["sort"]
+            status = data["status"]
             db_field = u"%s.%s" % (qn(model), qn(field))
             operator, over = criteria
             is_join = operator.lower() == 'join'
@@ -183,6 +197,8 @@ class BaseQueryByExampleFormSet(BaseFormSet):
                 selects.append(db_field)
             if sort:
                 sorts.append((db_field, sort))
+            if status:
+                statuses.append((model, status))
             if all(criteria):
                 if is_join:
                     over_split = over.lower().rsplit(".", 1)
@@ -221,18 +237,37 @@ class BaseQueryByExampleFormSet(BaseFormSet):
                                      db_operator))
             if qn(model) not in froms and model in self._db_table_names:
                 froms.append(qn(model))
-        return selects, froms, wheres, sorts, params
+        return selects, froms, wheres, sorts, params, statuses
 
     def get_raw_query(self, limit=None, offset=None, count=False,
                       add_extra_ids=False, add_params=False):
+        qn = self._db_operations.quote_name
         if self._raw_query:
             return self._raw_query
         if self._sorts:
             order_by = u"ORDER BY %s" % (", ".join([" ".join(x) for x in self._sorts]))
         else:
             order_by = u""
-        if self._wheres:
-            wheres = u"WHERE %s" % (" AND ".join(self._wheres))
+        _my_wheres = self._wheres
+        if self._statuses:
+            for m, s in self._statuses:
+                # Test cas spécial: Pour état employé, vérifier via les dossiers.
+                if m == 'rh_employe':
+                    m = qn('rh_dossier')
+                    if m not in self._froms:
+                        self._froms.append(m)
+                        _my_wheres.append("`rh_employe`.`id` = `rh_dossier`.`employe`")
+                if s == "inactive":
+                    _my_wheres.append("%s.date_fin < DATE(NOW())" % m)
+                if s == "active":
+                    _my_wheres.append("(%s.date_debut IS NULL OR %s.date_debut <= DATE(NOW())) AND (%s.date_fin IS NULL OR %s.date_fin >= DATE(NOW()))" %
+                                      (m, m, m, m))
+                if s == "futur":
+                    _my_wheres.append("%s.date_debut > DATE(NOW())" % m)
+                if s == "unknown":
+                    _my_wheres.append("(%s.date_debut IS NULL AND %s.date_fin IS NULL)" % (m, m))
+        if _my_wheres:
+            wheres = u"WHERE %s" % (" AND ".join(_my_wheres))
         else:
             wheres = u""
         if count:
index 99a54be..eba78df 100644 (file)
@@ -47,6 +47,7 @@
             <tr>
                 <th>{% trans "Show" %}</th>
                 <th>{% trans "Model" %}</th>
+                <th>État</th>
                 <th>{% trans "Field" %}</th>
                 <th>{% trans "Sort" %}</th>
                 <th>{% trans "Criteria" %}</th>
@@ -58,6 +59,7 @@
             <tr class="{% cycle 'row1' 'row2' %}">
                 <td>{{ form.show.errors }}{{ form.show }}</td>
                 <td>{{ form.model.errors }}{{ form.model }}</td>
+                <td>{{ form.status.errors }}{{ form.status }}</td>
                 <td>{{ form.field.errors }}{{ form.field }}</td>
                 <td>{{ form.sort.errors }}{{ form.sort }}</td>
                 <td>{{ form.criteria.errors }}{{ form.criteria }}</td>