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