app/soc/logic/helper/notifications.py
author Sverre Rabbelier <srabbelier@gmail.com>
Thu, 19 Feb 2009 22:51:24 +0000
changeset 1407 423425ac79f5
parent 1308 35b75ffcbb37
child 1450 688648dfe42c
permissions -rw-r--r--
Fixed copy/paste fail Patch by: Sverre Rabbelier
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 os
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    26
import time
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    27
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    28
from google.appengine.api import users
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    29
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    30
from django.template import loader
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    31
from django.utils.encoding import force_unicode
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 930
diff changeset
    32
from django.utils.translation import ugettext
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    33
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    34
# 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
    35
# due to cyclic imports
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    36
from soc.logic import dicts
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    37
from soc.logic import mail_dispatcher
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    38
from soc.views.helper import redirects
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    39
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    40
import soc.views.models as model_view
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    41
import soc.logic.models as model_logic
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    42
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    43
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 930
diff changeset
    44
DEF_NEW_NOTIFICATION_MSG = ugettext(
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    45
  "You have received a new Notification.")
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    46
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 930
diff changeset
    47
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
    48
    "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
    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
970
8b5611d5b053 Use ugettext instead of ugettext_lazy
Sverre Rabbelier <srabbelier@gmail.com>
parents: 930
diff changeset
    53
DEF_WELCOME_MSG_FMT = ugettext("Welcome to Melange %(name)s,")
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    54
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    55
DEF_GROUP_INVITE_NOTIFICATION_TEMPLATE = 'soc/notification/messages/invitation.html'
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    56
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
    57
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
    58
818
ddd102e82107 Fixed whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 792
diff changeset
    59
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    60
def sendInviteNotification(entity):
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    61
  """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
    62
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    63
  Args:
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    64
    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
    65
  """
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    66
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    67
  # get the user the request is for
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    68
  properties = {'link_id': entity.link_id }
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    69
  to_user = model_logic.user.logic.getForFields(properties, unique=True)
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    70
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    71
  invitation_url = "http://%(host)s%(index)s" % {
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    72
      'host' : os.environ['HTTP_HOST'],
930
7e88fba647b1 Added missing changes from r1522.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 915
diff changeset
    73
      'index': redirects.getInviteProcessRedirect(entity, None),
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    74
      }
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    75
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    76
  message_properties = {
915
27c656c01591 Added verbose role name to the request model.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 870
diff changeset
    77
      'role_verbose' : entity.role_verbose,
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    78
      'group': entity.scope.name,
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    79
      'invitation_url': invitation_url,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    80
      }
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    81
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    82
  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
    83
      'role_verbose' : entity.role_verbose,
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    84
      'group' : entity.scope.name
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    85
      }
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    86
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    87
  template = DEF_GROUP_INVITE_NOTIFICATION_TEMPLATE
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    88
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    89
  sendNotification(to_user, message_properties, subject, template)
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    90
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
    91
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
    92
def sendNewGroupNotification(entity, params):
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
    93
  """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
    94
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
    95
  Args:
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
    96
    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
    97
  """
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
    98
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
    99
  to_user = entity.applicant
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   100
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   101
  url = "http://%(host)s%(redirect)s" % {
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   102
      'redirect': redirects.getApplicantRedirect(entity, 
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   103
      {'url_name': params['group_url_name']}),
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   104
      'host': os.environ['HTTP_HOST'],
821
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   105
      }
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   106
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   107
  message_properties = {
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   108
      'group_name': entity.name,
821
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   109
      'url': url,
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   110
      }
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   111
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   112
  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
   113
      'application_type': params['name'],
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   114
      'group_name': entity.name,
