Code

Ticket #7672: dayofweek_filter-r7866.v2.diff

File dayofweek_filter-r7866.v2.diff, 6.8 KB (added by rossp, 6 years ago)

New revision, see note below.

Line 
1Index: django/db/models/sql/where.py
2===================================================================
3--- django/db/models/sql/where.py       (revision 7866)
4+++ django/db/models/sql/where.py       (working copy)
5@@ -152,7 +152,7 @@
6                     params)
7         elif lookup_type in ('range', 'year'):
8             return ('%s BETWEEN %%s and %%s' % field_sql, params)
9-        elif lookup_type in ('month', 'day'):
10+        elif lookup_type in ('month', 'day', 'week_day'):
11             return ('%s = %%s' % connection.ops.date_extract_sql(lookup_type,
12                     field_sql), params)
13         elif lookup_type == 'isnull':
14Index: django/db/models/sql/constants.py
15===================================================================
16--- django/db/models/sql/constants.py   (revision 7866)
17+++ django/db/models/sql/constants.py   (working copy)
18@@ -4,7 +4,7 @@
19 QUERY_TERMS = dict([(x, None) for x in (
20     'exact', 'iexact', 'contains', 'icontains', 'gt', 'gte', 'lt', 'lte', 'in',
21     'startswith', 'istartswith', 'endswith', 'iendswith', 'range', 'year',
22-    'month', 'day', 'isnull', 'search', 'regex', 'iregex',
23+    'month', 'day', 'week_day', 'isnull', 'search', 'regex', 'iregex',
24     )])
25
26 # Size of each "chunk" for get_iterator calls.
27Index: django/db/models/fields/__init__.py
28===================================================================
29--- django/db/models/fields/__init__.py (revision 7866)
30+++ django/db/models/fields/__init__.py (working copy)
31@@ -233,7 +233,7 @@
32         if hasattr(value, 'as_sql'):
33             sql, params = value.as_sql()
34             return QueryWrapper(('(%s)' % sql), params)
35-        if lookup_type in ('exact', 'regex', 'iregex', 'gt', 'gte', 'lt', 'lte', 'month', 'day', 'sea                                   rch'):
36+        if lookup_type in ('exact', 'regex', 'iregex', 'gt', 'gte', 'lt', 'lte', 'month', 'day', 'wee                                   k_day', 'search'):
37             return [value]
38         elif lookup_type in ('range', 'in'):
39             return value
40Index: django/db/backends/postgresql/operations.py
41===================================================================
42--- django/db/backends/postgresql/operations.py (revision 7866)
43+++ django/db/backends/postgresql/operations.py (working copy)
44@@ -26,7 +26,10 @@
45
46     def date_extract_sql(self, lookup_type, field_name):
47         # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRAC                                   T
48-        return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name)
49+               if lookup_type == 'week_day':
50+                       return "EXTRACT('dow' FROM %s)" % field_name
51+               else:
52+                       return "EXTRACT('%s' FROM %s)" % (lookup_type, field_name)
53
54     def date_trunc_sql(self, lookup_type, field_name):
55         # http://www.postgresql.org/docs/8.0/static/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC
56Index: django/db/backends/mysql/base.py
57===================================================================
58--- django/db/backends/mysql/base.py    (revision 7866)
59+++ django/db/backends/mysql/base.py    (working copy)
60@@ -68,7 +68,10 @@
61 class DatabaseOperations(BaseDatabaseOperations):
62     def date_extract_sql(self, lookup_type, field_name):
63         # http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html
64-        return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name)
65+        if lookup_type == 'week_day':
66+            return "DAYOFWEEK(%s)" % field_name
67+        else:
68+            return "EXTRACT(%s FROM %s)" % (lookup_type.upper(), field_name)
69
70     def date_trunc_sql(self, lookup_type, field_name):
71         fields = ['year', 'month', 'day', 'hour', 'minute', 'second']
72Index: django/db/backends/oracle/base.py
73===================================================================
74--- django/db/backends/oracle/base.py   (revision 7866)
75+++ django/db/backends/oracle/base.py   (working copy)
76@@ -56,7 +56,10 @@
77
78     def date_extract_sql(self, lookup_type, field_name):
79         # http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions42a.htm#101716                                   3
80-        return "EXTRACT(%s FROM %s)" % (lookup_type, field_name)
81+        if lookup_type == 'week_day':
82+            return "TO_CHAR(%s, 'D')" % field_name
83+        else:
84+            return "EXTRACT(%s FROM %s)" % (lookup_type, field_name)
85
86     def date_trunc_sql(self, lookup_type, field_name):
87         # Oracle uses TRUNC() for both dates and numbers.
88Index: tests/modeltests/basic/models.py
89===================================================================
90--- tests/modeltests/basic/models.py    (revision 7866)
91+++ tests/modeltests/basic/models.py    (working copy)
92@@ -68,6 +68,8 @@
93 <Article: Area woman programs in Python>
94 >>> Article.objects.get(pub_date__year=2005, pub_date__month=7, pub_date__day=28)
95 <Article: Area woman programs in Python>
96+>>> Article.objects.get(pub_date__week_day=5)
97+<Article: Area woman programs in Python>
98
99 # The "__exact" lookup type can be omitted, as a shortcut.
100 >>> Article.objects.get(id=1)
101@@ -82,6 +84,11 @@
102 >>> Article.objects.filter(pub_date__year=2005, pub_date__month=7)
103 [<Article: Area woman programs in Python>]
104
105+>>> Article.objects.filter(pub_date__week_day=5)
106+[<Article: Area woman programs in Python>]
107+>>> Article.objects.filter(pub_date__week_day=6)
108+[]
109+
110 # Django raises an Article.DoesNotExist exception for get() if the parameters
111 # don't match any object.
112 >>> Article.objects.get(id__exact=2)
113@@ -94,6 +101,11 @@
114     ...
115 DoesNotExist: Article matching query does not exist.
116
117+>>> Article.objects.get(pub_date__week_day=6)
118+Traceback (most recent call last):
119+    ...
120+DoesNotExist: Article matching query does not exist.
121+
122 # Lookup by a primary key is the most common case, so Django provides a
123 # shortcut for primary-key exact lookups.
124 # The following is identical to articles.get(id=1).
125Index: docs/db-api.txt
126===================================================================
127--- docs/db-api.txt     (revision 7866)
128+++ docs/db-api.txt     (working copy)
129@@ -1534,6 +1534,25 @@
130 Note this will match any record with a pub_date on the third day of the month,
131 such as January 3, July 3, etc.
132
133+week_day
134+~~~~~~~~
135+
136+For date/datetime fields, a 'day of the week' match.
137+
138+Example::
139+
140+    Entry.objects.filter(pub_date__week_day=2)
141+
142+SQL equivalent::
143+
144+    SELECT ... WHERE EXTRACT('dow' FROM pub_date) = '2';
145+
146+(The exact SQL syntax varies for each database engine.)
147+
148+Note this will match any record with a pub_date that falls on a Tuesday (day 2
149+of the week), regardless of the month or year in which it occurs. Week days
150+are zero-indexed, with day 0 being Sunday and day 6 being Saturday.
151+
152 isnull
153 ~~~~~~
154