-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlocation_impl.py
More file actions
29 lines (25 loc) · 1 KB
/
location_impl.py
File metadata and controls
29 lines (25 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from feeds_gen.models.location import Location
import pycountry
from shared.database_gen.sqlacodegen_models import Location as LocationOrm
class LocationImpl(Location):
class Config:
"""Pydantic configuration.
Enabling `from_attributes` method to create a model instance from a SQLAlchemy row object."""
from_attributes = True
@classmethod
def from_orm(cls, location: LocationOrm | None) -> Location | None:
"""Create a model instance from a SQLAlchemy a Location row object."""
if not location:
return None
country_name = location.country
if not country_name:
try:
country_name = pycountry.countries.get(alpha_2=location.country_code).name
except AttributeError:
pass
return cls(
country_code=location.country_code,
country=country_name,
subdivision_name=location.subdivision_name,
municipality=location.municipality,
)