Skip to content

Commit

Permalink
start consolidating models
Browse files Browse the repository at this point in the history
  • Loading branch information
NodeJSmith committed Dec 29, 2024
1 parent d5a8fe5 commit 0abd89e
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 225 deletions.
46 changes: 16 additions & 30 deletions src/otf_api/models/bookings.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
from collections.abc import Hashable
from datetime import datetime
from typing import Any

from pydantic import Field
from pydantic import AliasPath, Field

from otf_api.models.base import OtfItemBase
from otf_api.models.enums import BookingStatus, StudioStatus
from otf_api.models.mixins import OtfClassTimeMixin
from otf_api.models.mixins import AddressMixin, OtfClassTimeMixin, PhoneLongitudeLatitudeMixin


class Location(OtfItemBase):
address_one: str | None = Field(None, alias="address1")
address_two: str | None = Field(alias="address2")
city: str | None = None
country: str | None = None
distance: float | None = None
location_name: str | None = Field(None, alias="locationName")
latitude: float | None = Field(None, alias="latitude")
longitude: float | None = Field(None, alias="longitude")
phone_number: str | None = Field(None, alias="phone")
postal_code: str | None = Field(None, alias="postalCode")
state: str | None = None
class CountryCurrency(OtfItemBase):
country_currency_code: str = Field(..., alias="countryCurrencyCode")
currency_id: int | None = Field(None, alias=AliasPath("defaultCurrency", "currencyId"))
currency_alphabetic_code: str | None = Field(None, alias=AliasPath("defaultCurrency", "currencyAlphabeticCode"))


class Location(PhoneLongitudeLatitudeMixin, AddressMixin):
distance: float | None = Field(None, alias="distance", exclude=True, repr=False)
location_name: str | None = Field(None, alias="locationName", exclude=True, repr=False)


class Coach(OtfItemBase):
Expand All @@ -32,25 +27,16 @@ class Coach(OtfItemBase):
profile_picture_url: str | None = Field(None, alias="profilePictureUrl", exclude=True)


class StudioLocation(OtfItemBase):
latitude: float | None = Field(None, alias="latitude")
longitude: float | None = Field(None, alias="longitude")
phone_number: str | None = Field(None, alias="phoneNumber")
physical_city: str | None = Field(None, alias="physicalCity")
physical_address: str | None = Field(None, alias="physicalAddress")
physical_address2: str | None = Field(None, alias="physicalAddress2")
physical_state: str | None = Field(None, alias="physicalState")
physical_postal_code: str | None = Field(None, alias="physicalPostalCode")
physical_region: str | None = Field(None, alias="physicalRegion", exclude=True)
physical_country_id: int | None = Field(None, alias="physicalCountryId", exclude=True)
physical_country: str | None = Field(None, alias="physicalCountry")
country: dict[Hashable, Any] | None = Field(None, alias="country", exclude=True)
class StudioLocation(PhoneLongitudeLatitudeMixin, AddressMixin):
physical_region: str | None = Field(None, alias="physicalRegion", exclude=True, repr=False)
physical_country_id: int | None = Field(None, alias="physicalCountryId", exclude=True, repr=False)
country_currency: CountryCurrency | None = Field(None, alias="country_currency", exclude=True, repr=False)


class Studio(OtfItemBase):
studio_uuid: str = Field(alias="studioUUId")
studio_name: str = Field(alias="studioName")
studio_id: int = Field(alias="studioId")
studio_id: int = Field(alias="studioId", exclude=True, description="Not used by API")
description: str | None = None
contact_email: str | None = Field(None, alias="contactEmail", exclude=True)
status: StudioStatus | None = None
Expand Down
30 changes: 7 additions & 23 deletions src/otf_api/models/classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,16 @@

from otf_api.models.base import OtfItemBase
from otf_api.models.enums import DoW
from otf_api.models.mixins import OtfClassTimeMixin
from otf_api.models.mixins import AddressMixin, OtfClassTimeMixin, PhoneLongitudeLatitudeMixin


class Address(OtfItemBase):
line1: str
city: str
state: str
country: str
postal_code: str


class Studio(OtfItemBase):
id: str
class Studio(PhoneLongitudeLatitudeMixin, OtfItemBase):
studio_uuid: str = Field(alias="id")
name: str
mbo_studio_id: str
mbo_studio_id: str = Field(exclude=True)
time_zone: str
currency_code: str | None = None
address: Address
phone_number: str
latitude: float
longitude: float
address: AddressMixin


class Coach(OtfItemBase):
Expand All @@ -35,7 +24,7 @@ class Coach(OtfItemBase):

