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.

abstract_models.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from django.db import models
  2. from django.utils.translation import ugettext_lazy as _
  3. from django.template import Template, Context, TemplateDoesNotExist
  4. from django.template.loader import get_template
  5. from oscar.apps.customer.managers import CommunicationTypeManager
  6. class AbstractEmail(models.Model):
  7. """
  8. This is a record of all emails sent to a customer.
  9. Normally, we only record order-related emails.
  10. """
  11. user = models.ForeignKey('auth.User', related_name='emails')
  12. subject = models.TextField(_('Subject'), max_length=255)
  13. body_text = models.TextField()
  14. body_html = models.TextField(blank=True, null=True)
  15. date_sent = models.DateTimeField(auto_now_add=True)
  16. class Meta:
  17. abstract = True
  18. def __unicode__(self):
  19. return u"Email to %s with subject '%s'" % (self.user.username, self.subject)
  20. class AbstractCommunicationEventType(models.Model):
  21. # Code used for looking up this event programmatically.
  22. # eg. PASSWORD_RESET
  23. code = models.SlugField(max_length=128)
  24. # Name is the friendly description of an event for use in the admin
  25. name = models.CharField(max_length=255)
  26. # We allow communication types to be categorised
  27. ORDER_RELATED = 'Order related'
  28. USER_RELATED = 'User related'
  29. category = models.CharField(max_length=255, default=ORDER_RELATED)
  30. # Template content for emails
  31. email_subject_template = models.CharField(max_length=255, blank=True)
  32. email_body_template = models.TextField(blank=True, null=True)
  33. email_body_html_template = models.TextField(blank=True, null=True, help_text="HTML template")
  34. # Template content for SMS messages
  35. sms_template = models.CharField(max_length=170, blank=True, help_text="SMS template")
  36. date_created = models.DateTimeField(auto_now_add=True)
  37. date_updated = models.DateTimeField(auto_now=True)
  38. objects = CommunicationTypeManager()
  39. # File templates
  40. email_subject_template_file = 'customer/emails/commtype_%s_subject.txt'
  41. email_body_template_file = 'customer/emails/commtype_%s_body.txt'
  42. email_body_html_template_file = 'customer/emails/commtype_%s_body.html'
  43. sms_template_file = 'customer/sms/commtype_%s_body.txt'
  44. class Meta:
  45. abstract = True
  46. verbose_name_plural = _("Communication event types")
  47. def get_messages(self, ctx=None):
  48. """
  49. Return a dict of templates with the context merged in
  50. We look first at the field templates but fail over to
  51. a set of file templates.
  52. """
  53. if ctx is None:
  54. ctx = {}
  55. code = self.code.lower()
  56. # Build a dict of message name to Template instance
  57. templates = {'subject': 'email_subject_template',
  58. 'body': 'email_body_template',
  59. 'html': 'email_body_html_template',
  60. 'sms': 'sms_template'}
  61. for name, attr_name in templates.items():
  62. field = getattr(self, attr_name, None)
  63. if field:
  64. templates[name] = Template(field)
  65. else:
  66. template_name = getattr(self, "%s_file" % attr_name) % code
  67. try:
  68. templates[name] = get_template(template_name)
  69. except TemplateDoesNotExist:
  70. templates[name] = None
  71. messages = {}
  72. for name, template in templates.items():
  73. messages[name] = template.render(Context(ctx)) if template else ''
  74. # Ensure the email subject doesn't contain any newlines
  75. messages['subject'] = messages['subject'].replace("\n", "")
  76. return messages
  77. def __unicode__(self):
  78. return self.name
  79. def is_order_related(self):
  80. return self.category == self.ORDER_RELATED
  81. def is_user_related(self):
  82. return self.category == self.USER_RELATED