Skip to content

Commit

Permalink
Merge pull request #5 from cloudblue/feature/LITE-14852
Browse files Browse the repository at this point in the history
LITE-14852 LITE-14785 Dates are now supported with DateTime fields and search is optimized (fixed for MySQL)
  • Loading branch information
d3rky authored Jul 31, 2020
2 parents e45c2e0 + cf46b5b commit fe10fd1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
13 changes: 12 additions & 1 deletion dj_rql/filter_cls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

from collections import defaultdict
from datetime import datetime
from uuid import uuid4

from django.db.models import Q
Expand Down Expand Up @@ -354,6 +355,9 @@ def _build_q_for_search(self, operator, str_value):
})

unquoted_value = self.remove_quotes(str_value)
if not unquoted_value:
return Q()

if not unquoted_value.startswith(RQL_ANY_SYMBOL):
unquoted_value = '*' + unquoted_value

Expand Down Expand Up @@ -802,10 +806,17 @@ def _convert_value(cls, django_field, str_value, use_repr=False):
dt = parse_date(val)
if dt is None:
raise ValueError
return dt

elif filter_type == FilterTypes.DATETIME:
dt = parse_datetime(val)
if dt is None:
raise ValueError
dt = parse_date(val)
if dt is None:
raise ValueError

return datetime(year=dt.year, month=dt.month, day=dt.day)
return dt

elif filter_type == FilterTypes.BOOLEAN:
if val not in (RQL_FALSE, RQL_TRUE):
Expand Down
1 change: 1 addition & 0 deletions tests/test_filter_cls/test_apply_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def test_search():
assert apply_filters('(search="*aN*";search="*book")') == books
assert apply_filters('(search="*aN";search="*b*")') == books
assert apply_filters('search=bo') == [books[0]]
assert apply_filters('search=""') == books


def test_search_bad_lookup():
Expand Down
3 changes: 2 additions & 1 deletion tests/test_filter_cls/test_fields_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ def test_published_at():
assert filter_field(filter_name, CO.NE, '2019-02-12T10:02') == [books[1]]
assert filter_field(filter_name, CO.LE, '2020-01-01T00:00+08:00') == books
assert filter_field(filter_name, CO.GT, '2000-12-12T00:21:00') == books
assert filter_field(filter_name, CO.GT, '2000-12-12') == books


@pytest.mark.django_db
Expand Down Expand Up @@ -396,7 +397,7 @@ def test_date_field_fail(filter_name, bad_value):


@pytest.mark.parametrize('bad_value', [
'2019-02-12', '0', 'date', '2019-02-12T27:00:00', '2019-02-12T21:00:00K',
'0', 'date', '2019-02-12T27:00:00', '2019-02-12T21:00:00K',
])
@pytest.mark.parametrize('filter_name', ['published.at'])
def test_datetime_field_fail(filter_name, bad_value):
Expand Down

0 comments on commit fe10fd1

Please sign in to comment.