Added invitation received message upon creating a new invite.
authorLennard de Rijk <ljvderijk@gmail.com>
Sat, 29 Nov 2008 22:36:51 +0000
changeset 618 b2319f2633bc
parent 617 9cc42981d40a
child 619 7b61da3d4306
Added invitation received message upon creating a new invite. Moved the inviteAcceptedRedirect into the logic code. Added a TODO for further user notification upon creating a request. Patch by: Lennard de Rijk
app/soc/logic/models/request.py
app/soc/views/models/request.py
--- a/app/soc/logic/models/request.py	Sat Nov 29 21:58:34 2008 +0000
+++ b/app/soc/logic/models/request.py	Sat Nov 29 22:36:51 2008 +0000
@@ -19,10 +19,16 @@
 
 __authors__ = [
   '"Sverre Rabbelier" <sverer@rabbelier.nl>',
+  '"Lennard de Rijk" <ljvderijk@gmail.com>'
   ]
 
+from google.appengine.api import users
 
+from soc.logic import mail_dispatcher
 from soc.logic.models import base
+from soc.logic.models import user as user_logic
+
+import os
 
 import soc.models.request
 
@@ -55,6 +61,65 @@
     """
 
     return ['role', 'scope_path', 'link_id']
+  
+  def _onCreate(self, entity):
+    """Sends out a message notifying users about the new invite/request.
+    """
+    
+    if entity.group_accepted:  
+      # this is an invite
+      self.sendInviteMessage(entity)
+    elif entity.user_accepted:
+      # this is a request
+      # TODO(Lennard) Create a new request message
+      pass
 
+    
+  def sendInviteMessage(self, entity):
+    """Sends out an invite notification to the user the request is for.
+    
+    Args:
+      entity : A request containing the information needed to create the message
+    
+    """
+    # get the current user
+    properties = {'account': users.get_current_user()}
+    current_user_entity = user_logic.logic.getForFields(properties, unique=True)
+    
+    # get the user the request is for
+    properties = {'link_id': entity.link_id }
+    request_user_entity = user_logic.logic.getForFields(properties, unique=True)
+    
+    # create the invitation_url
+    invitation_url = "%(host)s%(index)s" % {
+                          'host' : os.environ['HTTP_HOST'], 
+                          'index': self.inviteAcceptedRedirect(entity, None)
+                          }
+    
+    # get the group entity
+    group_entity = entity.scope
+    
+    messageProperties = {
+                      'to_name': request_user_entity.name,
+                      'sender_name': current_user_entity.name,
+                      'role': entity.role,
+                      'group': group_entity.name,
+                      'invitation_url': invitation_url,
+                      'to': request_user_entity.account.email(),
+                      'sender': current_user_entity.account.email(),
+                      'subject': "Invitation to become a %(role)s for %(group)s"
+                         %{'role': entity.role, 'group': group_entity.name},
+                      }
+    
+    # send out the message using the default invitation template    
+    mail_dispatcher.sendMailFromTemplate('soc/mail/invitation.html', 
+                                         messageProperties)
+    
+  def inviteAcceptedRedirect(self, entity, _):
+    """Returns the redirect for accepting an invite
+    """
+
+    return '/%s/create/%s/%s' % (
+        entity.role, entity.scope_path, entity.link_id)
 
 logic = Logic()
--- a/app/soc/views/models/request.py	Sat Nov 29 21:58:34 2008 +0000
+++ b/app/soc/views/models/request.py	Sat Nov 29 22:36:51 2008 +0000
@@ -155,7 +155,7 @@
               'group_accepted' : True}
     
     uh_params = params.copy()
-    uh_params['list_action'] = (self.inviteAcceptedRedirect, None)
+    uh_params['list_action'] = (self._logic.inviteAcceptedRedirect, None)
     uh_params['list_description'] = ugettext_lazy(
         "An overview of your unhandled requests")
 
@@ -180,13 +180,6 @@
     # call the _list method from base to display the list
     return self._list(request, params, contents, page_name)
 
-  def inviteAcceptedRedirect(self, entity, _):
-    """Returns the redirect for accepting a request
-    """
-
-    return '/%s/create/%s/%s' % (
-        entity.role, entity.scope_path, entity.link_id)
-
   def _editSeed(self, request, seed):
     """See base.View._editGet().
     """