Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/release/2.7' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
petrjasek committed Jul 16, 2024
2 parents 4a8dbf9 + cc220b8 commit 70ccfe2
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 89 deletions.
50 changes: 50 additions & 0 deletions server/features/search_events.feature
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,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"}
]}
"""
19 changes: 14 additions & 5 deletions server/planning/events/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,23 @@ def get_coverage_id(coverage: EmbeddedCoverageItem) -> str:
]


def get_subject_str(subject: Dict[str, str]) -> str:
return ":".join(
[
subject.get("name", ""),
subject.get("qcode", ""),
subject.get("scheme", ""),
str(subject.get("translations", "")),
]
)


def is_event_updated(new_item: Event, old_item: Event) -> bool:
if new_item.get("name") != old_item.get("name"):
return True
new_subject = set([subject.get("qcode") for subject in new_item.get("subject", [])])
old_subject = set([subject.get("qcode") for subject in old_item.get("subject", [])])
if new_subject != old_subject:
return True
return False
new_subject = set([get_subject_str(subject) for subject in new_item.get("subject", [])])
old_subject = set([get_subject_str(subject) for subject in old_item.get("subject", [])])
return new_subject != old_subject


class EventsService(superdesk.Service):
Expand Down
74 changes: 57 additions & 17 deletions server/planning/search/queries/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,26 +207,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
25 changes: 25 additions & 0 deletions server/planning/tests/events_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,36 @@ def test_is_new_version():

assert not service.is_new_version(new_event, old_event)

new_event["subject"] = [{"qcode": "foo"}, {"qcode": "bar"}]
old_event["subject"] = [{"qcode": "bar"}, {"qcode": "foo"}]

assert not service.is_new_version(new_event, old_event)

new_event["subject"] = [{"qcode": "foo"}]
old_event["subject"] = [{"qcode": "bar"}]

assert service.is_new_version(new_event, old_event)

new_event["subject"] = [{"qcode": "foo", "name": "Foo"}]
old_event["subject"] = [{"qcode": "foo", "name": "foo"}]

assert service.is_new_version(new_event, old_event)

new_event["subject"] = [{}]
old_event["subject"] = [{"qcode": "foo", "name": "foo"}]

assert service.is_new_version(new_event, old_event)

new_event["subject"] = [{"qcode": "foo", "name": "foo", "translations": {"fr-CA": "Foo"}}]
old_event["subject"] = [{"qcode": "foo", "name": "foo", "translations": None}]

assert service.is_new_version(new_event, old_event)

new_event["subject"] = [{"qcode": "foo", "name": "foo", "translations": {"fr-CA": "Bar"}}]
old_event["subject"] = [{"qcode": "foo", "name": "foo", "translations": {"fr-CA": "Foo"}}]

assert service.is_new_version(new_event, old_event)


def test_should_update():
service = EventsService()
Expand Down

0 comments on commit 70ccfe2

Please sign in to comment.