Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import logging
- from datetime import datetime
- from django.conf import settings
- from django.core.exceptions import ImproperlyConfigured
- import messente
- from mailer import send_email
- __all__ = ('MainProvider', )
- class MessenteWrapper:
- """Wrapper for Messente service which provide sending SMS.
- """
- self.error_msg = 'Error while sending SMS: {}'
- def __init__(self):
- username = getattr(settings, 'MESSENTE_USERNAME', None)
- password = getattr(settings, 'MESSENTE_PASSWORD', None)
- if not username or not password:
- raise ImproperlyConfigured(
- "API credentials for Messente wasn't correctly set. Check "
- "`MESSENTE_USERNAME` and `MESSENTE_PASSWORD` settings."
- )
- self.api = messente.Messente(username=username, password=password)
- self.has_errors = False
- def handle_sms_errors(self, errors):
- """Build string with errors which was occured on validation.
- """
- errors_str = [f'{key}: {msg}' for key, msg in errors.items()]
- return self.error_msg.format('\n'.join(errors_str))
- def handle_response_errors(self, response):
- """Build string with errors which was occured on sending.
- """
- if not response.is_replied():
- return self.error_msg.format('Error: No Response')
- elif not response.is_ok():
- return self.error_msg.format(response.error_msg)
- else:
- return self.error_msg.format(response.get_raw_text())
- def send(self, to, message):
- """Send SMS to recipient.
- Args:
- to (str) - phone number of recipient
- message (str) - message which should be sent
- """
- self.has_errors = False
- sms_data = {'to': to, 'text': message}
- ok, errors = self.api.sms.validate(sms_data)
- if not ok:
- self.has_errors = True
- return self.handle_sms_errors(errors)
- response = self.api.sms.send(sms_data)
- self.has_errors = True
- return self.handle_response_errors(response)
- sms_id = response.get_sms_id()
- log_message = \
- f'SMS for {recipient} was successful sended. ID: {sms_id}'
- return log_message
- # TODO: Implement setting of subject.
- class PostmarkWrapper:
- """Wrapper for Postmark service for sending emails.
- """
- def send(self, to, message):
- """Send email to recipient.
- Args:
- to (str) - email of recipient
- message (str) - email content which should be sent
- """
- send_email(message, subject='Test Subject', to=[to])
- class LettersProvider:
- """API provider for Czech mail service which allow to send letters.
- """
- pass
- class DaktelaProvider:
- """API provider for Daktela call service.
- """
- pass
- class MainProvider:
- """Entry class for external communication wrappers.
- """
- def __init__(self):
- self.sms = MessenteWrapper()
- self.email = PostmarkWrapper()
- def send(send_type, to, message):
- """Get related API provider and send communication.
- """
- provider = getattr(self, send_type.lower(), None)
- if not provider:
- raise AttributeError(
- f'Wrong API provider type: "{provider}"'
- )
- return provider
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement