2021-03-04 17:47:13 +01:00
|
|
|
# standard imports
|
|
|
|
import logging
|
|
|
|
|
2021-04-14 11:00:10 +02:00
|
|
|
# third-party imports
|
2021-03-04 17:47:13 +01:00
|
|
|
import celery
|
2021-04-09 15:00:15 +02:00
|
|
|
from hexathon import strip_0x
|
2021-03-04 17:47:13 +01:00
|
|
|
|
|
|
|
# local imports
|
|
|
|
from cic_ussd.metadata import blockchain_address_to_metadata_pointer
|
2021-06-23 10:54:34 +02:00
|
|
|
from cic_ussd.metadata.custom import CustomMetadata
|
2021-04-14 11:00:10 +02:00
|
|
|
from cic_ussd.metadata.person import PersonMetadata
|
2021-04-09 15:00:15 +02:00
|
|
|
from cic_ussd.metadata.phone import PhonePointerMetadata
|
2021-06-23 10:54:34 +02:00
|
|
|
from cic_ussd.metadata.preferences import PreferencesMetadata
|
2021-04-09 15:00:15 +02:00
|
|
|
from cic_ussd.tasks.base import CriticalMetadataTask
|
2021-03-04 17:47:13 +01:00
|
|
|
|
|
|
|
celery_app = celery.current_app
|
2021-04-09 15:00:15 +02:00
|
|
|
logg = logging.getLogger().getChild(__name__)
|
2021-03-04 17:47:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
@celery_app.task
|
2021-04-14 11:00:10 +02:00
|
|
|
def query_person_metadata(blockchain_address: str):
|
2021-03-04 17:47:13 +01:00
|
|
|
"""
|
|
|
|
:param blockchain_address:
|
|
|
|
:type blockchain_address:
|
|
|
|
:return:
|
|
|
|
:rtype:
|
|
|
|
"""
|
|
|
|
identifier = blockchain_address_to_metadata_pointer(blockchain_address=blockchain_address)
|
2021-07-20 18:18:27 +02:00
|
|
|
logg.debug(f'Retrieving person metadata for address: {blockchain_address}.')
|
2021-04-14 11:00:10 +02:00
|
|
|
person_metadata_client = PersonMetadata(identifier=identifier)
|
|
|
|
person_metadata_client.query()
|
2021-03-04 17:47:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
@celery_app.task
|
2021-04-14 11:00:10 +02:00
|
|
|
def create_person_metadata(blockchain_address: str, data: dict):
|
2021-03-04 17:47:13 +01:00
|
|
|
"""
|
|
|
|
:param blockchain_address:
|
|
|
|
:type blockchain_address:
|
|
|
|
:param data:
|
|
|
|
:type data:
|
|
|
|
:return:
|
|
|
|
:rtype:
|
|
|
|
"""
|
|
|
|
identifier = blockchain_address_to_metadata_pointer(blockchain_address=blockchain_address)
|
2021-04-14 11:00:10 +02:00
|
|
|
person_metadata_client = PersonMetadata(identifier=identifier)
|
|
|
|
person_metadata_client.create(data=data)
|
2021-03-04 17:47:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
@celery_app.task
|
2021-06-23 10:54:34 +02:00
|
|
|
def edit_person_metadata(blockchain_address: str, data: dict):
|
2021-03-04 17:47:13 +01:00
|
|
|
identifier = blockchain_address_to_metadata_pointer(blockchain_address=blockchain_address)
|
2021-04-14 11:00:10 +02:00
|
|
|
person_metadata_client = PersonMetadata(identifier=identifier)
|
|
|
|
person_metadata_client.edit(data=data)
|
2021-04-09 15:00:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
@celery_app.task(bind=True, base=CriticalMetadataTask)
|
2021-04-14 11:00:10 +02:00
|
|
|
def add_phone_pointer(self, blockchain_address: str, phone_number: str):
|
|
|
|
identifier = phone_number.encode('utf-8')
|
2021-04-09 15:00:15 +02:00
|
|
|
stripped_address = strip_0x(blockchain_address)
|
2021-04-14 11:00:10 +02:00
|
|
|
phone_metadata_client = PhonePointerMetadata(identifier=identifier)
|
2021-04-09 15:00:15 +02:00
|
|
|
phone_metadata_client.create(data=stripped_address)
|
2021-06-23 10:54:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
@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)
|
2021-07-20 18:18:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
@celery_app.task()
|
|
|
|
def query_preferences_metadata(blockchain_address: str):
|
|
|
|
"""This method retrieves preferences metadata based on an account's blockchain address.
|
|
|
|
:param blockchain_address: Blockchain address of an account.
|
|
|
|
:type blockchain_address: str | Ox-hex
|
|
|
|
"""
|
|
|
|
identifier = blockchain_address_to_metadata_pointer(blockchain_address=blockchain_address)
|
|
|
|
logg.debug(f'Retrieving preferences metadata for address: {blockchain_address}.')
|
|
|
|
person_metadata_client = PreferencesMetadata(identifier=identifier)
|
|
|
|
return person_metadata_client.query()
|