app/soc/logic/helper/notifications.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 06 Dec 2008 14:23:53 +0000
changeset 679 77a286ff6667
parent 664 fb4c825b24ab
child 750 6e8d67d04507
permissions -rw-r--r--
Introduce dynamic scope_path regexps Instead of relying on scope_path's being "one slash deep", we should instead allow for either: 1. scope_paths that have a pre-defined depth 2. scope_paths that can be arbitrarily deep We achieve 1 by setting an entities scope_logic to another logic module. We then recursively call getScopeDepth until we get to the topmost entity (that is, an unscoped entity). A little different is the solution to 2, since some entities can have an arbitrarily deep scope (such as Documents), we need to have some way of signaling this to getScopePattern. A clean solution is to return None, rather than a number. If None is returned, the SCOPE_PATH_ARG_PATTERN is returned as regexp instead, which will match an arbitrarily deeply nested scope. The solution for 2 requires that we return None somewhere in the scope_logic chain, the most straight forward method to do so is to override getScopeDepth anywhere such a scope is needed and make it return None. A more elegant solution however, is to set the scope_logic to that module in all entities that require it. Patch by: Sverre Rabbelier

#!/usr/bin/python2.5
#
# Copyright 2008 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Helper functions for sending out notifications.
"""

__authors__ = [
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
  ]


import os

from google.appengine.api import users

from django.utils.translation import ugettext_lazy

from soc.logic import mail_dispatcher
from soc.views.helper import redirects

import soc.logic.models as model_logic


DEF_INVITATION_MSG_FMT = ugettext_lazy(
    "Invitation to become a %(role)s for %(group)s")

DEF_WELCOME_MSG_FMT = ugettext_lazy("Welcome to Melange %(name)s")

def sendInviteNotification(entity):
  """Sends out an invite notification to the user the request is for.

  Args:
    entity : A request containing the information needed to create the message
  """
  
  # get user logic
  user_logic = model_logic.user

  # get the current user
  properties = {'account': users.get_current_user()}
  current_user_entity = user_logic.logic.getForFields(properties, unique=True)
  
  # get the user the request is for
  properties = {'link_id': entity.link_id }
  request_user_entity = user_logic.logic.getForFields(properties, unique=True)

  # create the invitation_url
  invitation_url = "%(host)s%(index)s" % {
      'host' : os.environ['HTTP_HOST'], 
      'index': redirects.inviteAcceptedRedirect(entity, None)}

  # get the group entity
  group_entity = entity.scope

  messageProperties = {
      'to_name': request_user_entity.name,
      'sender_name': current_user_entity.name,
      'role': entity.role,
      'group': group_entity.name,
      'invitation_url': invitation_url,
      'to': request_user_entity.account.email(),
      'sender': current_user_entity.account.email(),
      'subject': DEF_INVITATION_MSG_FMT % {
          'role': entity.role,
          'group': group_entity.name
          },
      }

  # send out the message using the default invitation template    
  mail_dispatcher.sendMailFromTemplate('soc/mail/invitation.html', 
                                       messageProperties)
  
def sendWelcomeMessage(user_entity):
  """Sends out a welcome message to a user.

    Args:
      user_entity: User entity which the message should be send to
  """
  
  # get user logic
  user_logic = model_logic.user  
  
  # get the current user
  properties = {'account': users.get_current_user()}
  current_user_entity = user_logic.logic.getForFields(properties, unique=True)

  # TODO(Lennard): change the message sender to some sort of no-reply adress that is
  # probably a setting in sitesettings. (adress must be a developer). This is due
  # to a GAE limitation that allows only devs or the current user to send an email.
  # Currently this results in a user receiving the same email twice.
  
  # create the message contents
  messageProperties = {
      'to_name': user_entity.name,
      'sender_name': current_user_entity.name,
      'to': user_entity.account.email(),
      'sender': current_user_entity.account.email(),
      'subject': DEF_WELCOME_MSG_FMT % {
          'name': user_entity.name
          }
      } 

  # send out the message using the default welcome template    
  mail_dispatcher.sendMailFromTemplate('soc/mail/welcome.html', 
                                       messageProperties)