Skip to content

Commit

Permalink
Merge pull request #236 from ZeusWPI/fix-235-opening-hours
Browse files Browse the repository at this point in the history
Don't crash when opening hours syntax is invalid
  • Loading branch information
msathieu authored Oct 10, 2024
2 parents 3258b86 + a238da4 commit f2bd719
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions app/hlds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datetime import datetime, timedelta
from typing import Any, Iterable, List, Mapping, Optional, Tuple
from opening_hours import OpeningHours
import warnings

from ..utils import euro_string, first

Expand Down Expand Up @@ -133,7 +134,13 @@ def dish_by_id(self, dish_id: str) -> Optional[Dish]:
def is_open(self) -> bool | None:
if not self.opening_hours:
return None
return OpeningHours(self.opening_hours).is_open()
# Try to parse OSM opening hours, but ignore syntax errors
# See https://wiki.openstreetmap.org/wiki/Key:opening_hours/specification
try:
return OpeningHours(self.opening_hours).is_open()
except SyntaxError:
warnings.warn(f"Opening hour syntax parsing for {self.name} failed")
return None

def is_open_symbol(self) -> str:
if self.opening_hours is None:
Expand All @@ -144,10 +151,14 @@ def is_open_symbol(self) -> str:
def next_change_str(self) -> Optional[str]:
if self.opening_hours is None:
return "Unknown, add opening hours to OSM!"
try:
openinghours = OpeningHours(self.opening_hours)
except SyntaxError:
return "Unknown, OSM opening hours contain syntax error"

state_str = "Opening" if not self.is_open() else "Closing"

next_time: datetime = OpeningHours(self.opening_hours).next_change()
next_time: datetime = openinghours.next_change()
if next_time.date() == datetime.now().date():
time_str = next_time.strftime("today at %H:%M")
elif next_time.date() == datetime.now().date() + timedelta(days=1):
Expand Down

0 comments on commit f2bd719

Please sign in to comment.