qbe
authorOlivier Larchevêque <olivier.larcheveque@auf.org>
Tue, 29 May 2012 20:35:47 +0000 (16:35 -0400)
committerOlivier Larchevêque <olivier.larcheveque@auf.org>
Tue, 29 May 2012 20:35:47 +0000 (16:35 -0400)
src/qbe/CHANGES.txt
src/qbe/django_qbe/forms.py

index 57bdbae..073d799 100644 (file)
@@ -1,3 +1,8 @@
+0.1.X
+-----
+- Fix joining with models which have db_table set in _meta
+- Fix get labels translated
+
 0.1.6 (2011-04-20)
 ------------------
  - Added compatibility with Django 1.3 static files.
index f114446..620696b 100644 (file)
@@ -8,6 +8,7 @@ from django.conf import settings
 from django.forms.formsets import BaseFormSet, formset_factory
 from django.utils.importlib import import_module
 from django.utils.translation import ugettext as _
+from django.contrib.contenttypes.models import ContentType
 
 from django_qbe.utils import get_models
 from django_qbe.widgets import CriteriaInput
@@ -129,6 +130,19 @@ class BaseQueryByExampleFormSet(BaseFormSet):
         self._sorts = sorts
         self._params = params
 
+    def translate_model_to_db_table(self, model_name):
+        """
+        Ensure the full model name match the DB table name (not app_label
+        name).
+        """
+        app_label, name = model_name.split("_")
+        try:
+            ct = ContentType.objects.get(app_label=app_label, model=name)
+            model = ct.model_class()
+            return model._meta.db_table
+        except:
+            return model_name
+
     def get_query_parts(self):
         """
         Return SQL query for cleaned data
@@ -172,8 +186,9 @@ class BaseQueryByExampleFormSet(BaseFormSet):
             if all(criteria):
                 if is_join:
                     over_split = over.lower().rsplit(".", 1)
-                    join_model = qn(over_split[0].replace(".", "_"))
+                    join_model = qn(self.translate_model_to_db_table(over_split[0].replace(".", "_")))
                     join_field = qn(over_split[1])
+                    
                     if model in self._models:
                         _field = self._models[model]._meta.get_field(field)
                         join = u"%s.%s = %s.%s" \
@@ -316,6 +331,16 @@ class BaseQueryByExampleFormSet(BaseFormSet):
         else:
             return len(self.get_results())
 
+
+    def get_model(self, db_prefix, model):
+        klass = get_model(db_prefix, model)
+        if klass is None:
+            db_model = "%s_%s" % (db_prefix, model)
+            for table in self._models.keys():
+                if table == db_model:
+                    return self._models[table]
+        return klass
+
     def get_labels(self, add_extra_ids=False, row_number=False):
         if row_number:
             labels = [_(u"#")]
@@ -328,8 +353,9 @@ class BaseQueryByExampleFormSet(BaseFormSet):
         if selects and isinstance(selects, (tuple, list)):
             for select in selects:
                 label_splits = select.replace("`", "").replace("_", ".").split(".")
-                label_field = " ".join(label_splits[2:])
-                model = get_model(label_splits[0], label_splits[1])
+                # restore underscore for fields which use it
+                label_field = "_".join(label_splits[2:])
+                model = self.get_model(label_splits[0], label_splits[1])
                 label = model._meta.get_field_by_name(label_field)[0].verbose_name
                 labels.append(label.capitalize())
         return labels