DAMHelper / local_storage /entities.py
enricorampazzo's picture
now saving personal, location and contractor details in the browser local storage correctly πŸ˜…
cb7ff7d
raw
history blame
1.84 kB
import abc
from enum import Enum
class DetailsType(Enum):
PERSONAL_DETAILS = 1
LOCATION_DETAILS = 2
CONTRACTOR_DETAILS = 3
@classmethod
def values(cls):
return [cls.PERSONAL_DETAILS, cls.LOCATION_DETAILS, cls.CONTRACTOR_DETAILS]
class SavedDetails(abc.ABC):
excluded_fields = ["type_"]
def __init__(self, type_: DetailsType):
self.type_ = type_
@classmethod
def load(cls, data: dict):
type_ = data.get("type_")
if not type_ or type_ != cls.type_:
raise ValueError(f"the expected type is {cls.type_} but is actually {type_}")
return cls.__init__(**{k: v for k, v in data if k != "type"})
def to_json(self):
return {k:v for k,v in self.__dict__.items() if k not in self.excluded_fields}
class PersonalDetails(SavedDetails):
type_ = DetailsType.PERSONAL_DETAILS
def __init__(self, full_name, email, contact_number, *_):
super().__init__(self.type_)
self.full_name = full_name
self.email = email
self.contact_number = contact_number
class LocationDetails(SavedDetails):
type_ = DetailsType.LOCATION_DETAILS
def __init__(self, owner_or_tenant: str, community: str, building: str, unit_number: str, *_):
super().__init__(self.type_)
self.owner_or_tenant = owner_or_tenant
self.community = community
self.building = building
self.unit_number = unit_number
class ContractorDetails(SavedDetails):
type_ = DetailsType.CONTRACTOR_DETAILS
def __init__(self, contractor_name: str, contractor_contact_number: str, contractor_email: str, *_):
super().__init__(self.type_)
self.contractor_name = contractor_name
self.contractor_contact_number = contractor_contact_number
self.contractor_email = contractor_email