app/soc/logic/helper/notifications.py
changeset 635 2f5322ad1f5b
child 638 22ec01fdf8f4
equal deleted inserted replaced
634:ad58da0b78e8 635:2f5322ad1f5b
       
     1 #!/usr/bin/python2.5
       
     2 #
       
     3 # Copyright 2008 the Melange authors.
       
     4 #
       
     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
       
     8 #
       
     9 #   http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    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.
       
    16 
       
    17 """Helper functions for sending out notifications
       
    18 """
       
    19 
       
    20 __authors__ = [
       
    21   '"Lennard de Rijk" <ljvderijk@gmail.com>',
       
    22   ]
       
    23 
       
    24 from google.appengine.api import users
       
    25 
       
    26 from django.utils.translation import ugettext_lazy
       
    27 
       
    28 from soc.logic import mail_dispatcher
       
    29 from soc.logic.models import user as user_logic
       
    30 
       
    31 from soc.views.helper import redirects
       
    32 
       
    33 import os
       
    34 
       
    35 
       
    36 DEF_INVITATION_FMT = ugettext_lazy(
       
    37     "Invitation to become a %(role)s for %(group)s")
       
    38 
       
    39 
       
    40 def sendInviteNotification(entity):
       
    41   """Sends out an invite notification to the user the request is for.
       
    42 
       
    43   Args:
       
    44     entity : A request containing the information needed to create the message
       
    45   """
       
    46 
       
    47   # get the current user
       
    48   properties = {'account': users.get_current_user()}
       
    49   current_user_entity = user_logic.logic.getForFields(properties, unique=True)
       
    50   
       
    51   # get the user the request is for
       
    52   properties = {'link_id': entity.link_id }
       
    53   request_user_entity = user_logic.logic.getForFields(properties, unique=True)
       
    54 
       
    55   # create the invitation_url
       
    56   invitation_url = "%(host)s%(index)s" % {
       
    57       'host' : os.environ['HTTP_HOST'], 
       
    58       'index': redirects.inviteAcceptedRedirect(entity, None)}
       
    59 
       
    60   # get the group entity
       
    61   group_entity = entity.scope
       
    62 
       
    63   messageProperties = {
       
    64       'to_name': request_user_entity.name,
       
    65       'sender_name': current_user_entity.name,
       
    66       'role': entity.role,
       
    67       'group': group_entity.name,
       
    68       'invitation_url': invitation_url,
       
    69       'to': request_user_entity.account.email(),
       
    70       'sender': current_user_entity.account.email(),
       
    71       'subject': DEF_INVITATION_FMT % {
       
    72           'role': entity.role,
       
    73           'group': group_entity.name
       
    74           },
       
    75       }
       
    76 
       
    77   # send out the message using the default invitation template    
       
    78   mail_dispatcher.sendMailFromTemplate('soc/mail/invitation.html', 
       
    79                                        messageProperties)