Advertisement
Guest User

Untitled

a guest
Jul 6th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. import logging
  2. from datetime import datetime
  3.  
  4. from django.conf import settings
  5. from django.core.exceptions import ImproperlyConfigured
  6.  
  7. import messente
  8.  
  9. from mailer import send_email
  10.  
  11.  
  12. __all__ = ('MainProvider', )
  13.  
  14.  
  15. class MessenteWrapper:
  16. """Wrapper for Messente service which provide sending SMS.
  17. """
  18. self.error_msg = 'Error while sending SMS: {}'
  19.  
  20. def __init__(self):
  21. username = getattr(settings, 'MESSENTE_USERNAME', None)
  22. password = getattr(settings, 'MESSENTE_PASSWORD', None)
  23.  
  24. if not username or not password:
  25. raise ImproperlyConfigured(
  26. "API credentials for Messente wasn't correctly set. Check "
  27. "`MESSENTE_USERNAME` and `MESSENTE_PASSWORD` settings."
  28. )
  29. self.api = messente.Messente(username=username, password=password)
  30. self.has_errors = False
  31.  
  32. def handle_sms_errors(self, errors):
  33. """Build string with errors which was occured on validation.
  34. """
  35. errors_str = [f'{key}: {msg}' for key, msg in errors.items()]
  36.  
  37. return self.error_msg.format('\n'.join(errors_str))
  38.  
  39. def handle_response_errors(self, response):
  40. """Build string with errors which was occured on sending.
  41. """
  42. if not response.is_replied():
  43. return self.error_msg.format('Error: No Response')
  44.  
  45. elif not response.is_ok():
  46. return self.error_msg.format(response.error_msg)
  47.  
  48. else:
  49. return self.error_msg.format(response.get_raw_text())
  50.  
  51. def send(self, to, message):
  52. """Send SMS to recipient.
  53.  
  54. Args:
  55. to (str) - phone number of recipient
  56. message (str) - message which should be sent
  57. """
  58. self.has_errors = False
  59. sms_data = {'to': to, 'text': message}
  60.  
  61. ok, errors = self.api.sms.validate(sms_data)
  62. if not ok:
  63. self.has_errors = True
  64. return self.handle_sms_errors(errors)
  65.  
  66. response = self.api.sms.send(sms_data)
  67. self.has_errors = True
  68. return self.handle_response_errors(response)
  69.  
  70. sms_id = response.get_sms_id()
  71. log_message = \
  72. f'SMS for {recipient} was successful sended. ID: {sms_id}'
  73.  
  74. return log_message
  75.  
  76.  
  77. # TODO: Implement setting of subject.
  78. class PostmarkWrapper:
  79. """Wrapper for Postmark service for sending emails.
  80. """
  81. def send(self, to, message):
  82. """Send email to recipient.
  83.  
  84. Args:
  85. to (str) - email of recipient
  86. message (str) - email content which should be sent
  87. """
  88. send_email(message, subject='Test Subject', to=[to])
  89.  
  90.  
  91. class LettersProvider:
  92. """API provider for Czech mail service which allow to send letters.
  93. """
  94. pass
  95.  
  96.  
  97. class DaktelaProvider:
  98. """API provider for Daktela call service.
  99. """
  100. pass
  101.  
  102.  
  103. class MainProvider:
  104. """Entry class for external communication wrappers.
  105. """
  106. def __init__(self):
  107. self.sms = MessenteWrapper()
  108. self.email = PostmarkWrapper()
  109.  
  110. def send(send_type, to, message):
  111. """Get related API provider and send communication.
  112. """
  113. provider = getattr(self, send_type.lower(), None)
  114. if not provider:
  115. raise AttributeError(
  116. f'Wrong API provider type: "{provider}"'
  117. )
  118. return provider
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement