Merge branch 'master' into qbe
authorOlivier Larchevêque <olivier.larcheveque@auf.org>
Thu, 26 Jul 2012 20:29:23 +0000 (16:29 -0400)
committerOlivier Larchevêque <olivier.larcheveque@auf.org>
Thu, 26 Jul 2012 20:29:23 +0000 (16:29 -0400)
src/qbe/django_qbe/forms.py
src/qbe/django_qbe/static/django_qbe/css/qbe.css
src/qbe/django_qbe/static/django_qbe/js/qbe.core.js
src/qbe/django_qbe/templates/qbe.html

index 620696b..c8f45e9 100644 (file)
@@ -29,7 +29,15 @@ except AttributeError:
 SORT_CHOICES = (
     ("", ""),
     ("asc", _("Ascending")),
-    ("des", _("Descending")),
+    ("desc", _("Descending")),
+)
+
+STATUS_CHOICES = (
+    ("", "Tous"),
+    ("inactive", "Inactifs"),
+    ("active", "Actifs"),
+    ("futur", "Futurs"),
+    ("unknown", "Inconnus"),
 )
 
 
@@ -38,6 +46,8 @@ class QueryByExampleForm(forms.Form):
     model = forms.CharField(label=_("Model"))
     field = forms.CharField(label=_("Field"))
     criteria = forms.CharField(label=_("Criteria"), required=False)
+    status = forms.ChoiceField(label=_("Statut"), choices=STATUS_CHOICES,
+                             required=False)
     sort = forms.ChoiceField(label=_("Sort"), choices=SORT_CHOICES,
                              required=False)
 
@@ -59,6 +69,10 @@ class QueryByExampleForm(forms.Form):
             field_attr_class = "qbeFillFields enable:sort,%s" % criteria_names
         else:
             field_attr_class = "qbeFillFields enable:sort,criteria"
+        status_widget = forms.Select(attrs={'disabled': "disabled",
+                                          'class': 'hidden'},
+                                   choices=STATUS_CHOICES)
+        self.fields['status'].widget = status_widget
         field_widget = forms.Select(attrs={'class': field_attr_class})
         self.fields['field'].widget = field_widget
 
@@ -79,6 +93,7 @@ class BaseQueryByExampleFormSet(BaseFormSet):
     _selects = []
     _froms = []
     _wheres = []
+    _statuses = []
     _sorts = []
     _params = []
     _models = {}
@@ -120,7 +135,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 +144,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 +168,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,13 +193,19 @@ 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'
+            try:
+                is_join = operator.lower() == 'join'
+            except:
+                is_join = False
             if show and not is_join:
                 selects.append(db_field)
             if sort:
-                sorts.append(db_field)
+                sorts.append((db_field, sort))
+            if status:
+                statuses.append((model, status))
             if all(criteria):
                 if is_join:
                     over_split = over.lower().rsplit(".", 1)
@@ -191,9 +214,14 @@ class BaseQueryByExampleFormSet(BaseFormSet):
                     
                     if model in self._models:
                         _field = self._models[model]._meta.get_field(field)
+                        try:
+                            _db_column = qn(_field.db_column)
+                        except:
+                            _db_column = qn(_field.attname)
+
                         join = u"%s.%s = %s.%s" \
                                % (join_model, join_field, qn(model),
-                                  qn(_field.db_column))
+                                  qn(_db_column))
                     else:
                         join = u"%s.%s = %s" \
                                % (join_model, join_field,
@@ -216,18 +244,41 @@ 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(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.supprime = False AND ("
+                        "((%s.`date_debut` <= DATE(NOW()) OR %s.`date_debut` IS NULL) AND %s.`date_fin` >= DATE(NOW())) OR "
+                        "((%s.`date_fin` >= DATE(NOW()) OR %s.`date_fin` IS NULL) AND %s.`date_debut` <= DATE(NOW())) OR "
+                        "(%s.`date_debut` <= DATE(NOW()) AND %s.`date_fin` >= DATE(NOW())))"
+                        % (m, m, m, m, m, 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 7564825..ad8fb80 100644 (file)
@@ -166,3 +166,7 @@ input.qbeCheckModels {
     overflow: auto;
     margin-top: 10px;
 }
+
+.qbeTabular .hidden {
+       display: none;
+}
index 5f230bb..7ef17f5 100644 (file)
@@ -199,7 +199,8 @@ qbe.Core = function() {};
                         optFields.push(option);
                     }
                 }
-                $("#"+ domTo).html('<option value="">*</option>' + optPrimaries.join("") + optForeigns.join("") + optManies.join("") + optFields.join(""));
+                optEtat = '<option value="status">État</option>'
+                $("#"+ domTo).html('<option value="">*</option>' + optPrimaries.join("") + optForeigns.join("") + optManies.join("") + optFields.join("") + optEtat);
                 // We need to raise change event
                 $("#"+ domTo).change();
             }
@@ -216,11 +217,32 @@ qbe.Core = function() {};
             css = $(this).attr("class");
             cssSplit = css.split("enable:")
             inputs = cssSplit[cssSplit.length-1].split(",");
+            if (field == 'status') {
+                $("#"+ prefix + "-sort").attr("disabled", "disabled");
+                $("#"+ prefix + "-sort").val("");
+                $("#"+ prefix + "-criteria").attr("disabled", "disabled");
+                $("#"+ prefix + "-criteria").val("");
+                $("#"+ prefix + "-criteria").addClass("hidden");
+                $("#"+ prefix + "-criteria_0").attr("disabled", "disabled");
+                $("#"+ prefix + "-criteria_0").val("");
+                $("#"+ prefix + "-criteria_0").addClass("hidden");
+                $("#"+ prefix + "-criteria_1").attr("disabled", "disabled");
+                $("#"+ prefix + "-criteria_1").val("");
+                $("#"+ prefix + "-criteria_1").addClass("hidden");
+                $("#"+ prefix + "-status").removeClass("hidden");
+                $("#"+ prefix + "-status").removeAttr("disabled");
+
+            }
+            else {
+                $("#"+ prefix + "-status").val("");
+                $("#"+ prefix + "-status").addClass("hidden");
+
             for(var i=0; i<inputs.length; i++) {
                 input = inputs[i];
                 domTo = prefix +"-"+ input;
                 if (field) {
                     $("#"+ domTo).removeAttr("disabled");
+                    $("#"+ domTo).removeClass("hidden");
                 } else {
                     $("#"+ domTo).attr("disabled", "disabled");
                     $("#"+ domTo).val("");
@@ -262,6 +284,7 @@ qbe.Core = function() {};
                     }
                 }
             }
+            }
         };
 
         /**
index 99a54be..bb4763c 100644 (file)
@@ -60,7 +60,7 @@
                 <td>{{ form.model.errors }}{{ form.model }}</td>
                 <td>{{ form.field.errors }}{{ form.field }}</td>
                 <td>{{ form.sort.errors }}{{ form.sort }}</td>
-                <td>{{ form.criteria.errors }}{{ form.criteria }}</td>
+                <td>{{ form.criteria.errors }}{{ form.criteria }}{{ form.status }}</td>
                 <td class="delete"></td>
             </tr>
         {% endfor %}