Style fixes and removal of unused imports in soc.views.models.
[Melange.git] / app / soc / views / models / notification.py
blobbeac96396a485f86b7c9d1c23098d1445e1d2470
1 #!/usr/bin/python2.5
3 # Copyright 2008 the Melange authors.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 """This module contains the view code for Notifications.
18 """
20 __authors__ = [
21 '"Lennard de Rijk" <ljvderijk@gmail.com>',
25 import time
27 from django import forms
28 from django.utils.translation import ugettext
30 from soc.logic import cleaning
31 from soc.logic import dicts
32 from soc.models import notification as notification_model
33 from soc.views import helper
34 from soc.views.helper import access
35 from soc.views.helper import decorators
36 from soc.views.helper import lists as list_helper
37 from soc.views.helper import redirects
38 from soc.views.models import base
39 from soc.logic.models.notification import logic as notification_logic
40 from soc.logic.models.user import logic as user_logic
43 class CreateForm(helper.forms.BaseForm):
44 """Form for creating a Notification.
45 """
47 # to user field
48 to_user = forms.fields.CharField(label='To User')
50 def __init__(self, *args, **kwargs):
51 """ Calls super and then redefines the order in which the fields appear.
53 for parameters see BaseForm.__init__()
54 """
55 super(CreateForm, self).__init__(*args, **kwargs)
57 # set form fields order
58 self.fields.keyOrder = ['to_user', 'subject', 'message']
60 class Meta:
61 """Inner Meta class that defines some behavior for the form.
62 """
63 model = notification_model.Notification
65 # exclude the necessary fields from the form
66 exclude = ['link_id', 'scope', 'scope_path', 'from_user', 'unread']
68 clean_to_user = cleaning.clean_existing_user('to_user')
71 class View(base.View):
72 """View methods for the Notification model.
73 """
75 def __init__(self, params=None):
76 """Defines the fields and methods required for the base View class
77 to provide the user with list, public, create, edit and delete views.
79 Params:
80 params: a dict with params for this View
81 """
83 rights = access.Checker(params)
84 rights['unspecified'] = ['deny']
85 rights['edit'] = ['deny']
86 rights['show'] = [('checkIsMyEntity', [notification_logic, 'scope_path'])]
87 rights['delete'] = [('checkIsMyEntity', [notification_logic, 'scope_path'])]
88 rights['list'] = ['checkIsUser']
89 # create is developer only for the time being to test functionality
90 rights['create'] = ['checkIsDeveloper']
92 new_params = {}
93 new_params['logic'] = notification_logic
94 new_params['rights'] = rights
96 new_params['name'] = "Notification"
98 new_params['no_create_with_key_fields'] = True
99 new_params['create_form'] = CreateForm
101 new_params['edit_redirect'] = '/%(url_name)s/list'
103 params = dicts.merge(params, new_params)
105 super(View, self).__init__(params=params)
107 @decorators.merge_params
108 @decorators.check_access
109 def list(self, request, access_type,
110 page_name=None, params=None, filter=None, order=None, **kwargs):
111 """Lists all notifications that the current logged in user has stored.
113 for parameters see base.list()
116 # get the current user
117 user_entity = user_logic.getForCurrentAccount()
119 # only select the notifications for this user so construct a filter
120 filter = {
121 'scope': user_entity,
122 'unread': True,
125 # create the list parameters
126 un_params = params.copy() # unread notifications
128 # define the list redirect action to show the notification
129 un_params['list_action'] = (redirects.getPublicRedirect, params)
130 un_params['list_description'] = ugettext(
131 "An overview of your unread Notifications.")
133 # TODO(Lennard) when list sorting is implemented sort on descending date
134 un_list = list_helper.getListContent(
135 request, un_params, filter, idx=0)
137 # Now get the read list
139 # Reuse the filter, but only for read notifications
140 filter['unread'] = False
142 rn_params = params.copy() # read notifications
144 rn_params['list_action'] = (redirects.getPublicRedirect, params)
145 rn_params['list_description'] = ugettext(
146 "An overview of your read Notifications.")
148 rn_list = list_helper.getListContent(
149 request, rn_params, filter, idx=1)
151 # fill contents with all the needed lists
152 contents = [un_list, rn_list]
154 # call the _list method from base to display the list
155 return self._list(request, params, contents, page_name)
157 def _editPost(self, request, entity, fields):
158 """See base.View._editPost().
161 # get the current user
162 current_user = user_logic.getForCurrentAccount()
164 fields['link_id'] = 't%i' % (int(time.time()*100))
165 fields['scope'] = fields['to_user']
166 fields['from_user'] = current_user
167 fields['scope_path'] = fields['to_user'].link_id
169 def _editSeed(self, request, seed):
170 """Checks if scope_path is seeded and puts it into to_user.
172 for parameters see base._editSeed()
175 # if scope_path is present
176 if 'scope_path' in seed.keys():
177 # fill the to_user field with the scope path
178 seed['to_user'] = seed['scope_path']
180 def _public(self, request, entity, context):
181 """Marks the Notification as read if that hasn't happened yet.
183 for parameters see base._public()
186 # if the user viewing is the user for which this notification is meant
187 # and the notification has not been read yet
188 if entity.unread:
189 # get the current user
190 user = user_logic.getForCurrentAccount()
192 # if the message is meant for the user that is reading it
193 if entity.scope.key() == user.key():
194 # mark the entity as read
195 self._logic.updateEntityProperties(entity, {'unread' : False} )
197 context['entity_type_url'] = self._params['url_name']
198 context['entity_suffix'] = self._logic.getKeySuffix(entity)
200 return True
203 view = View()
205 admin = decorators.view(view.admin)
206 create = decorators.view(view.create)
207 edit = decorators.view(view.edit)
208 delete = decorators.view(view.delete)
209 list = decorators.view(view.list)
210 public = decorators.view(view.public)
211 export = decorators.view(view.export)