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 """Views for Requests.
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>',
22 '"Lennard de Rijk" <ljvderijk@gmail.com>',
23 '"Pawel Solyga" <pawel.solyga@gmail.com>',
27 from django
import forms
28 from django
import http
29 from django
.utils
.translation
import ugettext
31 from soc
.logic
import cleaning
32 from soc
.logic
import dicts
33 from soc
.logic
.models
import user
as user_logic
34 from soc
.views
import helper
35 from soc
.views
.helper
import access
36 from soc
.views
.helper
import decorators
37 from soc
.views
.helper
import dynaform
38 from soc
.views
.helper
import redirects
39 from soc
.views
.helper
import responses
40 from soc
.views
.helper
import widgets
41 from soc
.views
.models
import base
43 import soc
.models
.request
44 import soc
.logic
.models
.request
45 import soc
.logic
.dicts
46 import soc
.views
.helper
47 import soc
.views
.helper
.lists
48 import soc
.views
.helper
.responses
51 class View(base
.View
):
52 """View methods for the Request model.
55 def __init__(self
, params
=None):
56 """Defines the fields and methods required for the base View class
57 to provide the user with list, public, create, edit and delete views.
60 params: a dict with params for this View
63 rights
= access
.Checker(params
)
64 rights
['listSelf'] = ['checkIsUser']
65 rights
['create'] = ['deny']
66 rights
['edit'] = ['checkIsDeveloper']
67 rights
['process_invite'] = ['checkIsMyGroupAcceptedRequest']
68 rights
['list'] = ['checkIsDeveloper']
69 rights
['delete'] = ['checkIsDeveloper']
70 rights
['roles'] = ['checkIsUser']
73 new_params
['rights'] = rights
74 new_params
['logic'] = soc
.logic
.models
.request
.logic
76 new_params
['name'] = "Request"
78 new_params
['sidebar_defaults'] = [('/%s/list', 'List %(name_plural)s',
81 new_params
['create_template'] = ['soc/request/create.html']
83 new_params
['extra_dynaexclude'] = ['status', 'role_verbose', 'created_on']
85 new_params
['create_extra_dynaproperties'] = {
86 'link_id': widgets
.ReferenceField(reference_url
='user'),
87 'role': forms
.CharField(widget
=widgets
.ReadOnlyInput(),
89 'clean_link_id': cleaning
.clean_existing_user('link_id'),
92 new_params
['edit_extra_dynaproperties'] = {
93 'scope_path': forms
.CharField(widget
=forms
.HiddenInput
,
97 patterns
= [(r
'^%(url_name)s/(?P<access_type>process_invite)/'
99 'soc.views.models.%(module_name)s.process_invite',
100 'Process Invite to become')]
102 new_params
['extra_django_patterns'] = patterns
104 new_params
['invite_processing_template'] = 'soc/request/process_invite.html'
105 new_params
['request_processing_template'] = \
106 'soc/request/process_request.html'
108 params
= dicts
.merge(params
, new_params
)
110 super(View
, self
).__init
__(params
=params
)
112 # create and store the special forms for invite and requests
113 params
['invite_form'] = params
['create_form']
116 'link_id': forms
.CharField(widget
=widgets
.ReadOnlyInput(),
118 'group_id': forms
.CharField(widget
=widgets
.ReadOnlyInput(),
121 request_form
= dynaform
.extendDynaForm(
122 dynaform
= self
._params
['create_form'],
123 dynaproperties
= updated_fields
)
125 params
['request_form'] = request_form
128 @decorators.merge_params
129 @decorators.check_access
130 def processInvite(self
, request
, access_type
,
131 page_name
=None, params
=None, **kwargs
):
132 """Creates the page upon which an invite can be processed.
135 request: the standard Django HTTP request object
136 access_type : the name of the access type which should be checked
137 page_name: the page name displayed in templates as page and header title
138 params: a dict with params for this View
139 kwargs: the Key Fields for the specified entity
142 # get the context for this webpage
143 context
= responses
.getUniversalContext(request
)
144 helper
.responses
.useJavaScript(context
, params
['js_uses_all'])
146 request_logic
= params
['logic']
148 # get the request entity using the information from kwargs
149 fields
= {'link_id': kwargs
['link_id'],
150 'scope_path': kwargs
['scope_path'],
151 'role': kwargs
['role'],
152 'status': 'group_accepted'}
153 request_entity
= request_logic
.getForFields(fields
, unique
=True)
155 # set the page name using the request_entity
156 context
['page_name'] = '%s %s' % (page_name
,
157 request_entity
.role_verbose
)
159 get_dict
= request
.GET
161 if 'status' in get_dict
.keys():
162 if get_dict
['status'] == 'rejected':
163 # this invite has been rejected mark as rejected
164 request_logic
.updateEntityProperties(request_entity
, {
165 'status': 'rejected'})
167 # redirect to user role overview
168 return http
.HttpResponseRedirect('/user/roles')
170 # put the entity in the context
171 context
['entity'] = request_entity
172 context
['module_name'] = params
['module_name']
173 context
['invite_accepted_redirect'] = (
174 redirects
.getInviteAcceptedRedirect(request_entity
, self
._params
))
176 #display the invite processing page using the appropriate template
177 template
= params
['invite_processing_template']
179 return responses
.respond(request
, template
, context
=context
)
181 @decorators.merge_params
182 @decorators.check_access
183 def listSelf(self
, request
, access_type
,
184 page_name
=None, params
=None, **kwargs
):
185 """Displays the unhandled requests for this user.
188 request: the standard Django HTTP request object
189 access_type : the name of the access type which should be checked
190 page_name: the page name displayed in templates as page and header title
191 params: a dict with params for this View
195 # get the current user
196 user_entity
= user_logic
.logic
.getForCurrentAccount()
198 # construct the Unhandled Invites list
200 # only select the Invites for this user that haven't been handled yet
201 filter = {'link_id': user_entity
.link_id
,
202 'status': 'group_accepted'}
204 uh_params
= params
.copy()
205 uh_params
['list_action'] = (redirects
.getInviteProcessRedirect
, None)
206 uh_params
['list_description'] = ugettext(
207 "An overview of your unhandled invites.")
209 uh_list
= helper
.lists
.getListContent(
210 request
, uh_params
, filter, idx
=0)
212 # construct the Open Requests list
214 # only select the requests from the user
215 # that haven't been accepted by an admin yet
216 filter = {'link_id': user_entity
.link_id
,
219 ar_params
= params
.copy()
220 ar_params
['list_description'] = ugettext(
221 "List of your pending requests.")
223 ar_list
= helper
.lists
.getListContent(
224 request
, ar_params
, filter, idx
=1)
226 # fill contents with all the needed lists
227 contents
= [uh_list
, ar_list
]
229 # call the _list method from base to display the list
230 return self
._list
(request
, params
, contents
, page_name
)
235 admin
= decorators
.view(view
.admin
)
236 create
= decorators
.view(view
.create
)
237 edit
= decorators
.view(view
.edit
)
238 delete
= decorators
.view(view
.delete
)
239 list = decorators
.view(view
.list)
240 list_self
= decorators
.view(view
.listSelf
)
241 process_invite
= decorators
.view(view
.processInvite
)
242 public
= decorators
.view(view
.public
)
243 export
= decorators
.view(view
.export
)