Skip to content

Commit

Permalink
fix date filtering
Browse files Browse the repository at this point in the history
always return ongoing events for selected date
check properly all_day / no_end_time flags
  • Loading branch information
petrjasek committed Jul 12, 2024
1 parent 4d19a6f commit 9ddb85d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 73 deletions.
21 changes: 15 additions & 6 deletions server/planning/search/queries/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,24 +207,33 @@ def field_range(query: ElasticRangeParams):
# so we first convert it to local timezone
# and then we take only date part of it
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")
ignore_time = "dates.all_day" if query.field == "dates.start" else "dates.no_end_time"
return {
"bool": {
"should": [
{"range": {query.field: params}},
{
"bool": {
"must_not": [
{"term": {ignore_time: True}},
],
"must": [
{"range": {query.field: params}},
],
},
},
{
"bool": {
"must": [
{"term": {"dates.all_day": True}},
{"term": {ignore_time: True}},
{"range": {query.field: local_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 9ddb85d

Please sign in to comment.