app/soc/logic/helper/notifications.py
author Sverre Rabbelier <srabbelier@gmail.com>
Thu, 08 Jan 2009 21:24:36 +0000
changeset 792 3cc6bea5c632
parent 784 23eaf3aa19b1
child 818 ddd102e82107
permissions -rw-r--r--
Fixed line-ending and whitespace damage 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
#
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
     3
# Copyright 2008 the Melange authors.
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
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    32
from django.utils.translation import ugettext_lazy
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    33
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    34
from soc.logic import mail_dispatcher
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    35
from soc.views.helper import redirects
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    36
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    37
import soc.views.models as model_view
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    38
import soc.logic.models as model_logic
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
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    41
DEF_NEW_NOTIFICATION_MSG = ugettext_lazy(
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    42
  "You have received a new Notification.")
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    43
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    44
DEF_INVITATION_MSG_FMT = ugettext_lazy(
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    45
    "Invitation to become a %(role)s for %(group)s.")
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    46
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    47
DEF_WELCOME_MSG_FMT = ugettext_lazy("Welcome to Melange %(name)s,")
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    48
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    49
def sendInviteNotification(entity):
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    50
  """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
    51
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    52
  Args:
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    53
    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
    54
  """
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    55
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    56
  # get user logic
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    57
  user_logic = model_logic.user.logic
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    58
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    59
  # get the current user
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    60
  current_user_entity = user_logic.getForCurrentAccount()
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    61
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    62
  # get the user the request is for
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    63
  properties = {'link_id': entity.link_id }
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    64
  request_user_entity = user_logic.getForFields(properties, unique=True)
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
  # create the invitation_url
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    67
  invitation_url = "http://%(host)s%(index)s" % {
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    68
      'host' : os.environ['HTTP_HOST'],
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    69
      'index': redirects.inviteAcceptedRedirect(entity, None)}
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
  # get the group entity
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    72
  group_entity = entity.scope
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    73
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    74
  # create the properties for the message
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    75
  messageProperties = {
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    76
      'to_name': request_user_entity.name,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    77
      'sender_name': current_user_entity.name,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    78
      'role': entity.role,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    79
      'group': group_entity.name,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    80
      'invitation_url': invitation_url,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    81
      }
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
  # render the message
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    84
  message = loader.render_to_string(
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    85
      'soc/notification/messages/invitation.html',
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    86
      dictionary=messageProperties)
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    87
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    88
  # create the fields for the notification
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    89
  fields = {
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    90
      'from_user' : current_user_entity,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    91
      'subject' : DEF_INVITATION_MSG_FMT % {
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    92
          'role' : entity.role,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    93
          'group' : group_entity.name
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    94
          },
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    95
      'message' : message,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    96
      'scope' : request_user_entity,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    97
      'link_id' :'%i' % (time.time()),
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    98
      'scope_path' : request_user_entity.link_id
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
    99
  }
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   100
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   101
  # create and put a new notification in the datastore
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   102
  notification_logic = model_logic.notification.logic
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   103
  notification_logic.updateOrCreateFromFields(fields,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   104
      notification_logic.getKeyFieldsFromDict(fields))
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   105
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   106
def sendNewNotificationMessage(notification_entity):
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   107
  """Sends an email to a user about a new notification
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   108
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   109
    Args:
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   110
      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
   111
  """
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   112
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   113
  # get user logic
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   114
  user_logic = model_logic.user
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   115
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   116
  # get the current user
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   117
  current_user_entity = user_logic.logic.getForCurrentAccount()
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   118
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   119
  # create the url to show this notification
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   120
  notification_url = "http://%(host)s%(index)s" % {
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   121
      'host' : os.environ['HTTP_HOST'],
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   122
      'index': redirects.getPublicRedirect(notification_entity,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   123
          model_view.notification.view.getParams())}
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   124
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   125
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   126
  # 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
   127
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   128
  # create the message contents
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   129
  messageProperties = {
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   130
      'to_name': notification_entity.scope.name,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   131
      'sender_name': current_user_entity.name,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   132
      'to': notification_entity.scope.account.email(),
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   133
      'sender': current_user_entity.account.email(),
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   134
      'subject': force_unicode(DEF_NEW_NOTIFICATION_MSG),
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   135
      'notification' : notification_entity,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   136
      'notification_url' : notification_url
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   137
      }
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   138
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   139
  # 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
   140
  mail_dispatcher.sendMailFromTemplate('soc/mail/new_notification.html',
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   141
                                       messageProperties)
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   142
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   143
def sendWelcomeMessage(user_entity):
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   144
  """Sends out a welcome message to a user.
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   145
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   146
    Args:
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   147
      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
   148
  """
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   149
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   150
  # get user logic
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   151
  user_logic = model_logic.user
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   152
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   153
  # get the current user
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   154
  current_user_entity = user_logic.logic.getForCurrentAccount()
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   155
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   156
  # 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
   157
  # 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
   158
  # 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
   159
  # 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
   160
  # email twice.
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   161
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   162
  # create the message contents
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   163
  messageProperties = {
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   164
      'to_name': user_entity.name,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   165
      'sender_name': current_user_entity.name,
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   166
      'to': user_entity.account.email(),
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   167
      'sender': current_user_entity.account.email(),
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   168
      'subject': DEF_WELCOME_MSG_FMT % {
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   169
          'name': user_entity.name
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
      }
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   172
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   173
  # 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
   174
  mail_dispatcher.sendMailFromTemplate('soc/mail/welcome.html',
3cc6bea5c632 Fixed line-ending and whitespace damage
Sverre Rabbelier <srabbelier@gmail.com>
parents: 784
diff changeset
   175
                                       messageProperties)