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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. elif order.guest_email:
  27. self.send_email_messages(order.guest_email, messages)
  28. else:
  29. return
  30. else:
  31. self.dispatch_user_messages(order.user, messages)
  32. # Create order comms event for audit
  33. if event_type:
  34. CommunicationEvent._default_manager.create(order=order, type=event_type)
  35. def dispatch_user_messages(self, user, messages):
  36. """
  37. Send messages to a site user
  38. """
  39. if messages['subject'] and (messages['body'] or messages['html']):
  40. self.send_user_email_messages(user, messages)
  41. if messages['sms']:
  42. self.send_text_message(user, messages['sms'])
  43. # Internal
  44. def send_user_email_messages(self, user, messages):
  45. """
  46. Sends message to the registered user / customer and collects data in database
  47. """
  48. if not user.email:
  49. self.logger.warning("Unable to send email messages as user #%d has no email address", user.id)
  50. return
  51. email = self.send_email_messages(user.email, messages)
  52. # Is user is signed in, record the event for audit
  53. if email and user.is_authenticated():
  54. Email._default_manager.create(user=user,
  55. subject=email.subject,
  56. body_text=email.body,
  57. body_html=messages['html'])
  58. def send_email_messages(self, recipient, messages):
  59. """
  60. Plain email sending to the specified recipient
  61. """
  62. if hasattr(settings, 'OSCAR_FROM_EMAIL'):
  63. from_email = settings.OSCAR_FROM_EMAIL
  64. else:
  65. from_email = None
  66. # Determine whether we are sending a HTML version too
  67. if messages['html']:
  68. email = EmailMultiAlternatives(messages['subject'],
  69. messages['body'],
  70. from_email=from_email,
  71. to=[recipient])
  72. email.attach_alternative(messages['html'], "text/html")
  73. else:
  74. email = EmailMessage(messages['subject'],
  75. messages['body'],
  76. from_email=from_email,
  77. to=[recipient])
  78. self.logger.info("Sending email to %s" % recipient)
  79. email.send()
  80. return email
  81. def send_text_message(self, user, event_type):
  82. raise NotImplementedError