Cic notify build
This commit is contained in:
12
apps/cic-notify/cic_notify/tasks/sms/__init__.py
Normal file
12
apps/cic-notify/cic_notify/tasks/sms/__init__.py
Normal 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
|
||||
69
apps/cic-notify/cic_notify/tasks/sms/africastalking.py
Normal file
69
apps/cic-notify/cic_notify/tasks/sms/africastalking.py
Normal 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)
|
||||
26
apps/cic-notify/cic_notify/tasks/sms/db.py
Normal file
26
apps/cic-notify/cic_notify/tasks/sms/db.py
Normal 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()
|
||||
26
apps/cic-notify/cic_notify/tasks/sms/log.py
Normal file
26
apps/cic-notify/cic_notify/tasks/sms/log.py
Normal 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)
|
||||
Reference in New Issue
Block a user