app/soc/logic/helper/notifications.py
author Lennard de Rijk <ljvderijk@gmail.com>
Sat, 24 Oct 2009 15:14:11 -0700
changeset 3036 c703f3802841
parent 2879 cb0f9b4646aa
child 3051 af30b338d191
permissions -rw-r--r--
Use skipFields method in the expando base logic.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
     1
#!/usr/bin/python2.5
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
     2
#
1308
35b75ffcbb37 Partially reverted "Update the copyright notice for 2009."
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1307
diff changeset
     3
# Copyright 2008 the Melange authors.
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
     4
#
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
     6
# you may not use this file except in compliance with the License.
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
     7
# You may obtain a copy of the License at
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
     8
#
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    10
#
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    14
# See the License for the specific language governing permissions and
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    15
# limitations under the License.
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    16
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    17
"""Helper functions for sending out notifications.
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    18
"""
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    19
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    20
__authors__ = [
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    21
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    22
  ]
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    23
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    24
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    25
import time
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    26
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    27
from django.template import loader
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    28
from django.utils.encoding import force_unicode
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 930
diff changeset
    29
from django.utils.translation import ugettext
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    30
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    31
# We cannot import soc.logic.models notification nor user here
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    32
# due to cyclic imports
1548
15caebd3304d Denormalize accounts before trying to send e-mail
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1546
diff changeset
    33
from soc.logic import accounts
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    34
from soc.logic import dicts
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    35
from soc.logic import mail_dispatcher
2879
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
    36
from soc.logic import system
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    37
from soc.views.helper import redirects
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    38
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    39
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 930
diff changeset
    40
DEF_NEW_NOTIFICATION_MSG = ugettext(
1893
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
    41
    "You have received a new Notification.")
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    42
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 930
diff changeset
    43
DEF_INVITATION_MSG_FMT = ugettext(
915
27c656c01591 Added verbose role name to the request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 870
diff changeset
    44
    "Invitation to become a %(role_verbose)s for %(group)s.")
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    45
2676
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
    46
DEF_NEW_REQUEST_MSG_FMT = ugettext(
2683
8ea17736a10d Two style fixes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2676
diff changeset
    47
    "New Request Received from %(requester)s to become a %(role_verbose)s "
8ea17736a10d Two style fixes.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2676
diff changeset
    48
    "for %(group)s")
2676
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
    49
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
    50
DEF_NEW_GROUP_MSG_FMT = ugettext(
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
    51
    "Your %(application_type)s for %(group_name)s has been accepted.")
821
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
    52
1893
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
    53
DEF_NEW_REVIEW_SUBJECT_FMT = ugettext(
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
    54
    "New %s Review on %s")
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
    55
1450
688648dfe42c Use site name and noreply address when sending emails
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
    56
DEF_WELCOME_MSG_FMT = ugettext("Welcome to %(site_name)s, %(name)s,")
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    57
1648
11c74138c56d Remove unused variables and imports, fix too long lines in soc.logic.helper.notifications module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1563
diff changeset
    58
DEF_GROUP_INVITE_NOTIFICATION_TEMPLATE = 'soc/notification/messages/' \
11c74138c56d Remove unused variables and imports, fix too long lines in soc.logic.helper.notifications module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1563
diff changeset
    59
    'invitation.html'
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    60
2676
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
    61
DEF_NEW_REQUEST_NOTIFICATION_TEMPLATE = 'soc/notification/messages/' \
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
    62
    'new_request.html'
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
    63
1893
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
    64
DEF_NEW_REVIEW_NOTIFICATION_TEMPLATE = 'soc/notification/messages/' \
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
    65
    'new_review.html'
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
    66
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
    67
DEF_NEW_GROUP_TEMPLATE = 'soc/group/messages/accepted.html'
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    68
818
ddd102e82107 Fixed whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 792
diff changeset
    69
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    70
def sendInviteNotification(entity):
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    71
  """Sends out an invite notification to the user the request is for.
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    72
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    73
  Args:
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    74
    entity : A request containing the information needed to create the message
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    75
  """
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    76
2671
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
    77
  from soc.logic.models.user import logic as user_logic
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
    78
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    79
  # get the user the request is for
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    80
  properties = {'link_id': entity.link_id }
