From 56b3bd751d21f797717e4a7bd9790018c6b67a9e Mon Sep 17 00:00:00 2001 From: Philip Wafula Date: Wed, 23 Jun 2021 08:54:34 +0000 Subject: [PATCH] Philip/custom metadata --- apps/cic-ussd/cic_ussd/metadata/custom.py | 12 +++++++++++ .../cic-ussd/cic_ussd/metadata/preferences.py | 12 +++++++++++ .../cic_ussd/state_machine/logic/user.py | 20 +++++++++++++++++++ .../cic_ussd/tasks/callback_handler.py | 18 ++++++++++++++--- apps/cic-ussd/cic_ussd/tasks/metadata.py | 18 ++++++++++++++++- 5 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 apps/cic-ussd/cic_ussd/metadata/custom.py create mode 100644 apps/cic-ussd/cic_ussd/metadata/preferences.py diff --git a/apps/cic-ussd/cic_ussd/metadata/custom.py b/apps/cic-ussd/cic_ussd/metadata/custom.py new file mode 100644 index 00000000..6ab13d00 --- /dev/null +++ b/apps/cic-ussd/cic_ussd/metadata/custom.py @@ -0,0 +1,12 @@ +# standard imports + +# external imports + +# local imports +from .base import MetadataRequestsHandler + + +class CustomMetadata(MetadataRequestsHandler): + + def __init__(self, identifier: bytes): + super().__init__(cic_type=':cic.custom', identifier=identifier) diff --git a/apps/cic-ussd/cic_ussd/metadata/preferences.py b/apps/cic-ussd/cic_ussd/metadata/preferences.py new file mode 100644 index 00000000..88210d1a --- /dev/null +++ b/apps/cic-ussd/cic_ussd/metadata/preferences.py @@ -0,0 +1,12 @@ +# standard imports + +# external imports + +# local imports +from .base import MetadataRequestsHandler + + +class PreferencesMetadata(MetadataRequestsHandler): + + def __init__(self, identifier: bytes): + super().__init__(cic_type=':cic.preferences', identifier=identifier) 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 d2db5d29..c08325ba 100644 --- a/apps/cic-ussd/cic_ussd/state_machine/logic/user.py +++ b/apps/cic-ussd/cic_ussd/state_machine/logic/user.py @@ -29,6 +29,16 @@ def change_preferred_language_to_en(state_machine_data: Tuple[str, dict, Account Account.session.add(user) Account.session.commit() + preferences_data = { + 'preferred_language': 'en' + } + + s = celery.signature( + 'cic_ussd.tasks.metadata.add_preferences_metadata', + [user.blockchain_address, preferences_data] + ) + s.apply_async(queue='cic-ussd') + def change_preferred_language_to_sw(state_machine_data: Tuple[str, dict, Account]): """This function changes the user's preferred language to swahili. @@ -40,6 +50,16 @@ def change_preferred_language_to_sw(state_machine_data: Tuple[str, dict, Account Account.session.add(user) Account.session.commit() + preferences_data = { + 'preferred_language': 'sw' + } + + s = celery.signature( + 'cic_ussd.tasks.metadata.add_preferences_metadata', + [user.blockchain_address, preferences_data] + ) + s.apply_async(queue='cic-ussd') + def update_account_status_to_active(state_machine_data: Tuple[str, dict, Account]): """This function sets user's account to active. diff --git a/apps/cic-ussd/cic_ussd/tasks/callback_handler.py b/apps/cic-ussd/cic_ussd/tasks/callback_handler.py index ab860b94..aaf70745 100644 --- a/apps/cic-ussd/cic_ussd/tasks/callback_handler.py +++ b/apps/cic-ussd/cic_ussd/tasks/callback_handler.py @@ -53,13 +53,25 @@ def process_account_creation_callback(self, result: str, url: str, status_code: session.add(user) session.commit() session.close() - + queue = self.request.delivery_info.get('routing_key') - s = celery.signature( + + # add phone number metadata lookup + s_phone_pointer = celery.signature( 'cic_ussd.tasks.metadata.add_phone_pointer', [result, phone_number] ) - s.apply_async(queue=queue) + s_phone_pointer.apply_async(queue=queue) + + # add custom metadata tags + custom_metadata = { + "tags": ["ussd", "individual"] + } + s_custom_metadata = celery.signature( + 'cic_ussd.tasks.metadata.add_custom_metadata', + [result, custom_metadata] + ) + s_custom_metadata.apply_async(queue=queue) # expire cache cache.expire(task_id, timedelta(seconds=180)) diff --git a/apps/cic-ussd/cic_ussd/tasks/metadata.py b/apps/cic-ussd/cic_ussd/tasks/metadata.py index fa6caf86..37703a90 100644 --- a/apps/cic-ussd/cic_ussd/tasks/metadata.py +++ b/apps/cic-ussd/cic_ussd/tasks/metadata.py @@ -7,8 +7,10 @@ from hexathon import strip_0x # local imports from cic_ussd.metadata import blockchain_address_to_metadata_pointer +from cic_ussd.metadata.custom import CustomMetadata from cic_ussd.metadata.person import PersonMetadata from cic_ussd.metadata.phone import PhonePointerMetadata +from cic_ussd.metadata.preferences import PreferencesMetadata from cic_ussd.tasks.base import CriticalMetadataTask celery_app = celery.current_app @@ -44,7 +46,7 @@ def create_person_metadata(blockchain_address: str, data: dict): @celery_app.task -def edit_person_metadata(blockchain_address: str, data: bytes): +def edit_person_metadata(blockchain_address: str, data: dict): identifier = blockchain_address_to_metadata_pointer(blockchain_address=blockchain_address) person_metadata_client = PersonMetadata(identifier=identifier) person_metadata_client.edit(data=data) @@ -56,3 +58,17 @@ def add_phone_pointer(self, blockchain_address: str, phone_number: str): stripped_address = strip_0x(blockchain_address) phone_metadata_client = PhonePointerMetadata(identifier=identifier) phone_metadata_client.create(data=stripped_address) + + +@celery_app.task() +def add_custom_metadata(blockchain_address: str, data: dict): + identifier = blockchain_address_to_metadata_pointer(blockchain_address=blockchain_address) + custom_metadata_client = CustomMetadata(identifier=identifier) + custom_metadata_client.create(data=data) + + +@celery_app.task() +def add_preferences_metadata(blockchain_address: str, data: dict): + identifier = blockchain_address_to_metadata_pointer(blockchain_address=blockchain_address) + custom_metadata_client = PreferencesMetadata(identifier=identifier) + custom_metadata_client.create(data=data)