app/soc/views/models/notification.py
author Sverre Rabbelier <srabbelier@gmail.com>
Sat, 13 Dec 2008 13:09:06 +0000
changeset 734 1ebd40380aa5
parent 733 78fc6080ea13
child 736 c70d56182ce2
permissions -rw-r--r--
Make the place of the rights declaration consistent Other code places it at the top, so we should do that in notification as well. Patch by: Sverre Rabbelier
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
732
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
     1
#!/usr/bin/python2.5
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
     2
#
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
     3
# Copyright 2008 the Melange authors.
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
     4
#
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
     5
# Licensed under the Apache License, Version 2.0 (the "License");
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
     6
# you may not use this file except in compliance with the License.
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
     7
# You may obtain a copy of the License at
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
     8
#
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
     9
#   http://www.apache.org/licenses/LICENSE-2.0
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    10
#
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    11
# Unless required by applicable law or agreed to in writing, software
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    12
# distributed under the License is distributed on an "AS IS" BASIS,
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    14
# See the License for the specific language governing permissions and
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    15
# limitations under the License.
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    16
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    17
"""This module contains the view code for Notifications
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    18
"""
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    19
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    20
__authors__ = [
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    21
  '"Lennard de Rijk" <ljvderijk@gmail.com>',
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    22
]
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    23
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    24
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    25
import time
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    26
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    27
from google.appengine.api import users
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    28
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    29
from django import forms
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    30
from django import http
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    31
from django.utils.translation import ugettext_lazy
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    32
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    33
from soc.logic import dicts
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    34
from soc.logic import validate
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    35
from soc.models import notification as notification_model
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    36
from soc.views import helper
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    37
from soc.views import out_of_band
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    38
from soc.views.helper import access
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    39
from soc.views.helper import redirects
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    40
from soc.views.models import base
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    41
from soc.logic.models import notification as notification_logic
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    42
from soc.logic.models import user as user_logic
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    43
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    44
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    45
class CreateForm(helper.forms.BaseForm):
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    46
  """Form for creating a Notification.
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    47
  """
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    48
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    49
  # to user field
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    50
  to_user = forms.fields.CharField(label='To User')
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    51
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    52
  def __init__(self, *args, **kwargs):
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    53
    """ Calls super and then redefines the order in which the fields appear.
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    54
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    55
    for parameters see BaseForm.__init__()
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    56
    """
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    57
    super(CreateForm, self).__init__(*args, **kwargs)
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    58
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    59
    # set form fields order
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    60
    self.fields.keyOrder = ['to_user', 'subject', 'message']
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    61
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    62
  class Meta:
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    63
    model = notification_model.Notification
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    64
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    65
    # exclude the necessary fields from the form
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    66
    exclude = ['link_id', 'scope', 'scope_path', 'from_user', 'has_been_read']
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    67
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    68
  def clean_to_user(self):
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    69
    """Check if the to_user field has been filled in correctly.
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    70
    """
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    71
    link_id = self.cleaned_data.get('to_user').lower()
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    72
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    73
    if not validate.isLinkIdFormatValid(link_id):
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    74
      raise forms.ValidationError("This link ID is in wrong format.")
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    75
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    76
    to_user = user_logic.logic.getForFields({'link_id' : link_id}, unique=True)
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    77
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    78
    if not to_user:
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    79
      # user does not exist
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    80
      raise forms.ValidationError("This user does not exist")
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    81
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    82
    return link_id
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    83
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    84
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    85
class View(base.View):
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    86
  """View methods for the Notification model.
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    87
  """
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    88
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    89
  def __init__(self, params=None):
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    90
    """Defines the fields and methods required for the base View class
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    91
    to provide the user with list, public, create, edit and delete views.
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    92
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    93
    Params:
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    94
      params: a dict with params for this View
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    95
    """
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    96
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    97
    rights = {}
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
    98
    rights['unspecified'] = [access.deny]
733
78fc6080ea13 Remove custom override of django_patterns_defaults
Sverre Rabbelier <srabbelier@gmail.com>
parents: 732
diff changeset
    99
    rights['edit'] = [access.deny]
732
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   100
    rights['show'] = [access.checkIsMyNotification]
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   101
    rights['delete'] = [access.checkIsDeveloper]
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   102
    rights['list'] = [access.checkIsUser]
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   103
    # create is developer only for the time being to test functionality
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   104
    rights['create'] = [access.checkIsDeveloper]
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   105
734
1ebd40380aa5 Make the place of the rights declaration consistent
Sverre Rabbelier <srabbelier@gmail.com>
parents: 733
diff changeset
   106
    new_params = {}
1ebd40380aa5 Make the place of the rights declaration consistent
Sverre Rabbelier <srabbelier@gmail.com>
parents: 733
diff changeset
   107
    new_params['logic'] = notification_logic.logic
732
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   108
    new_params['rights'] = rights
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   109
734
1ebd40380aa5 Make the place of the rights declaration consistent
Sverre Rabbelier <srabbelier@gmail.com>
parents: 733
diff changeset
   110
    new_params['name'] = "Notification"
1ebd40380aa5 Make the place of the rights declaration consistent
Sverre Rabbelier <srabbelier@gmail.com>
parents: 733
diff changeset
   111
    new_params['name_short'] = "Notification"
