Skip to content

Commit

Permalink
fix date filtering (superdesk#2024)
Browse files Browse the repository at this point in the history
* fix date filtering

- always return ongoing events for selected date
- check properly all_day / no_end_time flags

SDESK-7334

* fix query for all day events

there is no end time for all day events as well

* add test
  • Loading branch information
petrjasek authored Jul 16, 2024
1 parent 4d19a6f commit cc220b8
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 84 deletions.
50 changes: 50 additions & 0 deletions server/features/search_events.feature
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,53 @@ Feature: Event Search
]}
"""

@auth
Scenario: Filter by date using America/Toronto timezone
Given "events"
"""
[{
"guid": "all_day_multi",
"name": "all day event multiday",
"dates": {"start": "2024-07-14T00:00:00+0000", "end": "2024-07-16T00:00:00+0000", "all_day": true}
}, {
"guid": "all_day_single",
"name": "all day single day",
"dates": {"start": "2024-07-15T00:00:00+0000", "end": "2024-07-15T00:00:00+0000", "all_day": true}
}, {
"guid": "no_end_time_multi",
"name": "no end time multiday",
"dates": {"start": "2024-07-13T10:00:00+0000", "end": "2024-07-15T00:00:00+0000", "no_end_time": true}
}, {
"guid": "no_end_time_single",
"name": "no end time single day",
"dates": {"start": "2024-07-15T10:00:00+0000", "end": "2024-07-15T10:00:00+0000", "no_end_time": true}
}, {
"guid": "matching",
"name": "regular",
"dates": {"start": "2024-07-15T10:00:00+0000", "end": "2024-07-16T00:00:00+0000"}
},
{
"guid": "not matching",
"name": "not matching",
"dates": {"start": "2024-07-01T10:00:00+0000", "end": "2024-07-02T00:00:00+0000"}
}
]
"""
When we get "/events_planning_search?repo=events&only_future=false&time_zone=America/Toronto&start_date=2024-07-15T04:00:00"
Then we get list with 5 items
"""
{"_items": [
{"guid": "all_day_multi"},
{"guid": "all_day_single"},
{"guid": "no_end_time_multi"},
{"guid": "no_end_time_single"},
{"guid": "matching"}
]}
"""
When we get "/events_planning_search?repo=events&only_future=false&time_zone=America/Toronto&start_date=2024-07-16T04:00:00"
Then we get list with 1 items
"""
{"_items": [
{"guid": "all_day_multi"}
]}
"""
74 changes: 57 additions & 17 deletions server/planning/search/queries/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,26 +209,66 @@ def field_range(query: ElasticRangeParams):
local_params = params.copy()
local_params.pop("time_zone", None)
for key in ("gt", "gte", "lt", "lte"):
if local_params.get(key) and "T" in local_params[key] and query.time_zone:
tz = pytz.timezone(query.time_zone)
if local_params.get(key) and "T" in local_params[key] and params.get("time_zone"):
tz = pytz.timezone(params["time_zone"])
utc_value = datetime.fromisoformat(local_params[key].replace("+0000", "+00:00"))
local_value = utc_value.astimezone(tz)
local_params[key] = local_value.strftime("%Y-%m-%d")
return {
"bool": {
"should": [
{"range": {query.field: params}},
{
"bool": {
"must": [
{"term": {"dates.all_day": True}},
{"range": {query.field: local_params}},
],
}
},
],
},
}
if query.field == "dates.start":
return {
"bool": {
"should": [
{
"bool": {
"must_not": [
{"term": {"dates.all_day": True}},
],
"must": [
{"range": {query.field: params}},
],
},
},
{
"bool": {
"must": [
{"term": {"dates.all_day": True}},
{"range": {query.field: local_params}},
],
},
},
],
},
}
else:
return {
"bool": {
"should": [
{
"bool": {
"must_not": [
{"term": {"dates.all_day": True}},
{"term": {"dates.no_end_time": True}},
],
"must": [
{"range": {query.field: params}},
],
},
},
{
"bool": {
"should": [
{"term": {"dates.all_day": True}},
{"term": {"dates.no_end_time": True}},
],
"must": [
{"range": {query.field: local_params}},
],
"minimum_should_match": 1,
},
},
],
},
}

return {"range": {query.field: params}}

Expand Down
78 changes: 11 additions & 67 deletions server/planning/search/queries/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,16 +255,7 @@ def search_date_start(params: Dict[str, Any], query: elastic.ElasticQuery):

if not date_filter and start_date and not end_date:
query.filter.append(
elastic.bool_or(
[
elastic.date_range(
elastic.ElasticRangeParams(field="dates.start", gte=start_date, time_zone=time_zone)
),
elastic.date_range(
elastic.ElasticRangeParams(field="dates.end", gte=start_date, time_zone=time_zone)
),
]
)
elastic.date_range(elastic.ElasticRangeParams(field="dates.end", gte=start_date, time_zone=time_zone)),
)


Expand All @@ -273,16 +264,7 @@ def search_date_end(params: Dict[str, Any], query: elastic.ElasticQuery):

if not date_filter and not start_date and end_date:
query.filter.append(
elastic.bool_or(
[
elastic.date_range(
elastic.ElasticRangeParams(field="dates.start", lte=end_date, time_zone=time_zone)
),
elastic.date_range(
elastic.ElasticRangeParams(field="dates.end", lte=end_date, time_zone=time_zone)
),
]
)
elastic.date_range(elastic.ElasticRangeParams(field="dates.start", lte=end_date, time_zone=time_zone)),
)


Expand All @@ -291,55 +273,17 @@ def search_date_range(params: Dict[str, Any], query: elastic.ElasticQuery):

if not date_filter and start_date and end_date:
query.filter.append(
elastic.bool_or(
elastic.bool_and(
[
elastic.bool_and(
[
elastic.date_range(
elastic.ElasticRangeParams(
field="dates.start",
gte=start_date,
time_zone=time_zone,
)
),
elastic.date_range(
elastic.ElasticRangeParams(field="dates.end", lte=end_date, time_zone=time_zone)
),
]
),
elastic.bool_and(
[
elastic.date_range(
elastic.ElasticRangeParams(
field="dates.start",
lt=start_date,
time_zone=time_zone,
)
),
elastic.date_range(
elastic.ElasticRangeParams(field="dates.end", gt=end_date, time_zone=time_zone)
),
]
elastic.date_range(
elastic.ElasticRangeParams(
field="dates.start",
gte=end_date,
time_zone=time_zone,
),
),
elastic.bool_or(
[
elastic.date_range(
elastic.ElasticRangeParams(
field="dates.start",
gte=start_date,
lte=end_date,
time_zone=time_zone,
)
),
elastic.date_range(
elastic.ElasticRangeParams(
field="dates.end",
gte=start_date,
lte=end_date,
time_zone=time_zone,
)
),
]
elastic.date_range(
elastic.ElasticRangeParams(field="dates.end", lte=start_date, time_zone=time_zone)
),
]
)
Expand Down

0 comments on commit cc220b8

Please sign in to comment.