From a05df2280b9b793d6acf263654767fcea0bf8390 Mon Sep 17 00:00:00 2001 From: PhilipWafula Date: Thu, 27 May 2021 19:03:43 +0300 Subject: [PATCH] Refactors state machine logic. --- .../cic_ussd/state_machine/logic/pin.py | 17 ++--- .../cic_ussd/state_machine/logic/user.py | 63 ++++++++++++------- 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/apps/cic-ussd/cic_ussd/state_machine/logic/pin.py b/apps/cic-ussd/cic_ussd/state_machine/logic/pin.py index 3577699d..5f51e602 100644 --- a/apps/cic-ussd/cic_ussd/state_machine/logic/pin.py +++ b/apps/cic-ussd/cic_ussd/state_machine/logic/pin.py @@ -13,7 +13,7 @@ import bcrypt # local imports from cic_ussd.db.models.account import AccountStatus, Account -from cic_ussd.encoder import PasswordEncoder, create_password_hash +from cic_ussd.encoder import PasswordEncoder, create_password_hash, check_password_hash from cic_ussd.operations import persist_session_to_db_task, create_or_update_session from cic_ussd.redis import InMemoryStore @@ -78,9 +78,13 @@ def save_initial_pin_to_session_data(state_machine_data: Tuple[str, dict, Accoun # set initial pin data initial_pin = create_password_hash(user_input) - session_data = { - 'initial_pin': initial_pin - } + if ussd_session.get('session_data'): + session_data = ussd_session.get('session_data') + session_data['initial_pin'] = initial_pin + else: + session_data = { + 'initial_pin': initial_pin + } # create new in memory ussd session with current ussd session data create_or_update_session( @@ -103,9 +107,8 @@ def pins_match(state_machine_data: Tuple[str, dict, Account]) -> bool: """ user_input, ussd_session, user = state_machine_data initial_pin = ussd_session.get('session_data').get('initial_pin') - fernet = PasswordEncoder(PasswordEncoder.key) - initial_pin = fernet.decrypt(initial_pin.encode()) - return bcrypt.checkpw(user_input.encode(), initial_pin) + logg.debug(f'USSD SESSION: {ussd_session}') + return check_password_hash(user_input, initial_pin) def complete_pin_change(state_machine_data: Tuple[str, dict, Account]): diff --git a/apps/cic-ussd/cic_ussd/state_machine/logic/user.py b/apps/cic-ussd/cic_ussd/state_machine/logic/user.py index da8a87db..d2db5d29 100644 --- a/apps/cic-ussd/cic_ussd/state_machine/logic/user.py +++ b/apps/cic-ussd/cic_ussd/state_machine/logic/user.py @@ -64,13 +64,17 @@ def process_gender_user_input(user: Account, user_input: str): if user.preferred_language == 'en': if user_input == '1': gender = 'Male' - else: + elif user_input == '2': gender = 'Female' + elif user_input == '3': + gender = 'Other' else: if user_input == '1': gender = 'Mwanaume' - else: + elif user_input == '2': gender = 'Mwanamke' + elif user_input == '3': + gender = 'Nyingine' return gender @@ -88,14 +92,18 @@ def save_metadata_attribute_to_session_data(state_machine_data: Tuple[str, dict, key = '' if 'given_name' in current_state: key = 'given_name' - elif 'family_name' in current_state: + + if 'family_name' in current_state: key = 'family_name' - elif 'gender' in current_state: + + if 'gender' in current_state: key = 'gender' user_input = process_gender_user_input(user=user, user_input=user_input) - elif 'location' in current_state: + + if 'location' in current_state: key = 'location' - elif 'products' in current_state: + + if 'products' in current_state: key = 'products' # check if there is existing session data @@ -121,12 +129,20 @@ def format_user_metadata(metadata: dict, user: Account): gender = metadata.get('gender') given_name = metadata.get('given_name') family_name = metadata.get('family_name') - location = { - "area_name": metadata.get('location') - } - products = [] - if metadata.get('products'): + + # check whether there's existing location data + if isinstance(metadata.get('location'), dict): + location = metadata.get('location') + else: + location = { + "area_name": metadata.get('location') + } + # check whether it is a list + if isinstance(metadata.get('products'), list): + products = metadata.get('products') + else: products = metadata.get('products').split(',') + phone_number = user.phone_number date_registered = int(user.created.replace().timestamp()) blockchain_address = user.blockchain_address @@ -192,28 +208,27 @@ def edit_user_metadata_attribute(state_machine_data: Tuple[str, dict, Account]): # validate user metadata person = Person() user_metadata = json.loads(user_metadata) - deserialized_person = person.deserialize(person_data=user_metadata) # edit specific metadata attribute if given_name: - deserialized_person.given_name = given_name - elif family_name: - deserialized_person.family_name = family_name - elif gender: - deserialized_person.gender = gender - elif location: + user_metadata['given_name'] = given_name + if family_name: + user_metadata['family_name'] = family_name + if gender: + user_metadata['gender'] = gender + if location: # get existing location metadata: location_data = user_metadata.get('location') location_data['area_name'] = location - deserialized_person.location = location_data - elif products: - deserialized_person.products = products + user_metadata['location'] = location_data + if products: + user_metadata['products'] = products - edited_metadata = deserialized_person.serialize() + user_metadata = format_user_metadata(metadata=user_metadata, user=user) s_edit_person_metadata = celery.signature( - 'cic_ussd.tasks.metadata.edit_person_metadata', - [blockchain_address, edited_metadata] + 'cic_ussd.tasks.metadata.create_person_metadata', + [blockchain_address, user_metadata] ) s_edit_person_metadata.apply_async(queue='cic-ussd')