-
Notifications
You must be signed in to change notification settings - Fork 0
/
Notifications.py
175 lines (112 loc) · 5.57 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# -*- coding: utf-8 -*-
import os
import jinja2
import logging
from google.appengine.api import mail
from google.appengine.api import users
from DataModels import Notification
#-----------------------------------------------------------------------------
jinja_environment = jinja2.Environment(
loader = jinja2.FileSystemLoader(os.path.dirname(__file__))
)
acceptTemplate = jinja_environment.get_template("templates/Email_Accept.html")
rejectTemplate = jinja_environment.get_template("templates/Email_Reject.html")
downgradeTemplate = jinja_environment.get_template("templates/Email_Downgrade.html")
stopTemplate = jinja_environment.get_template("templates/Email_Stop.html")
pendTemplate = jinja_environment.get_template("templates/Email_Pend.html")
activeTemplate = jinja_environment.get_template("templates/Email_AccountActive.html")
#-----------------------------------------------------------------------------
psinqueSender = "Psinque notifications <noreply@psinque.appspotmail.com>"
def getPrimaryEmail(userProfile):
return userProfile.emails.filter("primary =", True).get().itemValue
def sendNotification(userProfile, subject, html, shorttext = u""):
message = mail.EmailMessage(sender = psinqueSender,
subject = subject)
message.to = getPrimaryEmail(userProfile)
#message.body = html
message.html = html
message.send()
#if shorttext != u"":
#onpageNotification = Notification(parent = user,
#text = shorttext)
#onpageNotification.put()
#-----------------------------------------------------------------------------
# Notifications for the sending user -----------------------------------------
def notifyPendingPsinque(psinque):
receipient = psinque.fromUser
sender = psinque.parent().parent()
sendNotification(receipient,
"You have a new pending psinque request",
pendTemplate.render({
'receipientsName': receipient.displayName,
'friendsName': sender.displayName,
'acceptURL': "http://www.psinque.com/psinques/acceptrequest?key=" + str(psinque.key()) + "&email=true",
'rejectURL': "http://www.psinque.com/psinques/rejectrequest?key=" + str(psinque.key()) + "&email=true",
}))
def notifyStoppedUsingPrivateData(psinque):
receipient = psinque.fromUser
sender = psinque.parent().parent()
sendNotification(receipient,
"%s has stopped using your private data" % sender.displayName,
stopTemplate.render({
'receipientsName': receipient.displayName,
'friendsName': sender.displayName,
}))
# Notifications for the receiving user ---------------------------------------
def notifyDowngradedPsinque(psinque):
receipient = psinque.parent().parent()
sendNotification(receipient,
"Your access to private data has been revoked",
downgradeTemplate.render({
'receipientsName': receipient.displayName,
'friendsName': psinque.displayName,
}))
def notifyAcceptedRequest(psinque):
receipient = psinque.parent().parent()
sendNotification(receipient,
"Your request for private contact data has been accepted",
acceptTemplate.render({
'receipientsName': receipient.displayName,
'friendsName': psinque.displayName,
}))
def notifyRejectedRequest(psinque):
receipient = psinque.parent().parent()
sendNotification(psinque.fromUser,
"Your request for sharing private contact data has been rejected",
rejectTemplate.render({
'receipientsName': receipient.displayName,
'friendsName': psinque.displayName,
}))
# Invitation to join Psinque ------------------------------------------------
def notifyAccountActive(email, recipientsName):
message = mail.EmailMessage(sender = psinqueSender,
subject = "Your Psinque account has been activated")
message.to = email
message.html = activeTemplate.render()
message.send()
def notifyInvitation(email):
message = mail.EmailMessage(sender = psinqueSender,
subject = "Invitation to join Psinque")
message.to = email
message.html = rejectTemplate.render({
'receipientsName': receipient.displayName,
'friendsName': psinque.displayName,
})
message.send()
def inviteToPsinque(email):
existingEmail = UserEmail.all().filter("itemValue =", email).get()
if not existingEmail is None:
userProfile = existingEmail.parent()
if not userProfile.active:
userProfile.active = True
userProfile.put()
notifyAccountActive(email)
return True # profile activated
else:
return False # user already active
else:
invitation = Invitation(email = email)
invitation.put()
notifyInvitation(email)
return True
#-----------------------------------------------------------------------------