Spaces:
Running
Running
from streamlit_local_storage import LocalStorage | |
from local_storage.entities import SavedDetails, PersonalDetails, LocationDetails, ContractorDetails | |
from enums.enums import DetailsType | |
class LocalStorageManager: | |
def __init__(self): | |
self.ls: LocalStorage = LocalStorage() | |
def get_detail(self, key: str = None): | |
for detail_type in DetailsType.values(): | |
detail = self.ls.getItem(detail_type.name).get(key) | |
if detail: | |
if detail_type.value == DetailsType.PERSONAL_DETAILS.value: | |
return PersonalDetails(**detail) | |
elif detail_type.value == DetailsType.LOCATION_DETAILS.value: | |
return LocationDetails(**detail) | |
elif detail_type.value == DetailsType.CONTRACTOR_DETAILS.value: | |
return ContractorDetails(**detail) | |
return None | |
return None | |
def save_details(self, details: SavedDetails): | |
if isinstance(details, PersonalDetails): | |
type_ = DetailsType.PERSONAL_DETAILS | |
elif isinstance(details, LocationDetails): | |
type_ = DetailsType.LOCATION_DETAILS | |
elif isinstance(details, ContractorDetails): | |
type_ = DetailsType.CONTRACTOR_DETAILS | |
else: | |
raise ValueError("Unexpected type: {}", type(details)) | |
existing_data = (self.ls.getItem(type_.name) or {}) | |
existing_data[details.key] = details.to_json() | |
self.ls.setItem(type_.name, existing_data, type_.name) | |
def get_details(self, type_: DetailsType) -> list[SavedDetails]: | |
if type_.value == DetailsType.PERSONAL_DETAILS.value: | |
cls = PersonalDetails | |
elif type_.value == DetailsType.LOCATION_DETAILS.value: | |
cls = LocationDetails | |
else: | |
cls = ContractorDetails | |
details = self.ls.getItem(type_.name) or {} | |
return [cls(**v) for k,v in details.items()] | |