Skip to content

Commit

Permalink
Merge pull request #1299 from UUDigitalHumanitieslab/bugfix/date-filt…
Browse files Browse the repository at this point in the history
…er-from-query

Bugfix/date filter from query
  • Loading branch information
lukavdplas authored Oct 31, 2023
2 parents eceb9c2 + 80e9f50 commit 60211ce
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
12 changes: 10 additions & 2 deletions backend/visualization/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ def get_filters(query):

def is_date_filter(filter):
"""Checks if a filter object is a date filter"""
return has_path(filter, 'range', 'date')
range_filters = filter.get('range', dict()).keys()
return any(
has_path(filter, 'range', filter_name, 'format')
for filter_name in range_filters
)


def parse_date(datestring):
Expand All @@ -145,7 +149,11 @@ def get_date_range(query: Dict):
datefilters = list(filter(is_date_filter, filters))

if len(datefilters):
parameters = [f['range']['date'] for f in datefilters]
parameters = [
data
for f in datefilters
for data in f['range'].values()
]
min_dates = [parse_date(p['gte'])
for p in parameters if 'gte' in p]
max_dates = [parse_date(p['lte'])
Expand Down
37 changes: 37 additions & 0 deletions backend/visualization/tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,40 @@ def test_manipulation_is_pure(basic_query):
assert query.get_date_range(basic_query) == (None, None)
assert query.get_date_range(narrow_query) == narrow_timeframe
assert query.get_date_range(wide_query) == wide_timeframe

def test_is_date_filter():
assert query.is_date_filter({
'range': {
'date': {
'gte': '1950-01-01',
'lte': '1959-12-31',
'format': 'yyyy-MM-dd'
}
}
})

assert query.is_date_filter({
'range': {
'publication_date': {
'gte': '1950-01-01',
'lte': '1959-12-31',
'format': 'yyyy-MM-dd'
}
}
})

assert not query.is_date_filter({
'range': {
'year': {
'gte': 1950,
'lte': 1959,
}
}
})

def test_get_date_range():
min_date = datetime(1800, 1, 1)
max_date = datetime(1900, 1, 1)
date_filter = query.make_date_filter(min_date, max_date, 'publication_date')
q = query.add_filter(query.MATCH_ALL, date_filter)
assert query.get_date_range(q) == (min_date, max_date)

0 comments on commit 60211ce

Please sign in to comment.