Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to use date comparison operators #27

Merged
merged 2 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/smartquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ class Comment(BaseModel):
# whole date
log(Comment.where(created_at__year=2014, created_at__month=1,
created_at__day=1).all()) # cm11
# date comparisons
log(Comment.where(created_at__year_gt=2014).all()) # cm12, cm21, cm22

##### 1.4 where() with auto-joined relations #####

Expand Down
19 changes: 18 additions & 1 deletion sqlalchemy_mixins/smartquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,25 @@ class SmartQueryMixin(InspectionMixin, EagerLoadMixin):
'iendswith': lambda c, v: c.ilike('%' + v),

'year': lambda c, v: extract('year', c) == v,
'year_ne': lambda c, v: extract('year', c) != v,
'year_gt': lambda c, v: extract('year', c) > v,
'year_ge': lambda c, v: extract('year', c) >= v,
'year_lt': lambda c, v: extract('year', c) < v,
'year_le': lambda c, v: extract('year', c) <= v,

'month': lambda c, v: extract('month', c) == v,
'day': lambda c, v: extract('day', c) == v
'month_ne': lambda c, v: extract('month', c) != v,
'month_gt': lambda c, v: extract('month', c) > v,
'month_ge': lambda c, v: extract('month', c) >= v,
'month_lt': lambda c, v: extract('month', c) < v,
'month_le': lambda c, v: extract('month', c) <= v,

'day': lambda c, v: extract('day', c) == v,
'day_ne': lambda c, v: extract('day', c) != v,
'day_gt': lambda c, v: extract('day', c) > v,
'day_ge': lambda c, v: extract('day', c) >= v,
'day_lt': lambda c, v: extract('day', c) < v,
'day_le': lambda c, v: extract('day', c) <= v,
}

@classproperty
Expand Down
7 changes: 7 additions & 0 deletions sqlalchemy_mixins/tests/test_smartquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,13 @@ def test(filters, expected_result):
created_at__day=1), {cm11})
test(dict(created_at=datetime.datetime(2014, 1, 1)), {cm11})

# date comparisons
test(dict(created_at__year_ge=2014), {cm11, cm12, cm21, cm22})
test(dict(created_at__year_gt=2014), {cm12, cm21, cm22})
test(dict(created_at__year_le=2015), {cm11, cm12, cm21})
test(dict(created_at__month_lt=10), {cm11})



# noinspection PyUnusedLocal
class TestOrderExpr(BaseTest):
Expand Down