2671
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
    81
  to_user = user_logic.getForFields(properties, unique=True)
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    82
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    83
  invitation_url = "http://%(host)s%(index)s" % {
2879
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
    84
      'host' : system.getHostname(),
930
7e88fba647b1 Added missing changes from r1522.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 915
diff changeset
    85
      'index': redirects.getInviteProcessRedirect(entity, None),
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    86
      }
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    87
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    88
  message_properties = {
915
27c656c01591 Added verbose role name to the request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 870
diff changeset
    89
      'role_verbose' : entity.role_verbose,
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    90
      'group': entity.scope.name,
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    91
      'invitation_url': invitation_url,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    92
      }
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    93
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    94
  subject = DEF_INVITATION_MSG_FMT % {
915
27c656c01591 Added verbose role name to the request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 870
diff changeset
    95
      'role_verbose' : entity.role_verbose,
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    96
      'group' : entity.scope.name
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    97
      }
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    98
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    99
  template = DEF_GROUP_INVITE_NOTIFICATION_TEMPLATE
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   100
2671
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
   101
  from_user = user_logic.getForCurrentAccount()
1887
8b71f4e58f39 Changed notifications helper to properly use the fact that from_user is not a required property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1648
diff changeset
   102
8b71f4e58f39 Changed notifications helper to properly use the fact that from_user is not a required property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1648
diff changeset
   103
  sendNotification(to_user, from_user, message_properties, subject, template)
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   104
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   105
2676
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   106
def sendNewRequestNotification(request_entity):
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   107
  """Sends out a notification to the persons who can process this Request.
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   108
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   109
  Args:
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   110
    request_entity: an instance of Request model
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   111
  """
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   112
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   113
  from soc.logic.helper import notifications
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   114
  from soc.logic.models.role import ROLE_LOGICS
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   115
  from soc.logic.models.user import logic as user_logic
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   116
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   117
  # get the users who should get the notification
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   118
  to_users = []
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   119
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   120
  # retrieve the Role Logics which we should query on
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   121
  role_logic = ROLE_LOGICS[request_entity.role]
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   122
  role_logics_to_notify = role_logic.getRoleLogicsToNotifyUponNewRequest()
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   123
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   124
  # the scope of the roles is the same as the scope of the Request entity
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   125
  fields = {'scope': request_entity.scope,
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   126
            'status': 'active'}
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   127
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   128
  for role_logic in role_logics_to_notify:
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   129
    roles = role_logic.getForFields(fields)
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   130
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   131
    for role_entity in roles:
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   132
      # TODO: this might lead to double notifications
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   133
      to_users.append(role_entity.user)
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   134
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   135
  # get the user the request is from
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   136
  properties = {'link_id': request_entity.link_id }
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   137
  user_entity = user_logic.getForFields(properties, unique=True)
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   138
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   139
  message_properties = {
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   140
      'requester_name': user_entity.name,
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   141
      'entity': request_entity,
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   142
      'request_url': redirects.getProcessRequestRedirect(request_entity, None)}
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   143
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   144
  subject = DEF_NEW_REQUEST_MSG_FMT % {
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   145
      'requester': user_entity.name,
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   146
      'role_verbose' : request_entity.role_verbose,
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   147
      'group' : request_entity.scope.name
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   148
      }
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   149
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   150
  template = DEF_NEW_REQUEST_NOTIFICATION_TEMPLATE
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   151
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   152
  for to_user in to_users:
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   153
    notifications.sendNotification(to_user, None, message_properties,
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   154
                                   subject, template)
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   155
a7fabd1534f8 Added sendNewRequestNotification to the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2671
diff changeset
   156
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   157
def sendNewGroupNotification(entity, params):
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   158
  """Sends out an invite notification to the applicant of the group.
821
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   159
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   160
  Args:
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   161
    entity : An accepted group application
821
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   162
  """
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   163
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   164
  url = "http://%(host)s%(redirect)s" % {
1511
3342ce7a495c Fix some whitespace damadge and a style fix
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1508
diff changeset
   165
      'redirect': redirects.getApplicantRedirect(entity,
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   166
      {'url_name': params['group_url_name']}),
2879
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
   167
      'host': system.getHostname(),
821
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   168
      }
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   169
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   170
  message_properties = {
1533
50fb3a9b6615 Fix for Issue 218.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1512
diff changeset
   171
      'application_type': params['name'],
50fb3a9b6615 Fix for Issue 218.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1512
diff changeset
   172
      'group_type': params['group_name'],
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   173
      'group_name': entity.name,
821
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   174
      'url': url,
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   175
      }
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   176
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   177
  subject = DEF_NEW_GROUP_MSG_FMT % {
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   178
      'application_type': params['name'],
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   179
      'group_name': entity.name,
821
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   180
      }
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   181
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   182
  template = DEF_NEW_GROUP_TEMPLATE
