Explorar el Código

Rework email sending framework to be clearer

More comments added.
master
David Winterbottom hace 13 años
padre
commit
d807577eb6

+ 2
- 1
oscar/apps/checkout/mixins.py Ver fichero

@@ -256,7 +256,8 @@ class OrderPlacementMixin(CheckoutSessionMixin):
256 256
         try:
257 257
             event_type = CommunicationEventType.objects.get(code=code)
258 258
         except CommunicationEventType.DoesNotExist:
259
-            # No event in database, attempt to find templates for this type
259
+            # No event-type in database, attempt to find templates for this type
260
+            # and render them immediately to get the messages
260 261
             messages = CommunicationEventType.objects.get_and_render(code, ctx)
261 262
             event_type = None
262 263
         else:

+ 25
- 24
oscar/apps/customer/abstract_models.py Ver fichero

@@ -29,39 +29,39 @@ class AbstractEmail(models.Model):
29 29
 
30 30
 
31 31
 class AbstractCommunicationEventType(models.Model):
32
-    
32
+
33 33
     # Code used for looking up this event programmatically.
34 34
     # eg. PASSWORD_RESET
35 35
     code = models.SlugField(_('Code'), max_length=128)
36
-    
36
+
37 37
     # Name is the friendly description of an event for use in the admin
38 38
     name = models.CharField(_('Name'), max_length=255)
39
-    
39
+
40 40
     # We allow communication types to be categorised
41 41
     ORDER_RELATED = _('Order related')
42 42
     USER_RELATED = _('User related')
43 43
     category = models.CharField(_('Category'), max_length=255, default=ORDER_RELATED)
44
-    
44
+
45 45
     # Template content for emails
46 46
     email_subject_template = models.CharField(_('Email Subject Template'), max_length=255, blank=True)
47 47
     email_body_template = models.TextField(_('Email Body Template'), blank=True, null=True)
48 48
     email_body_html_template = models.TextField(_('Email Body HTML Temlate'), blank=True, null=True,
49 49
         help_text=_("HTML template"))
50
-    
50
+
51 51
     # Template content for SMS messages
52 52
     sms_template = models.CharField(_('SMS Template'), max_length=170, blank=True, help_text=_("SMS template"))
53
-    
53
+
54 54
     date_created = models.DateTimeField(auto_now_add=True)
55 55
     date_updated = models.DateTimeField(auto_now=True)
56
-    
56
+
57 57
     objects = CommunicationTypeManager()
58
-    
58
+
59 59
     # File templates
60 60
     email_subject_template_file = 'customer/emails/commtype_%s_subject.txt'
61 61
     email_body_template_file = 'customer/emails/commtype_%s_body.txt'
62 62
     email_body_html_template_file = 'customer/emails/commtype_%s_body.html'
63 63
     sms_template_file = 'customer/sms/commtype_%s_body.txt'
64
-    
64
+
65 65
     class Meta:
66 66
         abstract = True
67 67
         verbose_name = _("Communication Event Type")
@@ -71,16 +71,11 @@ class AbstractCommunicationEventType(models.Model):
71 71
         """
72 72
         Return a dict of templates with the context merged in
73 73
 
74
-        We look first at the field templates but fail over to 
75
-        a set of file templates.  
74
+        We look first at the field templates but fail over to
75
+        a set of file templates that follow a conventional path.
76 76
         """
77
-        if ctx is None:
78
-            ctx = {}
79
-
80
-        # Pass base URL for serving images within HTML emails
81
-        ctx['static_base_url'] = getattr(settings, 'OSCAR_STATIC_BASE_URL', None)
82
-
83 77
         code = self.code.lower()
78
+
84 79
         # Build a dict of message name to Template instance
85 80
         templates = {'subject': 'email_subject_template',
86 81
                      'body': 'email_body_template',
@@ -89,14 +84,21 @@ class AbstractCommunicationEventType(models.Model):
89 84
         for name, attr_name in templates.items():
90 85
             field = getattr(self, attr_name, None)
91 86
             if field:
87
+                # Template content is in a model field
92 88
                 templates[name] = Template(field)
93 89
             else:
90
+                # Model field is empty - look for a file template
94 91
                 template_name = getattr(self, "%s_file" % attr_name) % code
95 92
                 try:
96 93
                     templates[name] = get_template(template_name)
97 94
                 except TemplateDoesNotExist:
98 95
                     templates[name] = None
99
-        
96
+
97
+        # Pass base URL for serving images within HTML emails
98
+        if ctx is None:
99
+            ctx = {}
100
+        ctx['static_base_url'] = getattr(settings, 'OSCAR_STATIC_BASE_URL', None)
101
+
100 102
         messages = {}
101 103
         for name, template in templates.items():
102 104
             messages[name] = template.render(Context(ctx)) if template else ''
@@ -105,13 +107,12 @@ class AbstractCommunicationEventType(models.Model):
105 107
         messages['subject'] = messages['subject'].replace("\n", "")
106 108
 
107 109
         return messages
108
-        
110
+
109 111
     def __unicode__(self):
110
-        return self.name   
111
-    
112
+        return self.name
113
+
112 114
     def is_order_related(self):
113 115
         return self.category == self.ORDER_RELATED
114
-    
116
+
115 117
     def is_user_related(self):
116
-        return self.category == self.USER_RELATED 
117
-    
118
+        return self.category == self.USER_RELATED

+ 6
- 2
oscar/apps/customer/managers.py Ver fichero

@@ -2,10 +2,14 @@ from django.db import models
2 2
 
3 3
 
4 4
 class CommunicationTypeManager(models.Manager):
5
-    
5
+
6 6
     def get_and_render(self, code, context):
7 7
         """
8
-        Return a dictionary of rendered messages, ready for sending
8
+        Return a dictionary of rendered messages, ready for sending.
9
+
10
+        This method wraps around whether an instance of this event-type exists
11
+        in the database.  If not, then an instance is created on the fly and
12
+        used to generate the message contents.
9 13
         """
10 14
         try:
11 15
             commtype = self.get(code=code)

Loading…
Cancelar
Guardar