forked from openpolis/op_associazione
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notifications.py
118 lines (95 loc) · 4.47 KB
/
notifications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# -*- coding: utf-8 -*-
import datetime
from django.core.mail import EmailMultiAlternatives, mail_managers
from django.template.loader import get_template
from django.template import Context
from django.core.urlresolvers import reverse
from django.contrib.sites.models import Site
from django.conf import settings
def send_mail(subject, txt_content, from_address, to_addresses, html_content=None):
"""send a multipart email according to arguments passed"""
if not isinstance(to_addresses, (list, tuple)):
to_addresses = [to_addresses]
msg = EmailMultiAlternatives(subject, txt_content, from_address, to_addresses)
if html_content is not None:
msg.attach_alternative(html_content, "text/html")
msg.send()
def send_checksubscriptions_report(expiring, expired):
d = Context({ 'current_site': Site.objects.get(id=settings.SITE_ID),
'expiring': expiring, 'expired': expired, 'expiring_date': datetime.date.today() + datetime.timedelta(days=15) })
# send notification to managers
plaintext = get_template('email/checksubscriptions_report.txt')
mail_managers('[openpolis] report controllo iscrizioni', plaintext.render(d))
def send_expiring_warning_email(membership):
d = Context({ 'current_site': Site.objects.get(id=settings.SITE_ID),
'associate': membership.associate,
'expire_at': membership.expire_at.strftime("%d/%m/%Y"),
'renewal_url': reverse('subscribe-renewal', args=[membership.associate.hash_key]) })
plaintext = get_template('email/expiring_warning_email.txt')
htmly = get_template('email/expiring_warning_email.html')
send_mail(
'[openpolis] Tua iscrizione in scadenza il %s' % (membership.expire_at.strftime("%d/%m/%Y")),
plaintext.render(d),
settings.SERVER_EMAIL,
membership.associate.email,
html_content=htmly.render(d)
)
def send_expired_email(membership):
d = Context({ 'current_site': Site.objects.get(id=settings.SITE_ID),
'associate': membership.associate,
'renewal_url': reverse('subscribe-renewal', args=[membership.associate.hash_key]) })
plaintext = get_template('email/expired_email.txt')
htmly = get_template('email/expired_email.html')
send_mail(
'[openpolis] Tua iscrizione scaduta!',
plaintext.render(d),
settings.SERVER_EMAIL,
membership.associate.email,
html_content=htmly.render(d)
)
def send_renewal_verification_email(associate):
d = Context({ 'current_site': Site.objects.get(id=settings.SITE_ID),
'associate': associate,
'renewal_url': reverse('subscribe-renewal', args=[associate.hash_key]) })
plaintext = get_template('email/renewal_verification_email.txt')
htmly = get_template('email/renewal_verification_email.html')
send_mail(
'[openpolis] Resta qui',
plaintext.render(d),
settings.SERVER_EMAIL,
associate.email,
html_content=htmly.render(d)
)
def subscription_received(membership, request_type):
d = Context({ 'current_site': Site.objects.get(id=settings.SITE_ID),
'membership': membership, 'request_type': request_type })
# send mail to requiring associate
plaintext = get_template('email/your_subscription_received.txt')
htmly = get_template('email/your_subscription_received.html')
if request_type == 'renew':
subject = '[openpolis] Ancora insieme'
else:
subject = '[openpolis] Benvenuto!'
send_mail(
subject,
plaintext.render(d),
settings.SERVER_EMAIL,
membership.associate.email,
html_content=htmly.render(d)
)
# send notification to managers
plaintext = get_template('email/new_subscription_received.txt')
mail_managers('[openpolis] nuova iscrizione ricevuta', plaintext.render(d))
def send_subscription_activated_email(membership):
d = Context({ 'current_site': Site.objects.get(id=settings.SITE_ID),
'membership': membership })
# send mail to requiring associate
plaintext = get_template('email/your_subscription_activated.txt')
htmly = get_template('email/your_subscription_activated.html')
send_mail(
'[openpolis] Attivazione iscrizione',
plaintext.render(d),
settings.SERVER_EMAIL,
membership.associate.email,
html_content=htmly.render(d)
)