app/soc/logic/models/request.py
changeset 618 b2319f2633bc
parent 562 1bf2beedda03
child 619 7b61da3d4306
equal deleted inserted replaced
617:9cc42981d40a 618:b2319f2633bc
    17 """Host (Model) query functions.
    17 """Host (Model) query functions.
    18 """
    18 """
    19 
    19 
    20 __authors__ = [
    20 __authors__ = [
    21   '"Sverre Rabbelier" <sverer@rabbelier.nl>',
    21   '"Sverre Rabbelier" <sverer@rabbelier.nl>',
       
    22   '"Lennard de Rijk" <ljvderijk@gmail.com>'
    22   ]
    23   ]
    23 
    24 
       
    25 from google.appengine.api import users
    24 
    26 
       
    27 from soc.logic import mail_dispatcher
    25 from soc.logic.models import base
    28 from soc.logic.models import base
       
    29 from soc.logic.models import user as user_logic
       
    30 
       
    31 import os
    26 
    32 
    27 import soc.models.request
    33 import soc.models.request
    28 
    34 
    29 
    35 
    30 class Logic(base.Logic):
    36 class Logic(base.Logic):
    53   def getKeyFieldNames(self):
    59   def getKeyFieldNames(self):
    54     """See base.Logic.getKeyFieldNames.
    60     """See base.Logic.getKeyFieldNames.
    55     """
    61     """
    56 
    62 
    57     return ['role', 'scope_path', 'link_id']
    63     return ['role', 'scope_path', 'link_id']
       
    64   
       
    65   def _onCreate(self, entity):
       
    66     """Sends out a message notifying users about the new invite/request.
       
    67     """
       
    68     
       
    69     if entity.group_accepted:  
       
    70       # this is an invite
       
    71       self.sendInviteMessage(entity)
       
    72     elif entity.user_accepted:
       
    73       # this is a request
       
    74       # TODO(Lennard) Create a new request message
       
    75       pass
    58 
    76 
       
    77     
       
    78   def sendInviteMessage(self, entity):
       
    79     """Sends out an invite notification to the user the request is for.
       
    80     
       
    81     Args:
       
    82       entity : A request containing the information needed to create the message
       
    83     
       
    84     """
       
    85     # get the current user
       
    86     properties = {'account': users.get_current_user()}
       
    87     current_user_entity = user_logic.logic.getForFields(properties, unique=True)
       
    88     
       
    89     # get the user the request is for
       
    90     properties = {'link_id': entity.link_id }
       
    91     request_user_entity = user_logic.logic.getForFields(properties, unique=True)
       
    92     
       
    93     # create the invitation_url
       
    94     invitation_url = "%(host)s%(index)s" % {
       
    95                           'host' : os.environ['HTTP_HOST'], 
       
    96                           'index': self.inviteAcceptedRedirect(entity, None)
       
    97                           }
       
    98     
       
    99     # get the group entity
       
   100     group_entity = entity.scope
       
   101     
       
   102     messageProperties = {
       
   103                       'to_name': request_user_entity.name,
       
   104                       'sender_name': current_user_entity.name,
       
   105                       'role': entity.role,
       
   106                       'group': group_entity.name,
       
   107                       'invitation_url': invitation_url,
       
   108                       'to': request_user_entity.account.email(),
       
   109                       'sender': current_user_entity.account.email(),
       
   110                       'subject': "Invitation to become a %(role)s for %(group)s"
       
   111                          %{'role': entity.role, 'group': group_entity.name},
       
   112                       }
       
   113     
       
   114     # send out the message using the default invitation template    
       
   115     mail_dispatcher.sendMailFromTemplate('soc/mail/invitation.html', 
       
   116                                          messageProperties)
       
   117     
       
   118   def inviteAcceptedRedirect(self, entity, _):
       
   119     """Returns the redirect for accepting an invite
       
   120     """
       
   121 
       
   122     return '/%s/create/%s/%s' % (
       
   123         entity.role, entity.scope_path, entity.link_id)
    59 
   124 
    60 logic = Logic()
   125 logic = Logic()