Cic notify build

This commit is contained in:
2021-02-07 02:29:24 +00:00
parent 85f06aa445
commit cebdb24ed6
61 changed files with 1650 additions and 27 deletions

View File

@@ -0,0 +1,12 @@
# standard imports
# third-party imports
# local imports
import celery
celery_app = celery.current_app
from .africastalking import send
from .db import persist_notification
from .log import log

View File

@@ -0,0 +1,69 @@
# standard imports
import logging
# third party imports
import celery
import africastalking
# local imports
from cic_notify.error import NotInitializedError, AlreadyInitializedError
logg = logging.getLogger()
celery_app = celery.current_app
class AfricasTalkingNotifier:
initiated = None
sender_id = None
def __init__(self):
if not self.initiated:
raise NotInitializedError()
self.api_client = africastalking.SMS
@staticmethod
def initialize(api_username, api_key, sender_id=None):
"""
:param api_username:
:type api_username:
:param api_key:
:type api_key:
:param sender_id:
:type sender_id:
"""
if AfricasTalkingNotifier.initiated:
raise AlreadyInitializedError()
africastalking.initialize(username=api_username, api_key=api_key)
AfricasTalkingNotifier.sender_id = sender_id
AfricasTalkingNotifier.initiated = True
def send(self, message, recipient):
"""
:param message:
:type message:
:param recipient:
:type recipient:
:return:
:rtype:
"""
if self.sender_id:
response = self.api_client.send(message=message, recipients=[recipient], sender_id=self.sender_id)
logg.debug(f'Africastalking response sender-id {response}')
else:
response = self.api_client.send(message=message, recipients=[recipient])
logg.debug(f'africastalking response no-sender-id {response}')
@celery_app.task
def send(message, recipient):
"""
:param message:
:type message:
:param recipient:
:type recipient:
:return:
:rtype:
"""
africastalking_notifier = AfricasTalkingNotifier()
africastalking_notifier.send(message=message, recipient=recipient)

View File

@@ -0,0 +1,26 @@
# standard imports
# third-party imports
import celery
# local imports
from cic_notify.db.models.notification import Notification
from cic_notify.db.enum import NotificationTransportEnum
celery_app = celery.current_app
@celery_app.task
def persist_notification(recipient, message):
"""
:param recipient:
:type recipient:
:param message:
:type message:
:return:
:rtype:
"""
Notification.create_session()
notification = Notification(transport=NotificationTransportEnum.SMS, recipient=recipient, message=message)
Notification.session.add(notification)
Notification.session.commit()

View File

@@ -0,0 +1,26 @@
# standard imports
import logging
import time
# third-party imports
import celery
celery_app = celery.current_app
logg = celery_app.log.get_default_logger()
local_logg = logging.getLogger(__name__)
@celery_app.task
def log(recipient, message):
"""
:param recipient:
:type recipient:
:param message:
:type message:
:return:
:rtype:
"""
timestamp = time.time()
log_string = f'[{timestamp}] {__name__} message to {recipient}: {message}'
logg.info(log_string)
local_logg.info(log_string)