-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update notifications data for belga specfic templates [SDBELGA-781] (#…
…1957) * Update notifications data for belga specfic templates ' * reformat code via black * fix mypy * handle none case * update utils functions based on comments * refactore datetime func and add tests * add TypedDict definition * fix mypy * address comment * udpate docstring and minor changes * minor change * remove unused imports
- Loading branch information
1 parent
11441cc
commit 9a7ce5d
Showing
3 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from planning.tests import TestCase | ||
from datetime import datetime | ||
from planning.utils import get_event_formatted_dates | ||
|
||
|
||
class TestGetEventFormattedDates(TestCase): | ||
def test_multi_day_event(self): | ||
start = datetime(2024, 5, 28, 5, 00, 00) | ||
end = datetime(2024, 5, 29, 6, 00, 00) | ||
event = {"dates": {"start": start, "end": end}} | ||
result = get_event_formatted_dates(event) | ||
self.assertEqual(result, "05:00 28/05/2024 - 06:00 29/05/2024") | ||
|
||
def test_all_day_event(self): | ||
start = datetime(2024, 4, 27, 22, 00, 00) | ||
end = datetime(2024, 4, 28, 21, 59, 59) | ||
event = {"dates": {"start": start, "end": end}} | ||
result = get_event_formatted_dates(event) | ||
self.assertEqual(result, "ALL DAY 27/04/2024") | ||
|
||
def test_same_start_end(self): | ||
start = datetime(2024, 4, 1, 14, 45) | ||
end = datetime(2024, 4, 1, 14, 45) | ||
event = {"dates": {"start": start, "end": end}} | ||
result = get_event_formatted_dates(event) | ||
self.assertEqual(result, "14:45 01/04/2024") | ||
|
||
def test_dates_same_and_different_time(self): | ||
start = datetime(2024, 5, 28, 6, 00, 00) | ||
end = datetime(2024, 5, 28, 5, 00, 00) | ||
event = {"dates": {"start": start, "end": end}} | ||
result = get_event_formatted_dates(event) | ||
self.assertEqual(result, "06:00 - 05:00, 28/05/2024") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,97 @@ | ||
from typing import Union | ||
from typing import Union, List, Dict, Any, TypedDict | ||
from bson.objectid import ObjectId | ||
from bson.errors import InvalidId | ||
from datetime import datetime | ||
from flask_babel import format_time, format_datetime, lazy_gettext | ||
from eve.utils import str_to_date | ||
import arrow | ||
from flask import current_app as app | ||
|
||
|
||
class FormattedContact(TypedDict): | ||
name: str | ||
organisation: str | ||
email: List[str] | ||
phone: List[str] | ||
mobile: List[str] | ||
website: str | ||
|
||
|
||
MULTI_DAY_SECONDS = 24 * 60 * 60 # Number of seconds for an multi-day event | ||
ALL_DAY_SECONDS = MULTI_DAY_SECONDS - 1 # Number of seconds for an all-day event | ||
|
||
|
||
def try_cast_object_id(value: str) -> Union[ObjectId, str]: | ||
try: | ||
return ObjectId(value) | ||
except InvalidId: | ||
return value | ||
|
||
|
||
def get_formatted_contacts(event: Dict[str, Any]) -> List[FormattedContact]: | ||
contacts = event.get("event_contact_info", []) | ||
formatted_contacts: List[FormattedContact] = [] | ||
|
||
for contact in contacts: | ||
if contact.get("public", False): | ||
formatted_contact: FormattedContact = { | ||
"name": " ".join( | ||
[ | ||
c | ||
for c in [ | ||
contact.get("first_name", ""), | ||
contact.get("last_name", ""), | ||
] | ||
if c | ||
] | ||
), | ||
"organisation": contact.get("organisation", ""), | ||
"email": contact.get("contact_email", []), | ||
"phone": [c.get("number", "") for c in contact.get("contact_phone", []) if c.get("public")], | ||
"mobile": [c.get("number", "") for c in contact.get("mobile", []) if c.get("public")], | ||
"website": contact.get("website", ""), | ||
} | ||
formatted_contacts.append(formatted_contact) | ||
|
||
return formatted_contacts | ||
|
||
|
||
def parse_date(datetime: Union[str, datetime]) -> datetime: | ||
"""Return datetime instance for datetime.""" | ||
if isinstance(datetime, str): | ||
try: | ||
return str_to_date(datetime) | ||
except ValueError: | ||
return arrow.get(datetime).datetime | ||
return datetime | ||
|
||
|
||
def time_short(datetime: datetime): | ||
if datetime: | ||
return format_time(parse_date(datetime), app.config.get("TIME_FORMAT_SHORT", "HH:mm")) | ||
|
||
|
||
def date_short(datetime: datetime): | ||
if datetime: | ||
return format_datetime(parse_date(datetime), app.config.get("DATE_FORMAT_SHORT", "dd/MM/yyyy")) | ||
|
||
|
||
def get_event_formatted_dates(event: Dict[str, Any]) -> str: | ||
start = event.get("dates", {}).get("start") | ||
end = event.get("dates", {}).get("end") | ||
|
||
duration_seconds = int((end - start).total_seconds()) | ||
|
||
if duration_seconds == ALL_DAY_SECONDS: | ||
# All day event | ||
return "{} {}".format(lazy_gettext("ALL DAY"), date_short(start)) | ||
|
||
if duration_seconds >= MULTI_DAY_SECONDS: | ||
# Multi day event | ||
return "{} {} - {} {}".format(time_short(start), date_short(start), time_short(end), date_short(end)) | ||
|
||
if start == end: | ||
# start and end are the same | ||
return "{} {}".format(time_short(start), date_short(start)) | ||
|
||
return "{} - {}, {}".format(time_short(start), time_short(end), date_short(start)) |