app/soc/logic/helper/notifications.py
changeset 820 56eec9c8bb47
parent 818 ddd102e82107
child 821 5afe16b2e86b
equal deleted inserted replaced
819:08a79c3e8817 820:56eec9c8bb47
    29 
    29 
    30 from django.template import loader
    30 from django.template import loader
    31 from django.utils.encoding import force_unicode
    31 from django.utils.encoding import force_unicode
    32 from django.utils.translation import ugettext_lazy
    32 from django.utils.translation import ugettext_lazy
    33 
    33 
       
    34 # We cannot import soc.logic.models notification nor user here
       
    35 # due to cyclic imports
       
    36 from soc.logic import dicts
    34 from soc.logic import mail_dispatcher
    37 from soc.logic import mail_dispatcher
    35 from soc.views.helper import redirects
    38 from soc.views.helper import redirects
    36 
    39 
    37 import soc.views.models as model_view
    40 import soc.views.models as model_view
    38 import soc.logic.models as model_logic
    41 import soc.logic.models as model_logic
    44 DEF_INVITATION_MSG_FMT = ugettext_lazy(
    47 DEF_INVITATION_MSG_FMT = ugettext_lazy(
    45     "Invitation to become a %(role)s for %(group)s.")
    48     "Invitation to become a %(role)s for %(group)s.")
    46 
    49 
    47 DEF_WELCOME_MSG_FMT = ugettext_lazy("Welcome to Melange %(name)s,")
    50 DEF_WELCOME_MSG_FMT = ugettext_lazy("Welcome to Melange %(name)s,")
    48 
    51 
       
    52 DEF_GROUP_INVITE_NOTIFICATION_TEMPLATE = 'soc/notification/messages/invitation.html'
       
    53 
       
    54 
    49 
    55 
    50 def sendInviteNotification(entity):
    56 def sendInviteNotification(entity):
    51   """Sends out an invite notification to the user the request is for.
    57   """Sends out an invite notification to the user the request is for.
    52 
    58 
    53   Args:
    59   Args:
    54     entity : A request containing the information needed to create the message
    60     entity : A request containing the information needed to create the message
    55   """
    61   """
    56 
    62 
    57   # get user logic
       
    58   user_logic = model_logic.user.logic
       
    59 
       
    60   # get the current user
       
    61   current_user_entity = user_logic.getForCurrentAccount()
       
    62 
       
    63   # get the user the request is for
    63   # get the user the request is for
    64   properties = {'link_id': entity.link_id }
    64   properties = {'link_id': entity.link_id }
    65   request_user_entity = user_logic.getForFields(properties, unique=True)
    65   to_user = model_logic.user.logic.getForFields(properties, unique=True)
    66 
    66 
    67   # create the invitation_url
       
    68   invitation_url = "http://%(host)s%(index)s" % {
    67   invitation_url = "http://%(host)s%(index)s" % {
    69       'host' : os.environ['HTTP_HOST'],
    68       'host' : os.environ['HTTP_HOST'],
    70       'index': redirects.inviteAcceptedRedirect(entity, None)}
    69       'index': redirects.inviteAcceptedRedirect(entity, None),
       
    70       }
    71 
    71 
    72   # get the group entity
    72   message_properties = {
    73   group_entity = entity.scope
       
    74 
       
    75   # create the properties for the message
       
    76   messageProperties = {
       
    77       'to_name': request_user_entity.name,
       
    78       'sender_name': current_user_entity.name,
       
    79       'role': entity.role,
    73       'role': entity.role,
    80       'group': group_entity.name,
    74       'group': entity.scope.name,
    81       'invitation_url': invitation_url,
    75       'invitation_url': invitation_url,
    82       }
    76       }
    83 
    77 
    84   # render the message
    78   subject = DEF_INVITATION_MSG_FMT % {
    85   message = loader.render_to_string(
    79       'role' : entity.role,
    86       'soc/notification/messages/invitation.html',
    80       'group' : entity.scope.name
    87       dictionary=messageProperties)
    81       }
    88 
    82 
    89   # create the fields for the notification
    83   template = DEF_GROUP_INVITE_NOTIFICATION_TEMPLATE
       
    84 
       
    85   sendNotification(to_user, message_properties, subject, template)
       
    86 
       
    87 
       
    88 def sendNotification(to_user, message_properties, subject, template):
       
    89   """Sends out an notification to the specified user.
       
    90 
       
    91   Args:
       
    92     entity : A request containing the information needed to create the message
       
    93   """
       
    94 
       
    95   from_user = model_logic.user.logic.getForCurrentAccount()
       
    96 
       
    97   new_message_properties = {
       
    98       'sender_name': from_user.name,
       
    99       'to_name': to_user.name,
       
   100       }
       
   101 
       
   102   message_properties = dicts.merge(message_properties, new_message_properties)
       
   103 
       
   104   message = loader.render_to_string(template, dictionary=message_properties)
       
   105 
    90   fields = {
   106   fields = {
    91       'from_user' : current_user_entity,
   107       'from_user' : from_user,
    92       'subject' : DEF_INVITATION_MSG_FMT % {
   108       'subject': subject,
    93           'role' : entity.role,
       
    94           'group' : group_entity.name
       
    95           },
       
    96       'message' : message,
   109       'message' : message,
    97       'scope' : request_user_entity,
   110       'scope' : to_user,
    98       'link_id' :'%i' % (time.time()),
   111       'link_id' :'%i' % (time.time()),
    99       'scope_path' : request_user_entity.link_id
   112       'scope_path' : to_user.link_id
   100   }
   113   }
   101 
   114 
       
   115   key_fields = model_logic.notification.logic.getKeyFieldsFromDict(fields)
       
   116 
   102   # create and put a new notification in the datastore
   117   # create and put a new notification in the datastore
   103   notification_logic = model_logic.notification.logic
   118   model_logic.notification.logic.updateOrCreateFromFields(fields, key_fields)
   104   notification_logic.updateOrCreateFromFields(fields,
       
   105       notification_logic.getKeyFieldsFromDict(fields))
       
   106 
   119 
   107 
   120 
   108 def sendNewNotificationMessage(notification_entity):
   121 def sendNewNotificationMessage(notification_entity):
   109   """Sends an email to a user about a new notification
   122   """Sends an email to a user about a new notification
   110 
   123