1ebd40380aa5 Make the place of the rights declaration consistent
Sverre Rabbelier <srabbelier@gmail.com>
parents: 733
diff changeset
   112
    new_params['name_plural'] = "Notifications"
1ebd40380aa5 Make the place of the rights declaration consistent
Sverre Rabbelier <srabbelier@gmail.com>
parents: 733
diff changeset
   113
    new_params['url_name'] = "notification"
1ebd40380aa5 Make the place of the rights declaration consistent
Sverre Rabbelier <srabbelier@gmail.com>
parents: 733
diff changeset
   114
    new_params['module_name'] = "notification"
1ebd40380aa5 Make the place of the rights declaration consistent
Sverre Rabbelier <srabbelier@gmail.com>
parents: 733
diff changeset
   115
1ebd40380aa5 Make the place of the rights declaration consistent
Sverre Rabbelier <srabbelier@gmail.com>
parents: 733
diff changeset
   116
    new_params['create_form'] = CreateForm
1ebd40380aa5 Make the place of the rights declaration consistent
Sverre Rabbelier <srabbelier@gmail.com>
parents: 733
diff changeset
   117
732
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   118
    params = dicts.merge(params, new_params)
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   119
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   120
    super(View, self).__init__(params=params)
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   121
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   122
  def create(self, request, access_type,
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   123
             page_name=None, params=None, **kwargs):
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   124
    """On a successful post create redirects the user to the notification list.
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   125
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   126
    for parameters see base.create()
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   127
    """
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   128
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   129
    if request.method == 'POST':
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   130
      response = super(View, self).create(request, access_type,
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   131
        page_name, params, **kwargs)
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   132
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   133
      if (response.__class__ == http.HttpResponseRedirect and
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   134
          response['location'].startswith(
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   135
              '/%s/edit/' %(self._params['url_name']))):
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   136
        # redirect to list instead of edit view
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   137
        return http.HttpResponseRedirect('/%s/list' %(self._params['url_name']))
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   138
      else:
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   139
        return response
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   140
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   141
    else:
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   142
      # request.method == 'GET' so act normal
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   143
      return super(View, self).create(request, access_type,
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   144
          page_name, params, **kwargs)
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   145
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   146
  def list(self, request, access_type,
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   147
           page_name=None, params=None, seed=None, **kwargs):
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   148
    """Lists all notifications that the current logged in user has stored.
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   149
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   150
    for parameters see base.list()
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   151
    """
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   152
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   153
    params = dicts.merge(params, self._params)
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   154
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   155
    # get the current user
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   156
    user_entity = user_logic.logic.getForCurrentAccount()
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   157
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   158
    # only select the notifications for this user so construct a filter
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   159
    filter = {'scope': user_entity}
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   160
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   161
    # create the list parameters
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   162
    list_params = params.copy()
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   163
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   164
    # define the list redirect action to show the notification
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   165
    list_params['list_action'] = (redirects.getPublicRedirect, params)
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   166
    list_params['list_description'] = ugettext_lazy(
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   167
        "An overview of your received Notifications.")
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   168
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   169
    # TODO(Lennard) when list sorting is implemented sort on descending date
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   170
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   171
    # use the generic list method with the filter. The access check in this
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   172
    # method will trigger an errorResponse when user_entity is None
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   173
    return super(View, self).list(request, access_type,
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   174
        page_name, list_params, filter)
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   175
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   176
  def _editPost(self, request, entity, fields):
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   177
    """See base.View._editPost().
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   178
    """
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   179
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   180
    # get the current user
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   181
    current_user = user_logic.logic.getForCurrentAccount()
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   182
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   183
    to_user = user_logic.logic.getForFields(
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   184
        {'link_id' : fields['to_user']}, unique=True)
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   185
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   186
    fields['link_id'] = '%i' %(time.time())
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   187
    fields['scope'] = to_user
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   188
    fields['from_user'] = current_user
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   189
    fields['scope_path'] = fields['to_user']
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   190
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   191
  def _editSeed(self, request, seed):
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   192
    """Checks if scope_path is seeded and puts it into to_user.
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   193
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   194
    for parameters see base._editSeed()
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   195
    """
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   196
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   197
    # if scope_path is present
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   198
    if 'scope_path' in seed.keys():
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   199
      # fill the to_user field with the scope path
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   200
      seed['to_user'] = seed['scope_path']
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   201
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   202
  def _public(self, request, entity, context):
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   203
    """Marks the Notification as read if that hasn't happened yet.
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   204
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   205
    for parameters see base._public()
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   206
    """
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   207
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   208
    # if the user viewing is the user for which this notification is meant
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   209
    # and the notification has not been read yet
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   210
    if not entity.has_been_read:
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   211
      # get the current user
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   212
      user = user_logic.logic.getForCurrentAccount()
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   213
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   214
      if entity.scope.key() == user.key():
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   215
        # mark the entity as read
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   216
        self._logic.updateModelProperties(entity, {'has_been_read' : True} )
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   217
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   218
    context['entity_type_url'] = self._params['url_name']
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   219
    context['entity_suffix'] = self._logic.getKeySuffix(entity)
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   220
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   221
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   222
view = View()
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   223
create = view.create
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   224
edit = view.edit
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   225
delete = view.delete
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   226
list = view.list
0a8e60228311 Fix whitespace and EOL damage in notifcation.py
Sverre Rabbelier <srabbelier@gmail.com>
parents: 727
diff changeset
   227
public = view.public