Repository: iommirocks/iommi (language: python)

Issue:
Auto filter for datetime should filter on date by default
Filtering on exact millisecond doesn't feel like a very good default.


Discussion / hints:

Fix patch:
diff --git a/iommi/query.py b/iommi/query.py
index 5859176c..0796cf98 100644
--- a/iommi/query.py
+++ b/iommi/query.py
@@ -489,11 +489,13 @@ def time(cls, **kwargs):
 
     @classmethod
     @with_defaults(
-        field__call_target__attribute='datetime',
-        parse=datetime_parse,
+        field__call_target__attribute='date',
+        parse=date_parse,
         extra_evaluated__is_tz_aware=lambda **_: settings.USE_TZ,
     )
     def datetime(cls, **kwargs):
+        if isinstance(kwargs.get('attr'), str):
+            kwargs['attr'] = kwargs['attr'] + '__date'
         return cls(**kwargs)
 
     @classmethod


Test patch:
diff --git a/iommi/query__tests.py b/iommi/query__tests.py
index dd95fd3f..513937c9 100644
--- a/iommi/query__tests.py
+++ b/iommi/query__tests.py
@@ -325,14 +325,6 @@ def query_str(query):
     'shortcut, input, expected_parse',
     [
         (Filter.date, '2014-03-07', date(2014, 3, 7)),
-        (
-            Filter.datetime,
-            '2014-03-07 11:13',
-            timezone.make_naive(
-                timezone.make_aware(datetime(2014, 3, 7, 11, 13)),
-                timezone=datetime_timezone.utc,
-            ),
-        ),
         (Filter.time, '11', time(11)),
         (Filter.time, '11:13', time(11, 13)),
         (Filter.time, '11:13:17', time(11, 13, 17)),
@@ -350,6 +342,28 @@ class MyQuery(Query):
     assert query_str(query.get_q()) == query_str(Q(**{'quux__bar__bazaar__iexact': expected_parse}))
 
 
+@pytest.mark.parametrize(
+    'shortcut, input, expected_parse',
+    [
+        (
+            Filter.datetime,
+            '2014-03-07',
+            date(2014, 3, 7),
+        ),
+    ],
+)
+@override_settings(USE_TZ=True)
+def test_filter_parsing_datetime(shortcut, input, expected_parse):
+    class MyQuery(Query):
+        bazaar = shortcut(attr='quux__bar__bazaar')
+
+    query = MyQuery().bind(request=req('get', bazaar=input))
+    assert 'bazaar' in query.form.fields
+    assert not query.form.get_errors(), query.form.get_errors()
+    assert query.form.fields.bazaar.iommi_path == 'bazaar'
+    assert query_str(query.get_q()) == query_str(Q(**{'quux__bar__bazaar__date__iexact': expected_parse}))
+
+
 @pytest.mark.parametrize(
     'shortcut, input, expected_parse',
     [
@@ -951,6 +965,9 @@ def test_shortcuts_map_to_form(name, shortcut):
     ]:
         kwargs['model'] = None
 
+    if name == 'datetime':
+        name = 'date'
+
     assert shortcut(**kwargs).iommi_namespace.field.call_target.attribute == name