1
+ """Support for templating of notifications."""
2
+
1
3
from django .conf import settings
2
4
from django .template import Template , Context
3
5
from django .template .loader import render_to_string
4
6
from django .db import models
5
7
6
8
from .backends import send_notification
7
- from .constants import INFO , HTML , TEXT
9
+ from . import constants
8
10
9
11
10
12
class Notification (object ):
11
13
14
+ """An unsent notification linked to an object.
15
+
16
+ This class provides an interface to construct notification messages by
17
+ rendering Django templates. The ``Notification`` itself is not expected
18
+ to be persisted by the backends.
19
+
20
+ Call .send() to send the notification.
21
+
22
+ """
23
+
12
24
name = None
13
25
context_object_name = 'object'
14
- level = INFO
26
+ level = constants . INFO
15
27
subject = None
16
28
user = None
17
29
18
- def __init__ (self , object , request , user = None ):
19
- self .object = object
30
+ def __init__ (self , context_object , request , user = None ):
31
+ self .object = context_object
20
32
self .request = request
21
33
self .user = user
22
34
if self .user is None :
@@ -35,22 +47,23 @@ def get_context_data(self):
35
47
)
36
48
}
37
49
38
- def get_template_names (self , backend_name , source_format = HTML ):
50
+ def get_template_names (self , backend_name , source_format = constants . HTML ):
39
51
names = []
40
52
if self .object and isinstance (self .object , models .Model ):
53
+ meta = self .object ._meta # pylint: disable=protected-access
41
54
names .append (
42
55
'{app}/notifications/{name}_{backend}.{source_format}'
43
56
.format (
44
- app = self . object . _meta .app_label ,
45
- name = self .name or self . object . _meta .model_name ,
57
+ app = meta .app_label ,
58
+ name = self .name or meta .model_name ,
46
59
backend = backend_name ,
47
60
source_format = source_format ,
48
61
))
49
62
return names
50
63
else :
51
64
raise AttributeError ()
52
65
53
- def render (self , backend_name , source_format = HTML ):
66
+ def render (self , backend_name , source_format = constants . HTML ):
54
67
return render_to_string (
55
68
template_name = self .get_template_names (
56
69
backend_name = backend_name ,
0 commit comments