Added email message when receiving a notification.py
authorLennard de Rijk <ljvderijk@gmail.com>
Wed, 17 Dec 2008 22:15:21 +0000
changeset 757 b11cee4ab535
parent 756 a0c0b48563cb
child 758 59307ec93f1d
Added email message when receiving a notification.py At the moment this only triggers when the new notification is your first unread message. Also fixed an url bug in the invite notification. Patch by: Lennard de Rijk
app/soc/logic/helper/notifications.py
app/soc/logic/models/notification.py
app/soc/templates/soc/mail/new_notification.html
--- a/app/soc/logic/helper/notifications.py	Wed Dec 17 19:33:03 2008 +0000
+++ b/app/soc/logic/helper/notifications.py	Wed Dec 17 22:15:21 2008 +0000
@@ -28,14 +28,19 @@
 from google.appengine.api import users
 
 from django.template import loader
+from django.utils.encoding import force_unicode
 from django.utils.translation import ugettext_lazy
 
 from soc.logic import mail_dispatcher
 from soc.views.helper import redirects
 
+import soc.views.models as model_view
 import soc.logic.models as model_logic
 
 
+DEF_NEW_NOTIFICATION_MSG = ugettext_lazy(
+  "You have received a new Notification")
+
 DEF_INVITATION_MSG_FMT = ugettext_lazy(
     "Invitation to become a %(role)s for %(group)s")
 
@@ -59,7 +64,7 @@
   request_user_entity = user_logic.getForFields(properties, unique=True)
 
   # create the invitation_url
-  invitation_url = "%(host)s%(index)s" % {
+  invitation_url = "http://%(host)s%(index)s" % {
       'host' : os.environ['HTTP_HOST'], 
       'index': redirects.inviteAcceptedRedirect(entity, None)}
 
@@ -96,6 +101,42 @@
   notification_logic.updateOrCreateFromFields(fields, 
       notification_logic.getKeyFieldsFromDict(fields))
   
+def sendNewNotificationMessage(notification_entity):
+  """Sends an email to a user about a new notification
+  
+    Args:
+      notification_entity: Notification about which the message should be sent    
+  """
+
+  # get user logic
+  user_logic = model_logic.user  
+  
+  # get the current user
+  current_user_entity = user_logic.logic.getForCurrentAccount()
+
+  # create the url to show this notification
+  notification_url = "http://%(host)s%(index)s" % {
+      'host' : os.environ['HTTP_HOST'], 
+      'index': redirects.getPublicRedirect(notification_entity,
+          model_view.notification.view.getParams())}
+
+
+  # TODO(Lennard): Change the sender to the no-reply address
+
+  # create the message contents
+  messageProperties = {
+      'to_name': notification_entity.scope.name,
+      'sender_name': current_user_entity.name,
+      'to': notification_entity.scope.account.email(),
+      'sender': current_user_entity.account.email(),
+      'subject': force_unicode(DEF_NEW_NOTIFICATION_MSG),
+      'notification' : notification_entity,
+      'notification_url' : notification_url
+      }
+  
+  # send out the message using the default new notification template    
+  mail_dispatcher.sendMailFromTemplate('soc/mail/new_notification.html', 
+                                       messageProperties)
   
 def sendWelcomeMessage(user_entity):
   """Sends out a welcome message to a user.
@@ -108,8 +149,7 @@
   user_logic = model_logic.user  
   
   # get the current user
-  properties = {'account': users.get_current_user()}
-  current_user_entity = user_logic.logic.getForFields(properties, unique=True)
+  current_user_entity = user_logic.logic.getForCurrentAccount()
 
   # TODO(Lennard): change the message sender to some sort of no-reply adress 
   # that is probably a setting in sitesettings. (adress must be a developer). 
--- a/app/soc/logic/models/notification.py	Wed Dec 17 19:33:03 2008 +0000
+++ b/app/soc/logic/models/notification.py	Wed Dec 17 22:15:21 2008 +0000
@@ -22,6 +22,9 @@
   ]
 
 
+from google.appengine.ext import db
+
+from soc.logic.helper import notifications
 from soc.logic.models import base
 from soc.logic.models import user as user_logic
 
@@ -37,6 +40,22 @@
     """
     super(Logic, self).__init__(model=soc.models.notification.Notification,
          base_model=None, scope_logic=user_logic)
+    
+  def _onCreate(self, entity):
+    """Sends out a message if there is only one unread notification.
+    """
+    
+    # create a special query on which we can call count
+    query = db.Query(self._model)
+    query.filter('scope =', entity.scope)
+    query.filter('unread = ', True)
+    
+    # count the number of results with a maximum of two
+    unread_count = query.count(2)
+    
+    if unread_count == 1:
+      # there is only one unread notification so send out an email
+      notifications.sendNewNotificationMessage(entity)
 
 
 logic = Logic()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/soc/templates/soc/mail/new_notification.html	Wed Dec 17 22:15:21 2008 +0000
@@ -0,0 +1,20 @@
+{% extends "soc/mail/base.html" %}
+{% comment %}
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+  http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+{% endcomment %}
+<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
+{% block content %}
+You have received a new notification entitled "{{ notification.subject }}". 
+To read, respond to, or delete this notification please click <a href="{{ notification_url }}">here</a>.
+{% endblock %}
+{% block signature %}Greetings, <br />The Melange Team{% endblock %}