app/soc/logic/models/request.py
changeset 635 2f5322ad1f5b
parent 631 3763d36bcebc
child 655 9635cbaa2dcd
equal deleted inserted replaced
634:ad58da0b78e8 635:2f5322ad1f5b
    20 __authors__ = [
    20 __authors__ = [
    21   '"Sverre Rabbelier" <sverer@rabbelier.nl>',
    21   '"Sverre Rabbelier" <sverer@rabbelier.nl>',
    22   '"Lennard de Rijk" <ljvderijk@gmail.com>'
    22   '"Lennard de Rijk" <ljvderijk@gmail.com>'
    23   ]
    23   ]
    24 
    24 
    25 from google.appengine.api import users
    25 import soc.models.request
    26 
    26 
    27 from django.utils.translation import ugettext_lazy
    27 from soc.logic.helper import notifications
    28 
       
    29 from soc.logic import mail_dispatcher
       
    30 from soc.logic.models import base
    28 from soc.logic.models import base
    31 from soc.logic.models import user as user_logic
       
    32 
       
    33 from soc.views.helper import redirects
       
    34 
       
    35 import os
       
    36 
       
    37 import soc.models.request
       
    38 
    29 
    39 
    30 
    40 class Logic(base.Logic):
    31 class Logic(base.Logic):
    41   """Logic methods for the Request model.
    32   """Logic methods for the Request model.
    42   """
    33   """
    43 
       
    44   DEF_INVITATION_FMT = ugettext_lazy(
       
    45       "Invitation to become a %(role)s for %(group)s")
       
    46 
    34 
    47   def __init__(self, model=soc.models.request.Request,
    35   def __init__(self, model=soc.models.request.Request,
    48                base_model=None):
    36                base_model=None):
    49     """Defines the name, key_name and model for this entity.
    37     """Defines the name, key_name and model for this entity.
    50     """
    38     """
    73     """Sends out a message notifying users about the new invite/request.
    61     """Sends out a message notifying users about the new invite/request.
    74     """
    62     """
    75     
    63     
    76     if entity.group_accepted:  
    64     if entity.group_accepted:  
    77       # this is an invite
    65       # this is an invite
    78       self.sendInviteMessage(entity)
    66       notifications.sendInviteNotification(entity)
    79     elif entity.user_accepted:
    67     elif entity.user_accepted:
    80       # this is a request
    68       # this is a request
    81       # TODO(Lennard) Create a new request message
    69       # TODO(Lennard) Create a new request message
    82       pass
    70       pass
    83 
    71 
    84     
       
    85   def sendInviteMessage(self, entity):
       
    86     """Sends out an invite notification to the user the request is for.
       
    87 
       
    88     Args:
       
    89       entity : A request containing the information needed to create the message
       
    90     """
       
    91 
       
    92     # get the current user
       
    93     properties = {'account': users.get_current_user()}
       
    94     current_user_entity = user_logic.logic.getForFields(properties, unique=True)
       
    95 
       
    96     # get the user the request is for
       
    97     properties = {'link_id': entity.link_id }
       
    98     request_user_entity = user_logic.logic.getForFields(properties, unique=True)
       
    99 
       
   100     # create the invitation_url
       
   101     invitation_url = "%(host)s%(index)s" % {
       
   102         'host' : os.environ['HTTP_HOST'], 
       
   103         'index': redirects.inviteAcceptedRedirect(entity, None)}
       
   104 
       
   105     # get the group entity
       
   106     group_entity = entity.scope
       
   107 
       
   108     messageProperties = {
       
   109         'to_name': request_user_entity.name,
       
   110         'sender_name': current_user_entity.name,
       
   111         'role': entity.role,
       
   112         'group': group_entity.name,
       
   113         'invitation_url': invitation_url,
       
   114         'to': request_user_entity.account.email(),
       
   115         'sender': current_user_entity.account.email(),
       
   116         'subject': self.DEF_INVITATION_FMT % {
       
   117             'role': entity.role,
       
   118             'group': group_entity.name
       
   119             },
       
   120         }
       
   121 
       
   122     # send out the message using the default invitation template    
       
   123     mail_dispatcher.sendMailFromTemplate('soc/mail/invitation.html', 
       
   124                                          messageProperties)
       
   125 
       
   126 
    72 
   127 logic = Logic()
    73 logic = Logic()