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