Style fixes and removal of unused imports in soc.views.models.
[Melange.git] / app / soc / views / models / group_app.py
blob17be3cf6775bb46ce66ec88a13681c5a1bee0b23
1 #!/usr/bin/python2.5
3 # Copyright 2009 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 """Views for Group App.
18 """
20 __authors__ = [
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
22 '"Lennard de Rijk" <ljvderijk@gmail.com>',
26 from django import http
27 from django.utils.translation import ugettext
29 from soc.logic import cleaning
30 from soc.logic import dicts
31 from soc.logic.helper import notifications
32 from soc.logic.models.group_app import logic as group_app_logic
33 from soc.logic.models.user import logic as user_logic
34 from soc.views import out_of_band
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.helper import responses
39 from soc.views.helper import widgets
40 from soc.views.models import base
43 DEF_APPLICATION_LIST_DESCRIPTION_FMT = ugettext(
44 'Overview of %(name_plural)s whose status is "%(status)s"')
47 class View(base.View):
48 """View methods for the Group App model.
49 """
51 def __init__(self, params=None):
52 """Defines the fields and methods required for the base View class
53 to provide the user with list, public, create, edit and delete views.
55 Params:
56 params: a dict with params for this View
57 """
59 new_params = {}
60 new_params['logic'] = group_app_logic
62 new_params['name'] = "Group Application"
63 new_params['name_short'] = "Group App"
65 # use the twoline templates for these questionnaires
66 new_params['create_template'] = 'soc/models/twoline_edit.html'
67 new_params['edit_template'] = 'soc/models/twoline_edit.html'
69 patterns = [(r'^%(url_name)s/(?P<access_type>list_self)/%(scope)s$',
70 'soc.views.models.%(module_name)s.list_self',
71 'List my %(name_plural)s'),
72 (r'^%(url_name)s/(?P<access_type>review_overview)/%(scope)s$',
73 'soc.views.models.%(module_name)s.review_overview',
74 'List of %(name_plural)s for reviewing'),
75 (r'^%(url_name)s/(?P<access_type>review)/%(key_fields)s$',
76 'soc.views.models.%(module_name)s.review',
77 'Review %(name_short)s')]
79 new_params['extra_django_patterns'] = patterns
81 new_params['extra_dynaexclude'] = ['applicant', 'backup_admin', 'status',
82 'created_on', 'last_modified_on']
84 new_params['create_dynafields'] = [
85 {'name': 'backup_admin_link_id',
86 'base': widgets.ReferenceField,
87 'passthrough': ['reference_url', 'required', 'label'],
88 'reference_url': 'user',
89 'required': False,
90 'label': params['logic'].getModel().backup_admin.verbose_name,
91 'example_text': ugettext('The link_id of the backup admin'),
95 new_params['create_extra_dynaproperties'] = {
96 'clean_backup_admin_link_id':
97 cleaning.clean_users_not_same('backup_admin_link_id'),
100 new_params['edit_extra_dynaproperties'] = {
101 'clean_link_id' : cleaning.clean_link_id('link_id'),
104 params = dicts.merge(params, new_params, sub_merge=True)
106 super(View, self).__init__(params=params)
109 def _editGet(self, request, entity, form):
110 """See base.View._editGet().
113 if entity.backup_admin:
114 form.fields['backup_admin_link_id'].initial = entity.backup_admin.link_id
116 super(View, self)._editGet(request, entity, form)
118 def _editPost(self, request, entity, fields):
119 """See base.View._editPost().
122 if not entity:
123 # set the applicant field to the current user
124 fields['applicant'] = user_logic.getForCurrentAccount()
126 #set the backup_admin field with the cleaned link_id
127 fields['backup_admin'] = fields['backup_admin_link_id']
129 # the application has either been created or edited so
130 # the status needs to be set accordingly
131 fields['status'] = 'needs review'
133 super(View, self)._editPost(request, entity, fields)
136 @decorators.merge_params
137 @decorators.check_access
138 def list(self, request, access_type,
139 page_name=None, params=None, filter=None, order=None, **kwargs):
140 """Lists all notifications in separate tables, depending on their status.
142 for parameters see base.list()
145 # create the selection list
146 selection = [('needs review', (redirects.getEditRedirect, params)),
147 ('pre-accepted', (redirects.getEditRedirect, params)),
148 ('accepted', (redirects.getEditRedirect, params)),
149 ('pre-rejected', (redirects.getEditRedirect, params)),
150 ('rejected', (redirects.getEditRedirect, params)),
151 ('ignored', (redirects.getEditRedirect, params)),]
153 return self._applicationListConstructor(request, params, page_name,
154 filter=filter, selection=selection, **kwargs)
157 def _applicationListConstructor(self, request, params, page_name, filter={},
158 selection=[], **kwargs):
159 """Constructs the list containing applications for the given the arguments.
161 Args:
162 filter: This is the filter used for all application
163 selection: This is a list containing tuples stating the status for an
164 application and the redirect action.
165 See base.View.public() for the rest.
167 Returns:
168 HTTP Response containing the list view.
172 contents = []
173 list_params = params.copy()
174 index = 0
176 for status, action in selection:
177 # only select the requests that have been pre-accpeted
178 filter['status'] = status
180 name = status[0] if isinstance(status, list) else status
182 list_params['list_description'] = (
183 DEF_APPLICATION_LIST_DESCRIPTION_FMT % (
184 {'name_plural': params['name_plural'], 'status': name}))
185 list_params['list_action'] = action
187 list_content = list_helper.getListContent(
188 request, list_params, filter, idx=index)
190 contents += [list_content]
192 index += 1
194 # call the _list method from base to display the list
195 if kwargs.get('context'):
196 context = kwargs['context']
197 else:
198 context = {}
200 return self._list(request, params, contents, page_name, context=context)
203 @decorators.merge_params
204 @decorators.check_access
205 def listSelf(self, request, access_type,
206 page_name=None, params=None, **kwargs):
207 """List all applications from the current logged-in user.
209 For params see base.View.public().
212 user_entity = user_logic.getForCurrentAccount()
213 filter = {'applicant' : user_entity}
215 if kwargs['scope_path']:
216 filter['scope_path'] = kwargs['scope_path']
218 # create the selection list
219 selection = [(['needs review', 'pre-accepted', 'pre-rejected'],
220 (redirects.getEditRedirect, params)),
221 ('accepted', (redirects.getApplicantRedirect,
222 {'url_name': params['group_url_name']})),
223 ('rejected', (redirects.getPublicRedirect, params))]
225 return self._applicationListConstructor(request, params, page_name,
226 filter=filter, selection=selection, **kwargs)
228 @decorators.merge_params
229 @decorators.check_access
230 def review(self, request, access_type,
231 page_name=None, params=None, **kwargs):
232 """Handles the view containing the review of an application.
234 accepted (true or false) in the GET data will mark
235 the application accordingly.
238 For params see base.View.public().
241 try:
242 entity = self._logic.getFromKeyFieldsOr404(kwargs)
243 except out_of_band.Error, error:
244 return responses.errorResponse(
245 error, request, template=params['error_public'])
247 get_dict = request.GET
249 # check to see if we can make a decision for this application
250 if 'status' in get_dict.keys():
251 status_value = get_dict['status']
253 if status_value in ['accepted', 'rejected', 'ignored', 'pre-accepted',
254 'pre-rejected']:
255 # this application has been properly reviewed update the status
257 # only update if the status changes
258 if entity.status != status_value:
259 fields = {'status' : status_value}
261 self._logic.updateEntityProperties(entity, fields)
262 self._review(request, params, entity, status_value, **kwargs)
264 if status_value == 'accepted':
265 # the application has been accepted send out a notification
266 notifications.sendNewGroupNotification(entity, params)
268 # redirect to the review overview
269 fields = {'url_name': params['url_name']}
271 scope_path = entity.scope_path
273 if not scope_path:
274 scope_path = ''
276 # add scope_path to the dictionary
277 fields['scope_path'] = scope_path
279 return http.HttpResponseRedirect(
280 '/%(url_name)s/review_overview/%(scope_path)s' %fields)
282 # the application has not been reviewed so show the information
283 # using the appropriate review template
284 params['public_template'] = params['review_template']
286 return super(View, self).public(request, access_type,
287 page_name=page_name, params=params, **kwargs)
290 def _review(self, request, params, app_entity, status, **kwargs):
291 """Does any required post review processing.
293 Args:
294 request: the standard Django HTTP request object
295 params: a dict with params for this View
296 app_entity: The update application entity
297 status: The status that was given to the reviewed app_entity
300 pass
303 @decorators.merge_params
304 @decorators.check_access
305 def reviewOverview(self, request, access_type,
306 page_name=None, params=None, **kwargs):
307 """Displays multiple lists of applications that are in a different
308 status of the application process.
311 selection = [('needs review', (redirects.getReviewRedirect, params)),
312 ('pre-accepted', (redirects.getReviewRedirect, params)),
313 ('accepted', (redirects.getReviewRedirect, params)),
314 ('pre-rejected', (redirects.getReviewRedirect, params)),
315 ('rejected', (redirects.getReviewRedirect, params)),
316 ('ignored', (redirects.getReviewRedirect, params)),]
318 filter = {}
320 if kwargs['scope_path']:
321 filter['scope_path'] = kwargs['scope_path']
323 return self._applicationListConstructor(request, params, page_name,
324 filter=filter, selection=selection, **kwargs)