1
|
|
|
from django.conf import settings |
2
|
|
|
from django.core import mail |
3
|
|
|
from django.db import models |
4
|
|
|
from django.template.loader import get_template |
5
|
|
|
|
6
|
|
|
try: |
7
|
|
|
from django.template.exceptions import TemplateDoesNotExist |
8
|
|
|
except ImportError: |
9
|
|
|
from django.template.base import TemplateDoesNotExist |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class ThreadNotification(models.Model): |
13
|
|
|
""" Class to handle notifications about threads """ |
14
|
|
|
|
15
|
|
|
user = models.ForeignKey(settings.AUTH_USER_MODEL) |
16
|
|
|
thread = models.ForeignKey('simple_forums.Thread') |
17
|
|
|
|
18
|
|
|
def __str__(self): |
19
|
|
|
""" Return a string representation of the instance """ |
20
|
|
|
return 'Notify %s of changes to thread #%d (%s)' % ( |
21
|
|
|
self.user.username, self.thread.id, self.thread) |
22
|
|
|
|
23
|
|
|
def _email_context(self): |
24
|
|
|
""" Get context used for sending emails """ |
25
|
|
|
return {} |
26
|
|
|
|
27
|
|
|
@staticmethod |
28
|
|
|
def _full_template_name(name): |
29
|
|
|
""" Return a full template name """ |
30
|
|
|
return 'simple_forums/notifications/emails/%s' % name |
31
|
|
|
|
32
|
|
|
def load_templates(self): |
33
|
|
|
""" Load templates for notification email """ |
34
|
|
|
plain_temp = get_template( |
35
|
|
|
self._full_template_name('thread_update.txt')) |
36
|
|
|
plain = plain_temp.render(self._email_context()) |
37
|
|
|
|
38
|
|
|
try: |
39
|
|
|
html_temp = get_template( |
40
|
|
|
self._full_template_name('thread_update.html')) |
41
|
|
|
html = html_temp.render(self._email_context()) |
42
|
|
|
except TemplateDoesNotExist: |
43
|
|
|
html = None |
44
|
|
|
|
45
|
|
|
return (plain, html) |
46
|
|
|
|
47
|
|
|
def send_notification(self, message): |
48
|
|
|
""" Notify user that the given message has been posted """ |
49
|
|
|
subject = 'Thread Updated' |
50
|
|
|
message = 'Thread #%d was updated' % self.thread.pk |
51
|
|
|
|
52
|
|
|
mail.send_mail( |
53
|
|
|
subject, message, '[email protected]', |
54
|
|
|
(self.user.email,), html_message=None) |
55
|
|
|
|