app/soc/logic/helper/notifications.py
changeset 2676 a7fabd1534f8
parent 2671 2eaacbbdb168
child 2683 8ea17736a10d
equal deleted inserted replaced
2675:49cf6a8af70a 2676:a7fabd1534f8
    41     "You have received a new Notification.")
    41     "You have received a new Notification.")
    42 
    42 
    43 DEF_INVITATION_MSG_FMT = ugettext(
    43 DEF_INVITATION_MSG_FMT = ugettext(
    44     "Invitation to become a %(role_verbose)s for %(group)s.")
    44     "Invitation to become a %(role_verbose)s for %(group)s.")
    45 
    45 
       
    46 DEF_NEW_REQUEST_MSG_FMT = ugettext(
       
    47     "New Request Received from %(requester)s to become a %(role_verbose)s for %(group)s")
       
    48 
    46 DEF_NEW_GROUP_MSG_FMT = ugettext(
    49 DEF_NEW_GROUP_MSG_FMT = ugettext(
    47     "Your %(application_type)s for %(group_name)s has been accepted.")
    50     "Your %(application_type)s for %(group_name)s has been accepted.")
    48 
    51 
    49 DEF_NEW_REVIEW_SUBJECT_FMT = ugettext(
    52 DEF_NEW_REVIEW_SUBJECT_FMT = ugettext(
    50     "New %s Review on %s")
    53     "New %s Review on %s")
    51 
    54 
    52 DEF_WELCOME_MSG_FMT = ugettext("Welcome to %(site_name)s, %(name)s,")
    55 DEF_WELCOME_MSG_FMT = ugettext("Welcome to %(site_name)s, %(name)s,")
    53 
    56 
    54 DEF_GROUP_INVITE_NOTIFICATION_TEMPLATE = 'soc/notification/messages/' \
    57 DEF_GROUP_INVITE_NOTIFICATION_TEMPLATE = 'soc/notification/messages/' \
    55     'invitation.html'
    58     'invitation.html'
       
    59 
       
    60 DEF_NEW_REQUEST_NOTIFICATION_TEMPLATE = 'soc/notification/messages/' \
       
    61     'new_request.html'
    56 
    62 
    57 DEF_NEW_REVIEW_NOTIFICATION_TEMPLATE = 'soc/notification/messages/' \
    63 DEF_NEW_REVIEW_NOTIFICATION_TEMPLATE = 'soc/notification/messages/' \
    58     'new_review.html'
    64     'new_review.html'
    59 
    65 
    60 DEF_NEW_GROUP_TEMPLATE = 'soc/group/messages/accepted.html'
    66 DEF_NEW_GROUP_TEMPLATE = 'soc/group/messages/accepted.html'
    92   template = DEF_GROUP_INVITE_NOTIFICATION_TEMPLATE
    98   template = DEF_GROUP_INVITE_NOTIFICATION_TEMPLATE
    93 
    99 
    94   from_user = user_logic.getForCurrentAccount()
   100   from_user = user_logic.getForCurrentAccount()
    95 
   101 
    96   sendNotification(to_user, from_user, message_properties, subject, template)
   102   sendNotification(to_user, from_user, message_properties, subject, template)
       
   103 
       
   104 
       
   105 def sendNewRequestNotification(request_entity):
       
   106   """Sends out a notification to the persons who can process this Request.
       
   107 
       
   108   Args:
       
   109     request_entity: an instance of Request model
       
   110   """
       
   111 
       
   112   from soc.logic.helper import notifications
       
   113   from soc.logic.models.role import ROLE_LOGICS
       
   114   from soc.logic.models.user import logic as user_logic
       
   115 
       
   116   # get the users who should get the notification
       
   117   to_users = []
       
   118 
       
   119   # retrieve the Role Logics which we should query on
       
   120   role_logic = ROLE_LOGICS[request_entity.role]
       
   121   role_logics_to_notify = role_logic.getRoleLogicsToNotifyUponNewRequest()
       
   122 
       
   123   # the scope of the roles is the same as the scope of the Request entity
       
   124   fields = {'scope': request_entity.scope,
       
   125             'status': 'active'}
       
   126 
       
   127   for role_logic in role_logics_to_notify:
       
   128     roles = role_logic.getForFields(fields)
       
   129 
       
   130     for role_entity in roles:
       
   131       # TODO: this might lead to double notifications
       
   132       to_users.append(role_entity.user)
       
   133 
       
   134   # get the user the request is from
       
   135   properties = {'link_id': request_entity.link_id }
       
   136   user_entity = user_logic.getForFields(properties, unique=True)
       
   137 
       
   138   message_properties = {
       
   139       'requester_name': user_entity.name,
       
   140       'entity': request_entity,
       
   141       'request_url': redirects.getProcessRequestRedirect(request_entity, None)}
       
   142 
       
   143   subject = DEF_NEW_REQUEST_MSG_FMT % {
       
   144       'requester': user_entity.name,
       
   145       'role_verbose' : request_entity.role_verbose,
       
   146       'group' : request_entity.scope.name
       
   147       }
       
   148 
       
   149   template = DEF_NEW_REQUEST_NOTIFICATION_TEMPLATE
       
   150 
       
   151   for to_user in to_users:
       
   152     notifications.sendNotification(to_user, None, message_properties,
       
   153                                    subject, template)
    97 
   154 
    98 
   155 
    99 def sendNewGroupNotification(entity, params):
   156 def sendNewGroupNotification(entity, params):
   100   """Sends out an invite notification to the applicant of the group.
   157   """Sends out an invite notification to the applicant of the group.
   101 
   158