您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

utils.py 3.6KB

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