821
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   183
2170
cec46675f866 Send out notifications and emails to admin and backup admin
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2068
diff changeset
   184
  for to in [entity.applicant, entity.backup_admin]:
2171
83d96aadd228 Do not rely on a backup_admin being set
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2170
diff changeset
   185
    if not to:
83d96aadd228 Do not rely on a backup_admin being set
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2170
diff changeset
   186
      continue
83d96aadd228 Do not rely on a backup_admin being set
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2170
diff changeset
   187
2170
cec46675f866 Send out notifications and emails to admin and backup admin
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2068
diff changeset
   188
    sendNotification(to, None, message_properties, subject, template)
821
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   189
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   190
1893
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   191
def sendNewReviewNotification(to_user, review, reviewed_name, redirect_url):
1894
da52a47b5a6b Send out a message on a new StudentProposal review to all followers.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1893
diff changeset
   192
  """Sends out a notification to alert the user of a new Review.
1893
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   193
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   194
  Args:
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   195
    to_user: The user who should receive a notification
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   196
    review: The review which triggers this notification
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   197
    reviewed_name: Name of the entity reviewed
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   198
    redirect_url: URL to which the follower should be sent for more information
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   199
  """
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   200
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   201
  message_properties = {'redirect_url': redirect_url,
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   202
      'reviewer_name': review.author_name(),
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   203
      'reviewed_name': reviewed_name,
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   204
      }
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   205
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   206
  # determine the subject
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   207
  review_type = 'public' if review.is_public else 'private'
2068
f5e8fbbd3bea Style fixes and removal of unused imports in soc.logic.helper modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1894
diff changeset
   208
  subject =  DEF_NEW_REVIEW_SUBJECT_FMT % (review_type, reviewed_name)
1893
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   209
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   210
  template = DEF_NEW_REVIEW_NOTIFICATION_TEMPLATE
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   211
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   212
  # send the notification from the system
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   213
  sendNotification(to_user, None, message_properties, subject, template)
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   214
bcb73072aa66 Added sendNewReviewNotification to the notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1890
diff changeset
   215
1887
8b71f4e58f39 Changed notifications helper to properly use the fact that from_user is not a required property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1648
diff changeset
   216
def sendNotification(to_user, from_user, message_properties, subject, template):
8b71f4e58f39 Changed notifications helper to properly use the fact that from_user is not a required property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1648
diff changeset
   217
  """Sends out a notification to the specified user.
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   218
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   219
  Args:
831
cbe033fedde8 Add missing dots in docstrings. Add sendNotification parameters description to docstring in soc.logic.helper module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 821
diff changeset
   220
    to_user : user to which the notification will be send
1887
8b71f4e58f39 Changed notifications helper to properly use the fact that from_user is not a required property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1648
diff changeset
   221
    from_user: user from who sends the notifications (None iff sent by site)
8b71f4e58f39 Changed notifications helper to properly use the fact that from_user is not a required property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1648
diff changeset
   222
    message_properties : message properties
831
cbe033fedde8 Add missing dots in docstrings. Add sendNotification parameters description to docstring in soc.logic.helper module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 821
diff changeset
   223
    subject : subject of notification email
cbe033fedde8 Add missing dots in docstrings. Add sendNotification parameters description to docstring in soc.logic.helper module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 821
diff changeset
   224
    template : template used for generating notification
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   225
  """
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   226
2671
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
   227
  from soc.logic.models.notification import logic as notification_logic
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
   228
  from soc.logic.models.site import logic as site_logic
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
   229
1887
8b71f4e58f39 Changed notifications helper to properly use the fact that from_user is not a required property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1648
diff changeset
   230
  if from_user:
