Spaces:
Running
Running
import json | |
from streamlit_local_storage import LocalStorage | |
from local_storage.entities import SavedDetails, PersonalDetails, LocationDetails, ContractorDetails | |
from enums import DetailsType | |
def get_detail(key: str): | |
for detail_type in DetailsType.values(): | |
detail = LocalStorage(key=detail_type.name).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(details: SavedDetails, key: str): | |
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 = (ls.getItem(type_.name) or {}) | |
existing_data[key] = details.to_json() | |
ls.setItem(type_.name, existing_data, type_.name) | |
def get_details(type_: DetailsType): | |
return ls.getItem(type_.name) or {} | |