Skip to content

Commit 8d83f8b

Browse files
authored
Feature/time coordinator (#297)
1 parent 66c9fdf commit 8d83f8b

File tree

11 files changed

+989
-7
lines changed

11 files changed

+989
-7
lines changed

app/database/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,14 @@ class Quote(Base):
487487
author = Column(String)
488488

489489

490+
class Country(Base):
491+
__tablename__ = "countries"
492+
493+
id = Column(Integer, primary_key=True, index=True)
494+
name = Column(String, nullable=False, unique=True)
495+
timezone = Column(String, nullable=False)
496+
497+
490498
class Comment(Base):
491499
__tablename__ = "comments"
492500

app/internal/event.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import functools
12
import logging
23
import re
34
from typing import List, NamedTuple, Set, Union
@@ -11,7 +12,8 @@
1112
from sqlalchemy.orm import Session
1213
from starlette.status import HTTP_400_BAD_REQUEST
1314

14-
from app.database.models import Event
15+
from app.database.models import Country, Event
16+
from app.resources.countries import countries
1517

1618
ZOOM_REGEX = re.compile(r"https://.*?\.zoom.us/[a-z]/.[^.,\b\s]+")
1719

@@ -114,6 +116,43 @@ def get_messages(
114116
return messages
115117

116118

119+
def add_countries_to_db(session: Session) -> None:
120+
"""
121+
Adding all new countries to the "Country" table in the database.
122+
Information is based on the "countries" list.
123+
(The list is located in app/resources/countries.py)
124+
Names are described either as:
125+
"Country Name, City Name" or
126+
"Country Name" solely.
127+
Timezones are described as "Continent/ City Name"
128+
for example:
129+
name: Israel, Jerusalem
130+
timezone: Asia/Jerusalem
131+
"""
132+
for country in countries:
133+
partial_name = country["name"]
134+
for capital_city in country["timezones"]:
135+
capital_city_name = capital_city.split("/")[-1]
136+
if partial_name != capital_city_name:
137+
name = partial_name + ", " + capital_city_name
138+
else:
139+
name = capital_city_name
140+
new_country = Country(name=name, timezone=str(capital_city))
141+
session.merge(new_country)
142+
session.commit()
143+
144+
145+
@functools.lru_cache
146+
def get_all_countries_names(session: Session) -> List[str]:
147+
"""
148+
Returns a cached list of the countries names.
149+
"""
150+
db_entity = session.query(Country).first()
151+
if not db_entity:
152+
add_countries_to_db(session=session)
153+
return session.query(Country.name).all()
154+
155+
117156
async def get_location_coordinates(
118157
address: str,
119158
) -> Union[Location, str]:

0 commit comments

Comments
 (0)