8b71f4e58f39 Changed notifications helper to properly use the fact that from_user is not a required property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1648
diff changeset
   231
    sender_name = from_user.name
8b71f4e58f39 Changed notifications helper to properly use the fact that from_user is not a required property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1648
diff changeset
   232
  else:
2671
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
   233
    site_entity = site_logic.getSingleton()
2068
f5e8fbbd3bea Style fixes and removal of unused imports in soc.logic.helper modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1894
diff changeset
   234
    sender_name = 'The %s Team' % (site_entity.site_name)
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   235
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   236
  new_message_properties = {
1887
8b71f4e58f39 Changed notifications helper to properly use the fact that from_user is not a required property.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1648
diff changeset
   237
      'sender_name': sender_name,
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   238
      'to_name': to_user.name,
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   239
      }
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   240
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   241
  message_properties = dicts.merge(message_properties, new_message_properties)
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   242
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   243
  message = loader.render_to_string(template, dictionary=message_properties)
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   244
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   245
  fields = {
1890
a8a5da24212c Increased the length of the timebased link_id to handle more items at once.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1887
diff changeset
   246
      'from_user': from_user,
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   247
      'subject': subject,
1890
a8a5da24212c Increased the length of the timebased link_id to handle more items at once.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1887
diff changeset
   248
      'message': message,
a8a5da24212c Increased the length of the timebased link_id to handle more items at once.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1887
diff changeset
   249
      'scope': to_user,
2068
f5e8fbbd3bea Style fixes and removal of unused imports in soc.logic.helper modules.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 1894
diff changeset
   250
      'link_id': 't%i' % (int(time.time()*100)),
1890
a8a5da24212c Increased the length of the timebased link_id to handle more items at once.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1887
diff changeset
   251
      'scope_path': to_user.link_id
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   252
  }
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   253
2671
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
   254
  key_name = notification_logic.getKeyNameFromFields(fields)
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   255
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   256
  # create and put a new notification in the datastore
2671
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
   257
  notification_logic.updateOrCreateFromKeyName(fields, key_name)
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   258
818
ddd102e82107 Fixed whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 792
diff changeset
   259
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   260
def sendNewNotificationMessage(notification_entity):
831
cbe033fedde8 Add missing dots in docstrings. Add sendNotification parameters description to docstring in soc.logic.helper module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 821
diff changeset
   261
  """Sends an email to a user about a new notification.
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   262
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   263
    Args:
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   264
      notification_entity: Notification about which the message should be sent
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   265
  """
2671
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
   266
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
   267
  from soc.logic.models.site import logic as site_logic
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
   268
  from soc.views.models.notification import view as notification_view
2319
3eee2308f1dd Do not rely on notifiction module being imported
Sverre Rabbelier <sverre@rabbelier.nl>
parents: 2171
diff changeset
   269
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   270
  # create the url to show this notification
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   271
  notification_url = "http://%(host)s%(index)s" % {
2879
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
   272
      'host' : system.getHostname(),
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   273
      'index': redirects.getPublicRedirect(notification_entity,
2671
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
   274
          notification_view.getParams())}
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   275
1546
cc54ada3cf1b Using getDefaultMailSender for welcome and notification messages.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1533
diff changeset
   276
  sender = mail_dispatcher.getDefaultMailSender()
2671
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
   277
  site_entity = site_logic.getSingleton()
1561
fe1b141bfc2c Replaced 'Melange' with site_name where appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1560
diff changeset
   278
  site_name = site_entity.site_name
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   279
1550
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   280
  # get the default mail sender
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   281
  default_sender = mail_dispatcher.getDefaultMailSender()
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   282
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   283
  if not default_sender:
1546
cc54ada3cf1b Using getDefaultMailSender for welcome and notification messages.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1533
diff changeset
   284
    # no valid sender found, abort
cc54ada3cf1b Using getDefaultMailSender for welcome and notification messages.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1533
diff changeset
   285
    return
1550
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   286
  else:
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   287
    (sender_name, sender) = default_sender
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   288
1548
15caebd3304d Denormalize accounts before trying to send e-mail
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1546
diff changeset
   289
  to = accounts.denormalizeAccount(notification_entity.scope.account).email()
