adamv avatar adamv committed b7aff71

Fixes for issue 34, __year filters not working correctly.

Needed to override the proper DatabaseOperations method.

Comments (0)

Files changed (2)

source/sqlserver_ado/operations.py

     	quoted_field_name = self.quote_name(field_name)
 
         if lookup_type == 'year':
+            print "YEAR CHECKED"
             return "Convert(datetime, Convert(varchar, DATEPART(year, %s)) + '/01/01')" % quoted_field_name
         if lookup_type == 'month':
             return "Convert(datetime, Convert(varchar, DATEPART(year, %s)) + '/' + Convert(varchar, DATEPART(month, %s)) + '/01')" %\
         if value is None or value == '':
             return None
         return value # Should be a decimal type (or string)
+
+    def year_lookup_bounds(self, value):
+        """
+        Returns a two-elements list with the lower and upper bound to be used
+        with a BETWEEN operator to query a field value using a year lookup
+
+        `value` is an int, containing the looked-up year.
+        """
+        first = '%s-01-01 00:00:00'
+        second = '%s-12-31 23:59:59'
+        return [first % value, second % value]

tests/test_main/regressiontests/models.py

         RelatedA(a="valuea", b=b).save()
 
         items = RelatedA.objects.select_related()[1:2]
-        self.assertEqual(len(items), 1)
+        self.assertEqual(len(items), 1)
+        
+class Bug34DatetimeTable(models.Model):
+    """Ensure that __year filters work with datetime fields.
+    
+    >>> Bug34DatetimeTable(posted=datetime.date(2007,1,1)).save()
+    >>> Bug34DatetimeTable(posted=datetime.date(2008,2,2)).save()
+    >>> Bug34DatetimeTable(posted=datetime.date(2009,3,3)).save()
+    >>> len((Bug34DatetimeTable.objects.filter(posted__day=3)))
+    1
+    >>> len(list(Bug34DatetimeTable.objects.filter(posted__month=2)))
+    1
+    >>> len(list(Bug34DatetimeTable.objects.filter(posted__year=2008)))
+    1
+    >>> len(list(Bug34DatetimeTable.objects.filter(posted__year=2005)))
+    0
+    """
+    posted = models.DateTimeField()
+        
+class Bug34DateTable(models.Model):
+    """Ensure that __year filters work with date fields.
+    
+    >>> Bug34DateTable(posted=datetime.date(2007,1,1)).save()
+    >>> Bug34DateTable(posted=datetime.date(2008,2,2)).save()
+    >>> Bug34DateTable(posted=datetime.date(2009,3,3)).save()
+    >>> len((Bug34DateTable.objects.filter(posted__day=3)))
+    1
+    >>> len(list(Bug34DateTable.objects.filter(posted__month=2)))
+    1
+    >>> len(list(Bug34DateTable.objects.filter(posted__year=2008)))
+    1
+    >>> len(list(Bug34DateTable.objects.filter(posted__year=2005)))
+    0
+    """
+    posted = models.DateField()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.