821
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   115
      }
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   116
1138
18ef39338211 Refactored review and reviewoverview out of club_app.py
Lennard de Rijk <ljvderijk@gmail.com>
parents: 1115
diff changeset
   117
  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
   118
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   119
  sendNotification(to_user, message_properties, subject, template)
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   120
5afe16b2e86b Send a notifcation if a club application is accepted
Sverre Rabbelier <srabbelier@gmail.com>
parents: 820
diff changeset
   121
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   122
def sendNotification(to_user, message_properties, subject, template):
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   123
  """Sends out an notification to the specified user.
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   124
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   125
  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
   126
    to_user : user to which the notification will be send
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
   127
    message_properties : email message properties
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
   128
    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
   129
    template : template used for generating notification
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   130
  """
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   131
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   132
  from_user = model_logic.user.logic.getForCurrentAccount()
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   133
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   134
  new_message_properties = {
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   135
      'sender_name': from_user.name,
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   136
      'to_name': to_user.name,
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   137
      }
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   138
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   139
  message_properties = dicts.merge(message_properties, new_message_properties)
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   140
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   141
  message = loader.render_to_string(template, dictionary=message_properties)
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   142
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   143
  fields = {
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   144
      'from_user' : from_user,
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   145
      'subject': subject,
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   146
      'message' : message,
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   147
      'scope' : to_user,
1059
17221b578fe7 prefixing the link_id of a notification with a t for time to agree to the new link_id pattern.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 970
diff changeset
   148
      'link_id' :'t%i' % (time.time()),
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   149
      'scope_path' : to_user.link_id
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   150
  }
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   151
1115
0a723ff3d27c Cleanups in base.Logic
Sverre Rabbelier <srabbelier@gmail.com>
parents: 1059
diff changeset
   152
  key_fields = model_logic.notification.logic.getKeyFieldsFromFields(fields)
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   153
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   154
  # create and put a new notification in the datastore
820
56eec9c8bb47 Factor out sendNotification from sendInviteNotification
Sverre Rabbelier <srabbelier@gmail.com>
parents: 818
diff changeset
   155
  model_logic.notification.logic.updateOrCreateFromFields(fields, key_fields)
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   156
818
ddd102e82107 Fixed whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 792
diff changeset
   157
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   158
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
   159
  """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
   160
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   161
    Args:
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   162
      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
   163
  """
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   164
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   165
  # get user logic
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   166
  user_logic = model_logic.user
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   167
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   168
  # get the current user
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   169
  current_user_entity = user_logic.logic.getForCurrentAccount()
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   170
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   171
  # create the url to show this notification
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   172
  notification_url = "http://%(host)s%(index)s" % {
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   173
      'host' : os.environ['HTTP_HOST'],
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   174
      'index': redirects.getPublicRedirect(notification_entity,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   175
          model_view.notification.view.getParams())}
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   176
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   177
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   178
  # TODO(Lennard): Change the sender to the no-reply address
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   179
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   180
  # create the message contents
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   181
  messageProperties = {
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   182
      'to_name': notification_entity.scope.name,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   183
      'sender_name': current_user_entity.name,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   184
      'to': notification_entity.scope.account.email(),
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   185
      'sender': current_user_entity.account.email(),
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   186
      'subject': force_unicode(DEF_NEW_NOTIFICATION_MSG),
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   187
      'notification' : notification_entity,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   188
      'notification_url' : notification_url
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   189
      }
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   190
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   191
  # 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
   192
  mail_dispatcher.sendMailFromTemplate('soc/mail/new_notification.html',
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   193
                                       messageProperties)
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   194
818
ddd102e82107 Fixed whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 792
diff changeset
   195
792
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   196
def sendWelcomeMessage(user_entity):
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   197
  """Sends out a welcome message to a user.
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   198
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   199
    Args:
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   200
      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
   201
  """
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   202
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   203
  # get user logic
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   204
  user_logic = model_logic.user
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   205
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   206
  # get the current user
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   207
  current_user_entity = user_logic.logic.getForCurrentAccount()
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   208
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   209
  # TODO(Lennard): change the message sender to some sort of no-reply adress
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   210
  # that is probably a setting in sitesettings. (adress must be a developer).
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   211
  # This is due to a GAE limitation that allows only devs or the current user
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   212
  # to send an email. Currently this results in a user receiving the same
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   213
  # email twice.
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   214
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   215
  # create the message contents
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   216
  messageProperties = {
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   217
      'to_name': user_entity.name,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   218
      'sender_name': current_user_entity.name,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   219
      'to': user_entity.account.email(),
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   220
      'sender': current_user_entity.account.email(),
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   221
      'subject': DEF_WELCOME_MSG_FMT % {
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   222
          'name': user_entity.name
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   223
          }
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   224
      }
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   225
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   226
  # 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
   227
  mail_dispatcher.sendMailFromTemplate('soc/mail/welcome.html',
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   228
                                       messageProperties)