cic-stack/apps/cic-ussd/cic_ussd/account/metadata.py

44 lines
1.4 KiB
Python
Raw Normal View History

2021-08-06 18:29:01 +02:00
# standard imports
import json
import logging
from typing import Optional
# external imports
from cic_types.models.person import Person
# local imports
from cic_ussd.metadata import PreferencesMetadata
logg = logging.getLogger(__name__)
def get_cached_preferred_language(blockchain_address: str) -> Optional[str]:
"""This function retrieves an account's set preferred language from preferences metadata in redis cache.
:param blockchain_address:
:type blockchain_address:
:return: Account's set preferred language | Fallback preferred language.
:rtype: str
"""
2021-10-07 17:12:35 +02:00
identifier = bytes.fromhex(blockchain_address)
2021-08-06 18:29:01 +02:00
preferences_metadata_handler = PreferencesMetadata(identifier)
cached_preferences_metadata = preferences_metadata_handler.get_cached_metadata()
if cached_preferences_metadata:
preferences_metadata = json.loads(cached_preferences_metadata)
return preferences_metadata.get('preferred_language')
return None
def parse_account_metadata(account_metadata: dict) -> str:
"""
:param account_metadata:
:type account_metadata:
:return:
:rtype:
"""
person = Person()
deserialized_person = person.deserialize(person_data=account_metadata)
given_name = deserialized_person.given_name
family_name = deserialized_person.family_name
phone_number = deserialized_person.tel
return f'{given_name} {family_name} {phone_number}'