You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import logging
  2. from django.core.mail import EmailMessage, EmailMultiAlternatives
  3. from django.conf import settings
  4. from oscar.core.loading import import_module
  5. import_module('order.models', ['CommunicationEvent',], locals())
  6. import_module('customer.models', ['Email'], locals())
  7. class Dispatcher(object):
  8. def __init__(self, logger=None):
  9. if not logger:
  10. logger = logging.getLogger(__name__)
  11. self.logger = logger
  12. # Public API methods
  13. def dispatch_direct_messages(self, recipient, messages):
  14. """
  15. Dispatch one-off messages to explicitly specified recipient(s).
  16. """
  17. if messages['subject'] and messages['body']:
  18. self.send_email_messages(recipient, messages)
  19. def dispatch_order_messages(self, order, messages, event_type, **kwargs):
  20. """
  21. Dispatch order-related messages to the customer
  22. """
  23. if order.is_anonymous:
  24. if 'email_address' in kwargs:
  25. self.send_email_messages(kwargs['email_address'], messages)
  26. else:
  27. return
  28. else:
  29. self.dispatch_user_messages(order.user, messages)
  30. # Create order comms event for audit
  31. if event_type:
  32. CommunicationEvent._default_manager.create(order=order, type=event_type)
  33. def dispatch_user_messages(self, user, messages):
  34. """
  35. Send messages to a site user
  36. """
  37. if messages['subject'] and (messages['body'] or messages['html']):
  38. self.send_user_email_messages(user, messages)
  39. if messages['sms']:
  40. self.send_text_message(user, messages['sms'])
  41. # Internal
  42. def send_user_email_messages(self, user, messages):
  43. """
  44. Sends message to the registered user / customer and collects data in database
  45. """
  46. if not user.email:
  47. self.logger.warning("Unable to send email messages as user #%d has no email address", user.id)
  48. return
  49. email = self.send_email_messages(user.email, messages)
  50. # Is user is signed in, record the event for audit
  51. if email and user.is_authenticated():
  52. Email._default_manager.create(user=user,
  53. subject=email.subject,
  54. body_text=email.body,
  55. body_html=messages['html'])
  56. def send_email_messages(self, recipient, messages):
  57. """
  58. Plain email sending to the specified recipient
  59. """
  60. if hasattr(settings, 'OSCAR_FROM_EMAIL'):
  61. from_email = settings.OSCAR_FROM_EMAIL
  62. else:
  63. from_email = None
  64. # Determine whether we are sending a HTML version too
  65. if messages['html']:
  66. email = EmailMultiAlternatives(messages['subject'],
  67. messages['body'],
  68. from_email=from_email,
  69. to=[recipient])
  70. email.attach_alternative(messages['html'], "text/html")
  71. else:
  72. email = EmailMessage(messages['subject'],
  73. messages['body'],
  74. from_email=from_email,
  75. to=[recipient])
  76. self.logger.info("Sending email to %s" % recipient)
  77. email.send()
  78. return email
  79. def send_text_message(self, user, event_type):
  80. raise NotImplementedError