class OtfClass(OtfItemBase, OtfClassTimeMixin):
id: str
ot_class_uuid: str = Field(
class_uuid: str = Field(
alias="ot_base_class_uuid",
description="The OTF class UUID, this is what shows in a booking response and how you can book a class.",
)
Expand All @@ -52,7 +41,7 @@ class OtfClass(OtfItemBase, OtfClassTimeMixin):
waitlist_size: int
full: bool
waitlist_available: bool
canceled: bool
is_cancelled: bool = Field(alias="canceled")
mbo_class_id: str
mbo_class_schedule_id: str
mbo_class_description_id: str
Expand All @@ -70,11 +59,6 @@ def day_of_week_enum(self) -> DoW:
dow = self.starts_at_local.strftime("%A").upper()
return DoW(dow)

@property
def actual_class_uuid(self) -> str:
"""The UUID used to book the class"""
return self.ot_class_uuid


class OtfClassList(OtfItemBase):
classes: list[OtfClass]
Expand Down
68 changes: 22 additions & 46 deletions src/otf_api/models/favorite_studios.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,39 @@
from datetime import datetime

from pydantic import Field
from inflection import camelize
from pydantic import Field, model_validator

from otf_api.models.base import OtfItemBase
from otf_api.models.mixins import AddressMixin, PhoneLongitudeLatitudeMixin


class Location(OtfItemBase):
class Location(PhoneLongitudeLatitudeMixin, AddressMixin):
location_id: int = Field(..., alias="locationId")
location_uuid: str = Field(..., alias="locationUUId")
studio_id: int = Field(..., alias="studioId")
mbo_location_id: int = Field(..., alias="mboLocationId")
mbo_studio_id: int = Field(..., alias="mboStudioId")
latitude: float
longitude: float
address1: str
address2: str | None = None
city: str
state: str
phone: str
postal_code: str = Field(..., alias="postalCode")


class StudioLocation(OtfItemBase):
bill_to_address: str = Field(..., alias="billToAddress")
bill_to_address2: str = Field(..., alias="billToAddress2")
bill_to_city: str = Field(..., alias="billToCity")
bill_to_state: str = Field(..., alias="billToState")
bill_to_postal_code: str = Field(..., alias="billToPostalCode")
bill_to_region: str = Field(..., alias="billToRegion")
bill_to_country_id: int = Field(..., alias="billToCountryId")
bill_to_country: str = Field(..., alias="billToCountry")
ship_to_address: str = Field(..., alias="shipToAddress")
ship_to_address2: str | None = Field(None, alias="shipToAddress2")
ship_to_city: str = Field(..., alias="shipToCity")
ship_to_state: str = Field(..., alias="shipToState")
ship_to_postal_code: str = Field(..., alias="shipToPostalCode")
ship_to_region: str = Field(..., alias="shipToRegion")
ship_to_country_id: int = Field(..., alias="shipToCountryId")
ship_to_country: str = Field(..., alias="shipToCountry")
physical_address: str = Field(..., alias="physicalAddress")
physical_address2: str | None = Field(None, alias="physicalAddress2")
physical_city: str = Field(..., alias="physicalCity")
physical_state: str = Field(..., alias="physicalState")
physical_postal_code: str = Field(..., alias="physicalPostalCode")
physical_region: str = Field(..., alias="physicalRegion")
physical_country_id: int = Field(..., alias="physicalCountryId")
physical_country: str = Field(..., alias="physicalCountry")
phone_number: str = Field(..., alias="phoneNumber")
latitude: str
longitude: str


class StudioLocation(PhoneLongitudeLatitudeMixin, AddressMixin):
studio_location_id: int | None = Field(None, alias="studioLocationId", exclude=True, repr=False)
billing_address: AddressMixin | None = Field(None, exclude=True, repr=False)
shipping_address: AddressMixin | None = Field(None, exclude=True, repr=False)

@model_validator(mode="before")
@classmethod
def handle_addresses(cls, values):
for prefix, field in [("billTo", "billing_address"), ("shipTo", "shipping_address")]:
address_values = {k.replace(prefix, ""): v for k, v in values.items() if k.startswith(prefix)}
address_values = {camelize(k, uppercase_first_letter=False): v for k, v in address_values.items()}
values[field] = AddressMixin(**address_values)

return values


class FavoriteStudio(OtfItemBase):
studio_id: int = Field(..., alias="studioId")
studio_uuid: str = Field(..., alias="studioUUId")
studio_uuid: str = Field(..., alias="studioUUId", description="The studio UUID, used by API")
studio_id: int = Field(..., alias="studioId", description="Not used by API", exclude=True, repr=False)
mbo_studio_id: int = Field(..., alias="mboStudioId")
studio_name: str = Field(..., alias="studioName")
area_id: int | None = Field(None, alias="areaId")
Expand Down Expand Up @@ -97,10 +77,6 @@ class FavoriteStudio(OtfItemBase):
class FavoriteStudioList(OtfItemBase):
studios: list[FavoriteStudio]

@property
def studio_ids(self) -> list[int]:
return [studio.studio_id for studio in self.studios]

@property
def studio_uuids(self) -> list[str]:
return [studio.studio_uuid for studio in self.studios]
Expand Down
2 changes: 1 addition & 1 deletion src/otf_api/models/member_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,5 @@ def validate_birth_day(cls, value: date | str | None, **_kwargs) -> date | None:
if value is None:
return value
if not isinstance(value, date):
return datetime.strptime(value, "%Y-%m-%d").date() # noqa
return datetime.strptime(value, "%Y-%m-%d").date()
return value
Loading

0 comments on commit 0abd89e

Please sign in to comment.