app/soc/logic/helper/notifications.py
author Sverre Rabbelier <srabbelier@gmail.com>
Wed, 03 Dec 2008 21:27:52 +0000
changeset 658 9964b1571ef8
parent 641 c59a0ffc2c31
child 664 fb4c825b24ab
permissions -rw-r--r--
Factor out the Group part of Sponsor This makes it possible to reuse the Group logic. Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
635
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     1
#!/usr/bin/python2.5
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     2
#
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     3
# Copyright 2008 the Melange authors.
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     4
#
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     6
# you may not use this file except in compliance with the License.
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     7
# You may obtain a copy of the License at
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     8
#
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    10
#
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    14
# See the License for the specific language governing permissions and
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    15
# limitations under the License.
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    16
638
22ec01fdf8f4 Add missing dot and correctly sort imports in soc.logic.helper.notifications module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 635
diff changeset
    17
"""Helper functions for sending out notifications.
635
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    18
"""
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    19
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    20
__authors__ = [
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    21
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    22
  ]
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    23
638
22ec01fdf8f4 Add missing dot and correctly sort imports in soc.logic.helper.notifications module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 635
diff changeset
    24
22ec01fdf8f4 Add missing dot and correctly sort imports in soc.logic.helper.notifications module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 635
diff changeset
    25
import os
22ec01fdf8f4 Add missing dot and correctly sort imports in soc.logic.helper.notifications module.
Pawel Solyga <Pawel.Solyga@gmail.com>
parents: 635
diff changeset
    26
635
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    27
from google.appengine.api import users
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    28
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    29
from django.utils.translation import ugettext_lazy
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    30
641
c59a0ffc2c31 Fixed a styling error.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 640
diff changeset
    31
import soc.logic.models as model_logic
c59a0ffc2c31 Fixed a styling error.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 640
diff changeset
    32
635
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    33
from soc.logic import mail_dispatcher
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    34
from soc.views.helper import redirects
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    35
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    36
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    37
DEF_INVITATION_FMT = ugettext_lazy(
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    38
    "Invitation to become a %(role)s for %(group)s")
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    39
640
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    40
DEF_WELCOME_FMT = ugettext_lazy("Welcome to Melange %(name)s")
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    41
635
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    42
def sendInviteNotification(entity):
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    43
  """Sends out an invite notification to the user the request is for.
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    44
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    45
  Args:
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    46
    entity : A request containing the information needed to create the message
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    47
  """
640
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    48
  
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    49
  # get user logic
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    50
  user_logic = model_logic.user
635
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    51
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    52
  # get the current user
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    53
  properties = {'account': users.get_current_user()}
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    54
  current_user_entity = user_logic.logic.getForFields(properties, unique=True)
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    55
  
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    56
  # get the user the request is for
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    57
  properties = {'link_id': entity.link_id }
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    58
  request_user_entity = user_logic.logic.getForFields(properties, unique=True)
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    59
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    60
  # create the invitation_url
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    61
  invitation_url = "%(host)s%(index)s" % {
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    62
      'host' : os.environ['HTTP_HOST'], 
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    63
      'index': redirects.inviteAcceptedRedirect(entity, None)}
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    64
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    65
  # get the group entity
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    66
  group_entity = entity.scope
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    67
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    68
  messageProperties = {
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    69
      'to_name': request_user_entity.name,
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    70
      'sender_name': current_user_entity.name,
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    71
      'role': entity.role,
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    72
      'group': group_entity.name,
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    73
      'invitation_url': invitation_url,
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    74
      'to': request_user_entity.account.email(),
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    75
      'sender': current_user_entity.account.email(),
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    76
      'subject': DEF_INVITATION_FMT % {
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    77
          'role': entity.role,
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    78
          'group': group_entity.name
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    79
          },
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    80
      }
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    81
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    82
  # send out the message using the default invitation template    
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    83
  mail_dispatcher.sendMailFromTemplate('soc/mail/invitation.html', 
2f5322ad1f5b Moved sendInviteMessage to a new module in logic/helper/notifications.py.
Lennard de Rijk <ljvderijk@gmail.com>
parents:
diff changeset
    84
                                       messageProperties)
640
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    85
  
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    86
def sendWelcomeMessage(user_entity):
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    87
  """Sends out a welcome message to a user.
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    88
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    89
    Args:
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    90
      user_entity: User entity which the message should be send to
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    91
  """
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    92
  
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    93
  # get user logic
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    94
  user_logic = model_logic.user  
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    95
  
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    96
  # get the current user
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    97
  properties = {'account': users.get_current_user()}
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    98
  current_user_entity = user_logic.logic.getForFields(properties, unique=True)
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
    99
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   100
  # create the message contents
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   101
  # TODO(Lennard) change the message sender to some sort of no-reply adress that is
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   102
  # probably a setting in sitesettings. (adress must be a developer). This is due
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   103
  # to a GAE limitation that allows only devs or the current user to send an email.
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   104
  # Currently this results in a user receiving the same email twice.
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   105
  messageProperties = {
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   106
      'to_name': user_entity.name,
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   107
      'sender_name': current_user_entity.name,
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   108
      'to': user_entity.account.email(),
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   109
      'sender': current_user_entity.account.email(),
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   110
      'subject': DEF_WELCOME_FMT % {
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   111
          'name': user_entity.name
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   112
          }
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   113
      } 
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   114
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   115
  # send out the message using the default welcome template    
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   116
  mail_dispatcher.sendMailFromTemplate('soc/mail/welcome.html', 
a62a78fe4e43 Added User welcome message.
Lennard de Rijk <ljvderijk@gmail.com>
parents: 638
diff changeset
   117
                                       messageProperties)