app/soc/logic/helper/notifications.py
changeset 756 a0c0b48563cb
parent 750 6e8d67d04507
child 757 b11cee4ab535
equal deleted inserted replaced
755:1ed041c0cdc6 756:a0c0b48563cb
    20 __authors__ = [
    20 __authors__ = [
    21   '"Lennard de Rijk" <ljvderijk@gmail.com>',
    21   '"Lennard de Rijk" <ljvderijk@gmail.com>',
    22   ]
    22   ]
    23 
    23 
    24 
    24 
       
    25 import time
    25 import os
    26 import os
    26 
    27 
    27 from google.appengine.api import users
    28 from google.appengine.api import users
    28 
    29 
       
    30 from django.template import loader
    29 from django.utils.translation import ugettext_lazy
    31 from django.utils.translation import ugettext_lazy
    30 
    32 
    31 from soc.logic import mail_dispatcher
    33 from soc.logic import mail_dispatcher
    32 from soc.views.helper import redirects
    34 from soc.views.helper import redirects
    33 
    35 
    45   Args:
    47   Args:
    46     entity : A request containing the information needed to create the message
    48     entity : A request containing the information needed to create the message
    47   """
    49   """
    48   
    50   
    49   # get user logic
    51   # get user logic
    50   user_logic = model_logic.user
    52   user_logic = model_logic.user.logic
    51 
    53 
    52   # get the current user
    54   # get the current user
    53   properties = {'account': users.get_current_user()}
    55   current_user_entity = user_logic.getForCurrentAccount()
    54   current_user_entity = user_logic.logic.getForFields(properties, unique=True)
       
    55   
    56   
    56   # get the user the request is for
    57   # get the user the request is for
    57   properties = {'link_id': entity.link_id }
    58   properties = {'link_id': entity.link_id }
    58   request_user_entity = user_logic.logic.getForFields(properties, unique=True)
    59   request_user_entity = user_logic.getForFields(properties, unique=True)
    59 
    60 
    60   # create the invitation_url
    61   # create the invitation_url
    61   invitation_url = "%(host)s%(index)s" % {
    62   invitation_url = "%(host)s%(index)s" % {
    62       'host' : os.environ['HTTP_HOST'], 
    63       'host' : os.environ['HTTP_HOST'], 
    63       'index': redirects.inviteAcceptedRedirect(entity, None)}
    64       'index': redirects.inviteAcceptedRedirect(entity, None)}
    64 
    65 
    65   # get the group entity
    66   # get the group entity
    66   group_entity = entity.scope
    67   group_entity = entity.scope
    67 
    68   
       
    69   # create the properties for the message
    68   messageProperties = {
    70   messageProperties = {
    69       'to_name': request_user_entity.name,
    71       'to_name': request_user_entity.name,
    70       'sender_name': current_user_entity.name,
    72       'sender_name': current_user_entity.name,
    71       'role': entity.role,
    73       'role': entity.role,
    72       'group': group_entity.name,
    74       'group': group_entity.name,
    73       'invitation_url': invitation_url,
    75       'invitation_url': invitation_url,
    74       'to': request_user_entity.account.email(),
    76       }
    75       'sender': current_user_entity.account.email(),
    77   
    76       'subject': DEF_INVITATION_MSG_FMT % {
    78   # render the message
    77           'role': entity.role,
    79   message = loader.render_to_string('soc/notification/messages/invitation.html', dictionary=messageProperties)
    78           'group': group_entity.name
    80   
       
    81   # create the fields for the notification
       
    82   fields = { 
       
    83       'from_user' : current_user_entity,
       
    84       'subject' : DEF_INVITATION_MSG_FMT % {
       
    85           'role' : entity.role,
       
    86           'group' : group_entity.name 
    79           },
    87           },
    80       }
    88       'message' : message,
    81 
    89       'scope' : request_user_entity,
    82   # send out the message using the default invitation template    
    90       'link_id' :'%i' % (time.time()),
    83   mail_dispatcher.sendMailFromTemplate('soc/mail/invitation.html', 
    91       'scope_path' : request_user_entity.link_id
    84                                        messageProperties)
    92   }
       
    93   
       
    94   # create and put a new notification in the datastore
       
    95   notification_logic = model_logic.notification.logic
       
    96   notification_logic.updateOrCreateFromFields(fields, 
       
    97       notification_logic.getKeyFieldsFromDict(fields))
       
    98   
    85   
    99   
    86 def sendWelcomeMessage(user_entity):
   100 def sendWelcomeMessage(user_entity):
    87   """Sends out a welcome message to a user.
   101   """Sends out a welcome message to a user.
    88 
   102 
    89     Args:
   103     Args: