Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

abstract_models.py 4.5KB

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