15caebd3304d Denormalize accounts before trying to send e-mail
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1546
diff changeset
   290
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   291
  # create the message contents
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   292
  messageProperties = {
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   293
      'to_name': notification_entity.scope.name,
1550
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   294
      'sender_name': sender_name,
1548
15caebd3304d Denormalize accounts before trying to send e-mail
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1546
diff changeset
   295
      'to': to,
1546
cc54ada3cf1b Using getDefaultMailSender for welcome and notification messages.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1533
diff changeset
   296
      'sender': sender,
1561
fe1b141bfc2c Replaced 'Melange' with site_name where appropriate
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1560
diff changeset
   297
      'site_name': site_name,
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   298
      'subject': force_unicode(DEF_NEW_NOTIFICATION_MSG),
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   299
      'notification' : notification_entity,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   300
      'notification_url' : notification_url
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   301
      }
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   302
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   303
  # send out the message using the default new notification template
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   304
  mail_dispatcher.sendMailFromTemplate('soc/mail/new_notification.html',
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   305
                                       messageProperties)
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   306
818
ddd102e82107 Fixed whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 792
diff changeset
   307
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   308
def sendWelcomeMessage(user_entity):
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   309
  """Sends out a welcome message to a user.
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   310
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   311
    Args:
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   312
      user_entity: User entity which the message should be send to
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   313
  """
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   314
2671
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
   315
  from soc.logic.models.site import logic as site_logic
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
   316
1546
cc54ada3cf1b Using getDefaultMailSender for welcome and notification messages.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1533
diff changeset
   317
  # get site name
2671
2eaacbbdb168 Use proper in-line imports where possible in the Notifications helper.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2378
diff changeset
   318
  site_entity = site_logic.getSingleton()
1450
688648dfe42c Use site name and noreply address when sending emails
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   319
  site_name = site_entity.site_name
688648dfe42c Use site name and noreply address when sending emails
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   320
1546
cc54ada3cf1b Using getDefaultMailSender for welcome and notification messages.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1533
diff changeset
   321
  # get the default mail sender
1550
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   322
  default_sender = mail_dispatcher.getDefaultMailSender()
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   323
1550
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   324
  if not default_sender:
1546
cc54ada3cf1b Using getDefaultMailSender for welcome and notification messages.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1533
diff changeset
   325
    # no valid sender found, should not happen but abort anyway
cc54ada3cf1b Using getDefaultMailSender for welcome and notification messages.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1533
diff changeset
   326
    return
1550
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   327
  else:
1560
656aace0b15f Catch mail exceptions and use get_current_user
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1550
diff changeset
   328
    sender_name, sender = default_sender
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   329
1548
15caebd3304d Denormalize accounts before trying to send e-mail
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1546
diff changeset
   330
  to = accounts.denormalizeAccount(user_entity.account).email()
15caebd3304d Denormalize accounts before trying to send e-mail
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1546
diff changeset
   331
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   332
  # create the message contents
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   333
  messageProperties = {
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   334
      'to_name': user_entity.name,
1550
a872030dc3ca getDefaultMailSender now returns a tuple (name, email adress).
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1548
diff changeset
   335
      'sender_name': sender_name,
1548
15caebd3304d Denormalize accounts before trying to send e-mail
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1546
diff changeset
   336
      'to': to,
1546
cc54ada3cf1b Using getDefaultMailSender for welcome and notification messages.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1533
diff changeset
   337
      'sender': sender,
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   338
      'subject': DEF_WELCOME_MSG_FMT % {
1450
688648dfe42c Use site name and noreply address when sending emails
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1308
diff changeset
   339
          'site_name': site_name,
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   340
          'name': user_entity.name
2378
b5c74f077f0a Added a link to the website where the user has registered to the welcome email.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2360
diff changeset
   341
          },
b5c74f077f0a Added a link to the website where the user has registered to the welcome email.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 2360
diff changeset
   342
      'site_name': site_name,
2879
cb0f9b4646aa Revert "revert wrong head"
Sverre Rabbelier <srabbelier@gmail.com>
parents: 2871
diff changeset
   343
      'site_location': 'http://%s' % system.getHostname(),
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   344
      }
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   345
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   346
  # send out the message using the default welcome template
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   347
  mail_dispatcher.sendMailFromTemplate('soc/mail/welcome.html',
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   348
                                       messageProperties)