Style and pylint fixes in Melange modules.
[Melange.git] / app / soc / logic / helper / notifications.py
blobdd4dfc803d1f6fad95b3185946fae78a49249a3a
1 #!/usr/bin/python2.5
3 # Copyright 2008 the Melange authors.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """Helper functions for sending out notifications.
18 """
20 __authors__ = [
21 '"Lennard de Rijk" <ljvderijk@gmail.com>',
25 import os
26 import time
28 from django.template import loader
29 from django.utils.encoding import force_unicode
30 from django.utils.translation import ugettext
32 # We cannot import soc.logic.models notification nor user here
33 # due to cyclic imports
34 from soc.logic import accounts
35 from soc.logic import dicts
36 from soc.logic import mail_dispatcher
37 from soc.views.helper import redirects
39 import soc.views.models as model_view
40 import soc.logic.models as model_logic
43 DEF_NEW_NOTIFICATION_MSG = ugettext(
44 "You have received a new Notification.")
46 DEF_INVITATION_MSG_FMT = ugettext(
47 "Invitation to become a %(role_verbose)s for %(group)s.")
49 DEF_NEW_GROUP_MSG_FMT = ugettext(
50 "Your %(application_type)s for %(group_name)s has been accepted.")
52 DEF_NEW_REVIEW_SUBJECT_FMT = ugettext(
53 "New %s Review on %s")
55 DEF_WELCOME_MSG_FMT = ugettext("Welcome to %(site_name)s, %(name)s,")
57 DEF_GROUP_INVITE_NOTIFICATION_TEMPLATE = 'soc/notification/messages/' \
58 'invitation.html'
60 DEF_NEW_REVIEW_NOTIFICATION_TEMPLATE = 'soc/notification/messages/' \
61 'new_review.html'
63 DEF_NEW_GROUP_TEMPLATE = 'soc/group/messages/accepted.html'
66 def sendInviteNotification(entity):
67 """Sends out an invite notification to the user the request is for.
69 Args:
70 entity : A request containing the information needed to create the message
71 """
73 # get the user the request is for
74 properties = {'link_id': entity.link_id }
75 to_user = model_logic.user.logic.getForFields(properties, unique=True)
77 invitation_url = "http://%(host)s%(index)s" % {
78 'host' : os.environ['HTTP_HOST'],
79 'index': redirects.getInviteProcessRedirect(entity, None),
82 message_properties = {
83 'role_verbose' : entity.role_verbose,
84 'group': entity.scope.name,
85 'invitation_url': invitation_url,
88 subject = DEF_INVITATION_MSG_FMT % {
89 'role_verbose' : entity.role_verbose,
90 'group' : entity.scope.name
93 template = DEF_GROUP_INVITE_NOTIFICATION_TEMPLATE
95 from_user = model_logic.user.logic.getForCurrentAccount()
97 sendNotification(to_user, from_user, message_properties, subject, template)
100 def sendNewGroupNotification(entity, params):
101 """Sends out an invite notification to the applicant of the group.
103 Args:
104 entity : An accepted group application
107 url = "http://%(host)s%(redirect)s" % {
108 'redirect': redirects.getApplicantRedirect(entity,
109 {'url_name': params['group_url_name']}),
110 'host': os.environ['HTTP_HOST'],
113 message_properties = {
114 'application_type': params['name'],
115 'group_type': params['group_name'],
116 'group_name': entity.name,
117 'url': url,
120 subject = DEF_NEW_GROUP_MSG_FMT % {
121 'application_type': params['name'],
122 'group_name': entity.name,
125 template = DEF_NEW_GROUP_TEMPLATE
127 for to in [entity.applicant, entity.backup_admin]:
128 if not to:
129 continue
131 sendNotification(to, None, message_properties, subject, template)
134 def sendNewReviewNotification(to_user, review, reviewed_name, redirect_url):
135 """Sends out a notification to alert the user of a new Review.
137 Args:
138 to_user: The user who should receive a notification
139 review: The review which triggers this notification
140 reviewed_name: Name of the entity reviewed
141 redirect_url: URL to which the follower should be sent for more information
144 message_properties = {'redirect_url': redirect_url,
145 'reviewer_name': review.author_name(),
146 'reviewed_name': reviewed_name,
149 # determine the subject
150 review_type = 'public' if review.is_public else 'private'
151 subject = DEF_NEW_REVIEW_SUBJECT_FMT % (review_type, reviewed_name)
153 template = DEF_NEW_REVIEW_NOTIFICATION_TEMPLATE
155 # send the notification from the system
156 sendNotification(to_user, None, message_properties, subject, template)
159 def sendNotification(to_user, from_user, message_properties, subject, template):
160 """Sends out a notification to the specified user.
162 Args:
163 to_user : user to which the notification will be send
164 from_user: user from who sends the notifications (None iff sent by site)
165 message_properties : message properties
166 subject : subject of notification email
167 template : template used for generating notification
170 if from_user:
171 sender_name = from_user.name
172 else:
173 site_entity = model_logic.site.logic.getSingleton()
174 sender_name = 'The %s Team' % (site_entity.site_name)
176 new_message_properties = {
177 'sender_name': sender_name,
178 'to_name': to_user.name,
181 message_properties = dicts.merge(message_properties, new_message_properties)
183 message = loader.render_to_string(template, dictionary=message_properties)
185 fields = {
186 'from_user': from_user,
187 'subject': subject,
188 'message': message,
189 'scope': to_user,
190 'link_id': 't%i' % (int(time.time()*100)),
191 'scope_path': to_user.link_id
194 # pylint: disable-msg=W0612
195 import soc.logic.models.notification
196 key_name = model_logic.notification.logic.getKeyNameFromFields(fields)
198 # create and put a new notification in the datastore
199 model_logic.notification.logic.updateOrCreateFromKeyName(fields, key_name)
202 def sendNewNotificationMessage(notification_entity):
203 """Sends an email to a user about a new notification.
205 Args:
206 notification_entity: Notification about which the message should be sent
208 # pylint: disable-msg=W0612
209 import soc.views.models.notification
211 # create the url to show this notification
212 notification_url = "http://%(host)s%(index)s" % {
213 'host' : os.environ['HTTP_HOST'],
214 'index': redirects.getPublicRedirect(notification_entity,
215 model_view.notification.view.getParams())}
217 sender = mail_dispatcher.getDefaultMailSender()
218 site_entity = model_logic.site.logic.getSingleton()
219 site_name = site_entity.site_name
221 # get the default mail sender
222 default_sender = mail_dispatcher.getDefaultMailSender()
224 if not default_sender:
225 # no valid sender found, abort
226 return
227 else:
228 (sender_name, sender) = default_sender
230 to = accounts.denormalizeAccount(notification_entity.scope.account).email()
232 # create the message contents
233 messageProperties = {
234 'to_name': notification_entity.scope.name,
235 'sender_name': sender_name,
236 'to': to,
237 'sender': sender,
238 'site_name': site_name,
239 'subject': force_unicode(DEF_NEW_NOTIFICATION_MSG),
240 'notification' : notification_entity,
241 'notification_url' : notification_url
244 # send out the message using the default new notification template
245 mail_dispatcher.sendMailFromTemplate('soc/mail/new_notification.html',
246 messageProperties)
249 def sendWelcomeMessage(user_entity):
250 """Sends out a welcome message to a user.
252 Args:
253 user_entity: User entity which the message should be send to
256 # get site name
257 site_entity = model_logic.site.logic.getSingleton()
258 site_name = site_entity.site_name
260 # get the default mail sender
261 default_sender = mail_dispatcher.getDefaultMailSender()
263 if not default_sender:
264 # no valid sender found, should not happen but abort anyway
265 return
266 else:
267 sender_name, sender = default_sender
269 to = accounts.denormalizeAccount(user_entity.account).email()
271 # create the message contents
272 messageProperties = {
273 'to_name': user_entity.name,
274 'sender_name': sender_name,
275 'site_name': site_name,
276 'to': to,
277 'sender': sender,
278 'subject': DEF_WELCOME_MSG_FMT % {
279 'site_name': site_name,
280 'name': user_entity.name
284 # send out the message using the default welcome template
285 mail_dispatcher.sendMailFromTemplate('soc/mail/welcome.html',
286 messageProperties)