cic-stack/apps/cic-ussd/cic_ussd/state_machine/logic/validator.py

62 lines
2.0 KiB
Python
Raw Normal View History

2021-02-06 16:13:47 +01:00
# standard imports
import logging
import re
from typing import Tuple
# third-party imports
2021-08-06 18:29:01 +02:00
from chainlib.hash import strip_0x
from sqlalchemy.orm.session import Session
2021-02-06 16:13:47 +01:00
# local imports
2021-04-19 10:44:40 +02:00
from cic_ussd.db.models.account import Account
2021-08-06 18:29:01 +02:00
from cic_ussd.metadata import PersonMetadata
2021-02-06 16:13:47 +01:00
logg = logging.getLogger()
2021-08-06 18:29:01 +02:00
def has_cached_person_metadata(state_machine_data: Tuple[str, dict, Account, Session]):
2021-02-06 16:13:47 +01:00
"""This function checks whether the attributes of the user's metadata constituting a profile are filled out.
:param state_machine_data: A tuple containing user input, a ussd session and user object.
:type state_machine_data: str
"""
2021-08-06 18:29:01 +02:00
user_input, ussd_session, account, session = state_machine_data
identifier = bytes.fromhex(strip_0x(account.blockchain_address))
metadata_client = PersonMetadata(identifier)
return metadata_client.get_cached_metadata() is not None
2021-02-06 16:13:47 +01:00
2021-08-06 18:29:01 +02:00
def is_valid_name(state_machine_data: Tuple[str, dict, Account, Session]):
2021-02-06 16:13:47 +01:00
"""This function checks that a user provided name is valid
:param state_machine_data: A tuple containing user input, a ussd session and user object.
:type state_machine_data: str
"""
2021-08-06 18:29:01 +02:00
user_input, ussd_session, account, session = state_machine_data
2021-02-06 16:13:47 +01:00
name_matcher = "^[a-zA-Z]+$"
valid_name = re.match(name_matcher, user_input)
2021-08-06 18:29:01 +02:00
return bool(valid_name)
2021-08-06 18:29:01 +02:00
def is_valid_gender_selection(state_machine_data: Tuple[str, dict, Account, Session]):
"""
:param state_machine_data:
:type state_machine_data:
:return:
:rtype:
"""
2021-08-06 18:29:01 +02:00
user_input, ussd_session, account, session = state_machine_data
selection_matcher = "^[1-3]$"
return bool(re.match(selection_matcher, user_input))
2021-06-23 15:25:09 +02:00
2021-08-06 18:29:01 +02:00
def is_valid_date(state_machine_data: Tuple[str, dict, Account, Session]):
2021-06-23 15:25:09 +02:00
"""
:param state_machine_data:
:type state_machine_data:
:return:
:rtype:
"""
2021-08-06 18:29:01 +02:00
user_input, ussd_session, account, session = state_machine_data
2021-06-23 15:25:09 +02:00
# For MVP this value is defaulting to year
return len(user_input) == 4 and int(user_input) >= 1900