Commits

Anonymous committed 22ee321

add compatibility with django 1.6

  • Participants
  • Parent commits 4a48ef8

Comments (0)

Files changed (4)

 syntax: glob
-.tox
+.tox
+.idea
+*.pyc

File django_easyfilters/filters.py

 from django.utils import formats
 from django.utils.dates import MONTHS
 from django.utils.text import capfirst
+from django.db.models.fields import DateField
 import six
 
 from django_easyfilters.queries import date_aggregation, value_counts, numeric_range_counts
             # API for `QuerySet.dates` used to return a list of `datetime` objects,
             # but from Django 1.6 onwards, it returns a list of `date` objects instead.
             # See: https://docs.djangoproject.com/en/1.6/releases/1.6/#queryset-dates-returns-date-objects
-            date_qs = (qs.datetimes(self.field, range_type.label) if hasattr(qs, 'datetimes') else
-                       qs.dates(self.field, range_type.label))
+            # for django under 1.6 id doesnt matter if field is DateField or DateTime field
+            # we need check it for django 1.6
+            if isinstance(self.field_obj, DateField):
+                date_qs = qs.dates(self.field, range_type.label)
+            else:
+                date_qs = (qs.datetimes(self.field, range_type.label) if hasattr(qs, 'datetimes') else
+                           qs.dates(self.field, range_type.label))
             results = date_aggregation(date_qs)
 
             date_choice_counts = self.collapse_results(results, range_type)

File django_easyfilters/queries.py

 from django.db.models.sql.subqueries import AggregateQuery
 from django.utils.datastructures import SortedDict
 
+#try to import SelectInfo, appeared in django 1.6
+try:
+    from django.db.models.sql.constants import SelectInfo
+except ImportError:
+    SelectInfo = None
+
 
 # Some fairly brittle, low level stuff, to get the aggregation
 # queries we need.
 
 class DateWithAlias(Date):
     alias = 'easyfilter_date_alias'
+
     def as_sql(self, qn, connection):
-        return super(DateWithAlias, self).as_sql(qn, connection) + ' as ' + self.alias
+        date = super(DateWithAlias, self).as_sql(qn, connection)
+        #since django 1.6 returns tuple not string, so we need to use check
+        if isinstance(date, (list, tuple)):
+            return date[0] + ' as ' + self.alias, date[1]
+        return date + ' as ' + self.alias
 
 
 def date_aggregation(date_qs):
 
     # Replace 'select' to add an alias
     date_obj = date_q.select[0]
-    date_q.select = [DateWithAlias(date_obj.col, date_obj.lookup_type)]
 
+    #if it is django 1.6 generates new Select INfo object else the sane as at previous version
+    if SelectInfo and isinstance(date_obj, SelectInfo):
+        date_q.select = [SelectInfo(col=DateWithAlias(date_obj.col.col, date_obj.col.lookup_type), field=None)]
+    else:
+        date_q.select = [DateWithAlias(date_obj.col, date_obj.lookup_type)]
+    print date_q.select
     # Now use as a subquery to do aggregation
     query = DateAggregateQuery(date_qs.model)
     query.add_subquery(date_q, date_qs.db)

File django_easyfilters/tests/urls.py

 # URLs to make it easy to add more data for the test suite.
-
-from django.conf.urls.defaults import *
+try:
+    from django.conf.urls.defaults import *
+except ImportError:
+    from django.conf.urls import *
 from django.contrib import admin
 admin.autodiscover()