app/soc/logic/helper/notifications.py
changeset 792 3cc6bea5c632
parent 784 23eaf3aa19b1
child 818 ddd102e82107
equal deleted inserted replaced
791:30da180c4bca 792:3cc6bea5c632
    50   """Sends out an invite notification to the user the request is for.
    50   """Sends out an invite notification to the user the request is for.
    51 
    51 
    52   Args:
    52   Args:
    53     entity : A request containing the information needed to create the message
    53     entity : A request containing the information needed to create the message
    54   """
    54   """
    55   
    55 
    56   # get user logic
    56   # get user logic
    57   user_logic = model_logic.user.logic
    57   user_logic = model_logic.user.logic
    58 
    58 
    59   # get the current user
    59   # get the current user
    60   current_user_entity = user_logic.getForCurrentAccount()
    60   current_user_entity = user_logic.getForCurrentAccount()
    61   
    61 
    62   # get the user the request is for
    62   # get the user the request is for
    63   properties = {'link_id': entity.link_id }
    63   properties = {'link_id': entity.link_id }
    64   request_user_entity = user_logic.getForFields(properties, unique=True)
    64   request_user_entity = user_logic.getForFields(properties, unique=True)
    65 
    65 
    66   # create the invitation_url
    66   # create the invitation_url
    67   invitation_url = "http://%(host)s%(index)s" % {
    67   invitation_url = "http://%(host)s%(index)s" % {
    68       'host' : os.environ['HTTP_HOST'], 
    68       'host' : os.environ['HTTP_HOST'],
    69       'index': redirects.inviteAcceptedRedirect(entity, None)}
    69       'index': redirects.inviteAcceptedRedirect(entity, None)}
    70 
    70 
    71   # get the group entity
    71   # get the group entity
    72   group_entity = entity.scope
    72   group_entity = entity.scope
    73   
    73 
    74   # create the properties for the message
    74   # create the properties for the message
    75   messageProperties = {
    75   messageProperties = {
    76       'to_name': request_user_entity.name,
    76       'to_name': request_user_entity.name,
    77       'sender_name': current_user_entity.name,
    77       'sender_name': current_user_entity.name,
    78       'role': entity.role,
    78       'role': entity.role,
    79       'group': group_entity.name,
    79       'group': group_entity.name,
    80       'invitation_url': invitation_url,
    80       'invitation_url': invitation_url,
    81       }
    81       }
    82   
    82 
    83   # render the message
    83   # render the message
    84   message = loader.render_to_string(
    84   message = loader.render_to_string(
    85       'soc/notification/messages/invitation.html', 
    85       'soc/notification/messages/invitation.html',
    86       dictionary=messageProperties)
    86       dictionary=messageProperties)
    87   
    87 
    88   # create the fields for the notification
    88   # create the fields for the notification
    89   fields = { 
    89   fields = {
    90       'from_user' : current_user_entity,
    90       'from_user' : current_user_entity,
    91       'subject' : DEF_INVITATION_MSG_FMT % {
    91       'subject' : DEF_INVITATION_MSG_FMT % {
    92           'role' : entity.role,
    92           'role' : entity.role,
    93           'group' : group_entity.name 
    93           'group' : group_entity.name
    94           },
    94           },
    95       'message' : message,
    95       'message' : message,
    96       'scope' : request_user_entity,
    96       'scope' : request_user_entity,
    97       'link_id' :'%i' % (time.time()),
    97       'link_id' :'%i' % (time.time()),
    98       'scope_path' : request_user_entity.link_id
    98       'scope_path' : request_user_entity.link_id
    99   }
    99   }
   100   
   100 
   101   # create and put a new notification in the datastore
   101   # create and put a new notification in the datastore
   102   notification_logic = model_logic.notification.logic
   102   notification_logic = model_logic.notification.logic
   103   notification_logic.updateOrCreateFromFields(fields, 
   103   notification_logic.updateOrCreateFromFields(fields,
   104       notification_logic.getKeyFieldsFromDict(fields))
   104       notification_logic.getKeyFieldsFromDict(fields))
   105   
   105 
   106 def sendNewNotificationMessage(notification_entity):
   106 def sendNewNotificationMessage(notification_entity):
   107   """Sends an email to a user about a new notification
   107   """Sends an email to a user about a new notification
   108   
   108 
   109     Args:
   109     Args:
   110       notification_entity: Notification about which the message should be sent    
   110       notification_entity: Notification about which the message should be sent
   111   """
   111   """
   112 
   112 
   113   # get user logic
   113   # get user logic
   114   user_logic = model_logic.user  
   114   user_logic = model_logic.user
   115   
   115 
   116   # get the current user
   116   # get the current user
   117   current_user_entity = user_logic.logic.getForCurrentAccount()
   117   current_user_entity = user_logic.logic.getForCurrentAccount()
   118 
   118 
   119   # create the url to show this notification
   119   # create the url to show this notification
   120   notification_url = "http://%(host)s%(index)s" % {
   120   notification_url = "http://%(host)s%(index)s" % {
   121       'host' : os.environ['HTTP_HOST'], 
   121       'host' : os.environ['HTTP_HOST'],
   122       'index': redirects.getPublicRedirect(notification_entity,
   122       'index': redirects.getPublicRedirect(notification_entity,
   123           model_view.notification.view.getParams())}
   123           model_view.notification.view.getParams())}
   124 
   124 
   125 
   125 
   126   # TODO(Lennard): Change the sender to the no-reply address
   126   # TODO(Lennard): Change the sender to the no-reply address
   133       'sender': current_user_entity.account.email(),
   133       'sender': current_user_entity.account.email(),
   134       'subject': force_unicode(DEF_NEW_NOTIFICATION_MSG),
   134       'subject': force_unicode(DEF_NEW_NOTIFICATION_MSG),
   135       'notification' : notification_entity,
   135       'notification' : notification_entity,
   136       'notification_url' : notification_url
   136       'notification_url' : notification_url
   137       }
   137       }
   138   
   138 
   139   # send out the message using the default new notification template    
   139   # send out the message using the default new notification template
   140   mail_dispatcher.sendMailFromTemplate('soc/mail/new_notification.html', 
   140   mail_dispatcher.sendMailFromTemplate('soc/mail/new_notification.html',
   141                                        messageProperties)
   141                                        messageProperties)
   142   
   142 
   143 def sendWelcomeMessage(user_entity):
   143 def sendWelcomeMessage(user_entity):
   144   """Sends out a welcome message to a user.
   144   """Sends out a welcome message to a user.
   145 
   145 
   146     Args:
   146     Args:
   147       user_entity: User entity which the message should be send to
   147       user_entity: User entity which the message should be send to
   148   """
   148   """
   149   
   149 
   150   # get user logic
   150   # get user logic
   151   user_logic = model_logic.user  
   151   user_logic = model_logic.user
   152   
   152 
   153   # get the current user
   153   # get the current user
   154   current_user_entity = user_logic.logic.getForCurrentAccount()
   154   current_user_entity = user_logic.logic.getForCurrentAccount()
   155 
   155 
   156   # TODO(Lennard): change the message sender to some sort of no-reply adress 
   156   # TODO(Lennard): change the message sender to some sort of no-reply adress
   157   # that is probably a setting in sitesettings. (adress must be a developer). 
   157   # that is probably a setting in sitesettings. (adress must be a developer).
   158   # This is due to a GAE limitation that allows only devs or the current user 
   158   # This is due to a GAE limitation that allows only devs or the current user
   159   # to send an email. Currently this results in a user receiving the same 
   159   # to send an email. Currently this results in a user receiving the same
   160   # email twice.
   160   # email twice.
   161   
   161 
   162   # create the message contents
   162   # create the message contents
   163   messageProperties = {
   163   messageProperties = {
   164       'to_name': user_entity.name,
   164       'to_name': user_entity.name,
   165       'sender_name': current_user_entity.name,
   165       'sender_name': current_user_entity.name,
   166       'to': user_entity.account.email(),
   166       'to': user_entity.account.email(),
   167       'sender': current_user_entity.account.email(),
   167       'sender': current_user_entity.account.email(),
   168       'subject': DEF_WELCOME_MSG_FMT % {
   168       'subject': DEF_WELCOME_MSG_FMT % {
   169           'name': user_entity.name
   169           'name': user_entity.name
   170           }
   170           }
   171       } 
   171       }
   172 
   172 
   173   # send out the message using the default welcome template    
   173   # send out the message using the default welcome template
   174   mail_dispatcher.sendMailFromTemplate('soc/mail/welcome.html', 
   174   mail_dispatcher.sendMailFromTemplate('soc/mail/welcome.html',
   175                                        messageProperties)
   175                                        messageProperties)