Skip to content

Commit

Permalink
location is no longer optional (#7)
Browse files Browse the repository at this point in the history
* location is no longer optional

* refactor date function
  • Loading branch information
spicydilly authored Sep 30, 2023
1 parent 5f2adca commit 78a4647
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 29 deletions.
13 changes: 5 additions & 8 deletions src/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from typing import Optional

TICKETS_PREFIX = "Tickets:"
LOCATION_PREFIX = "Location:"
WEBSITE_PREFIX = "Website:"


Expand All @@ -12,12 +11,12 @@ class Event:
"""Dataclass to represent an event"""

title: str = field(default=None)
location: str = field(default=None)
description: str = field(default=None)
date: str = field(default=None)
start_time: str = field(default=None)
end_time: str = field(default=None)
tickets: Optional[str] = None
location: Optional[str] = None
website: Optional[str] = None

TEMPLATE_PATH = (
Expand All @@ -33,6 +32,8 @@ def template_content(self) -> str:
def __post_init__(self):
if not self.title:
raise ValueError("Title is required.")
if not self.location:
raise ValueError("Location is required.")
if not self.description:
raise ValueError("Description is required.")
if not self.date:
Expand All @@ -58,8 +59,6 @@ def _parse_description(self, description: str) -> None:
for line in lines:
if line.startswith(TICKETS_PREFIX):
self.tickets = line.replace(TICKETS_PREFIX, "").strip()
elif line.startswith(LOCATION_PREFIX):
self.location = line.replace(LOCATION_PREFIX, "").strip()
elif line.startswith(WEBSITE_PREFIX):
self.website = line.replace(WEBSITE_PREFIX, "").strip()

Expand All @@ -68,9 +67,7 @@ def _parse_description(self, description: str) -> None:
[
line
for line in lines
if not line.startswith(
(TICKETS_PREFIX, LOCATION_PREFIX, WEBSITE_PREFIX)
)
if not line.startswith((TICKETS_PREFIX, WEBSITE_PREFIX))
]
).strip()

Expand All @@ -83,7 +80,6 @@ def format(event: Event) -> str:
# List of optional fields with conditions
optional_fields_data = [
(TICKETS_PREFIX, event.tickets),
(LOCATION_PREFIX, event.location),
(WEBSITE_PREFIX, event.website),
]

Expand All @@ -99,6 +95,7 @@ def format(event: Event) -> str:

return event.template_content.format(
title=event.title,
location=event.location,
description=event.description,
date=event.date,
start_time=event.start_time,
Expand Down
20 changes: 11 additions & 9 deletions src/google_calendar_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ def get_events_this_month(self) -> List[Event]:
first_day_of_month + relativedelta(months=1, days=-1)
).replace(hour=23, minute=59, second=59, microsecond=999999)

def extract_datetime(data):
return datetime.datetime.fromisoformat(
data.get("dateTime", data.get("date")).split("Z")[0]
)

try:
events_result = (
self.service.events()
Expand All @@ -90,15 +85,16 @@ def extract_datetime(data):
events_list: List[Event] = [
Event(
title=event.get("summary", ""),
location=event.get("location", ""),
description=event.get("description", ""),
start_time=extract_datetime(event["start"])
start_time=self.get_date(event["start"])
.strftime("%I%p")
.lstrip("0"),
date=(
f"{extract_datetime(event['start']).strftime('%b')} "
f"{self.ordinal(extract_datetime(event['start']).day)}"
f"{self.get_date(event['start']).strftime('%b')} "
f"{self.ordinal(self.get_date(event['start']).day)}"
),
end_time=extract_datetime(event["end"])
end_time=self.get_date(event["end"])
.strftime("%I%p")
.lstrip("0"),
)
Expand All @@ -110,6 +106,12 @@ def extract_datetime(data):
self.logger.error(f"Error fetching events: {e}")
return []

@staticmethod
def get_date(data):
return datetime.datetime.fromisoformat(
data.get("dateTime", data.get("date")).split("Z")[0]
)

@staticmethod
def ordinal(n) -> str:
"""Return number n with an ordinal string suffix."""
Expand Down
2 changes: 1 addition & 1 deletion src/templates/monthly_events_template.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*{title}* - {date} @ {start_time} to {end_time}
*{title}* - {date} {start_time} to {end_time} @ {location}
{description}
{optional_fields}
27 changes: 16 additions & 11 deletions tests/test_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def test_missing_title(self):
with self.assertRaises(ValueError) as context:
Event(
description="This is a sample event.",
location="A Place",
date="2023-09-26",
start_time="10:00",
end_time="12:00",
Expand All @@ -18,6 +19,7 @@ def test_missing_description(self):
with self.assertRaises(ValueError) as context:
Event(
title="Sample Event",
location="A Place",
date="2023-09-26",
start_time="10:00",
end_time="12:00",
Expand All @@ -28,6 +30,7 @@ def test_missing_date(self):
with self.assertRaises(ValueError) as context:
Event(
title="Sample Event",
location="A Place",
description="This is a sample event.",
start_time="10:00",
end_time="12:00",
Expand All @@ -38,6 +41,7 @@ def test_missing_start_time(self):
with self.assertRaises(ValueError) as context:
Event(
title="Sample Event",
location="A Place",
date="2023-09-26",
description="This is a sample event.",
end_time="12:00",
Expand All @@ -48,6 +52,7 @@ def test_missing_end_time(self):
with self.assertRaises(ValueError) as context:
Event(
title="Sample Event",
location="A Place",
date="2023-09-26",
description="This is a sample event.",
start_time="10:00",
Expand All @@ -57,50 +62,50 @@ def test_missing_end_time(self):
def test_pretty_with_mandatory_fields(self):
event = Event(
title="Sample Event",
location="A Place",
description="This is a sample event.",
date="2023-09-26",
start_time="10:00",
end_time="12:00",
)
expected_output = (
"*Sample Event* - 2023-09-26 @ 10:00 to 12:00\n"
"*Sample Event* - 2023-09-26 10:00 to 12:00 @ A Place\n"
"This is a sample event."
)
self.assertEqual(EventFormatter.format(event), expected_output)

def test_pretty_with_all_fields(self):
event = Event(
title="Sample Event",
location="A Place",
description=(
"This is a sample event.\nTickets: www.tickets.com\nLocation:"
" Main Hall\nWebsite: www.event-website.com"
"This is a sample event.\nTickets: www.tickets.com\nWebsite:"
" www.event-website.com"
),
date="2023-09-26",
start_time="10:00",
end_time="12:00",
)
expected_output = (
"*Sample Event* - 2023-09-26 @ 10:00 to 12:00\nThis is a sample"
" event.\n[Tickets](www.tickets.com) | [Location](Main Hall) |"
"*Sample Event* - 2023-09-26 10:00 to 12:00 @ A Place\nThis is a"
" sample event.\n[Tickets](www.tickets.com) |"
" [Website](www.event-website.com)"
)
self.assertEqual(EventFormatter.format(event), expected_output)

def test_pretty_with_some_optional_fields(self):
event = Event(
title="Sample Event",
description=(
"This is a sample event.\nTickets: www.tickets.com\nLocation:"
" Main Hall"
),
location="A Place",
description="This is a sample event.\nTickets: www.tickets.com",
date="2023-09-26",
start_time="10:00",
end_time="12:00",
)
expected_output = (
"*Sample Event* - 2023-09-26 @ 10:00 to 12:00\n"
"*Sample Event* - 2023-09-26 10:00 to 12:00 @ A Place\n"
"This is a sample event.\n"
"[Tickets](www.tickets.com) | [Location](Main Hall)"
"[Tickets](www.tickets.com)"
)
self.assertEqual(EventFormatter.format(event), expected_output)

Expand Down
4 changes: 4 additions & 0 deletions tests/test_google_calendar_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ def test_get_events_this_month_with_two_events(self):
mock_response_items = [
{
"summary": "Test Event 1",
"location": "A Place",
"description": "Description 1",
"start": {"dateTime": "2023-09-19T10:00:00+01:00"},
"end": {"dateTime": "2023-09-19T11:00:00+01:00"},
},
{
"summary": "Test Event 2",
"location": "A Place",
"description": "Description 2",
"start": {"dateTime": "2023-09-20T00:00:00+01:00"},
"end": {"dateTime": "2023-09-20T01:00:00+01:00"},
Expand All @@ -45,13 +47,15 @@ def test_get_events_this_month_with_two_events(self):
expected_events = [
Event(
title="Test Event 1",
location="A Place",
description="Description 1",
date="Sep 19th",
start_time="10AM",
end_time="11AM",
),
Event(
title="Test Event 2",
location="A Place",
description="Description 2",
date="Sep 20th",
start_time="12AM",
Expand Down

0 comments on commit 78a4647

Please sign in to comment.