Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

abstract_models.py 4.0KB

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