17 """Views for Club Admins. |
17 """Views for Club Admins. |
18 """ |
18 """ |
19 |
19 |
20 __authors__ = [ |
20 __authors__ = [ |
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
21 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
|
22 '"Lennard de Rijk" <ljvderijk@gmail.com>' |
22 ] |
23 ] |
23 |
24 |
24 |
25 |
|
26 from django import http |
25 from django import forms |
27 from django import forms |
26 |
28 |
27 from soc.logic import cleaning |
29 from soc.logic import cleaning |
28 from soc.logic import dicts |
30 from soc.logic import dicts |
|
31 from soc.logic.models import club as club_logic |
|
32 from soc.logic.models import user as user_logic |
|
33 from soc.logic.models import request as request_logic |
|
34 from soc.views.helper import access |
|
35 from soc.views.helper import decorators |
|
36 from soc.views.helper import dynaform |
29 from soc.views.helper import redirects |
37 from soc.views.helper import redirects |
|
38 from soc.views.helper import responses |
|
39 from soc.views.helper import widgets |
30 from soc.views.models import base |
40 from soc.views.models import base |
31 from soc.views.models import club as club_view |
41 from soc.views.models import club as club_view |
|
42 from soc.views.models import request as request_view |
32 |
43 |
33 import soc.logic.models.club_admin |
44 import soc.logic.models.club_admin |
34 |
45 |
35 |
46 |
36 class View(base.View): |
47 class View(base.View): |
37 """View methods for the Program model. |
48 """View methods for the Club Admin model. |
38 """ |
49 """ |
39 |
50 |
40 def __init__(self, params=None): |
51 def __init__(self, params=None): |
41 """Defines the fields and methods required for the base View class |
52 """Defines the fields and methods required for the base View class |
42 to provide the user with list, public, create, edit and delete views. |
53 to provide the user with list, public, create, edit and delete views. |
43 |
54 |
44 Params: |
55 Params: |
45 params: a dict with params for this View |
56 params: a dict with params for this View |
46 """ |
57 """ |
47 |
58 |
|
59 rights = {} |
|
60 rights['create'] = [access.checkIsHost] |
|
61 rights['edit'] = [access.checkIsMyActiveRole(soc.logic.models.club_admin)] |
|
62 rights['delete'] = [access.checkIsHost] |
|
63 rights['invite'] = [access.checkIsClubAdminForClub] |
|
64 rights['accept_invite'] = [access.checkCanCreateFromRequest('club_admin')] |
|
65 |
48 new_params = {} |
66 new_params = {} |
49 new_params['logic'] = soc.logic.models.club_admin.logic |
67 new_params['logic'] = soc.logic.models.club_admin.logic |
|
68 new_params['rights'] = rights |
50 |
69 |
51 new_params['scope_view'] = club_view |
70 new_params['scope_view'] = club_view |
52 new_params['scope_redirect'] = redirects.getCreateRedirect |
71 new_params['scope_redirect'] = redirects.getCreateRedirect |
53 |
72 |
54 new_params['name'] = "Club Admin" |
73 new_params['name'] = "Club Admin" |
55 |
74 |
56 new_params['extra_dynaexclude'] = ['user', 'org'] |
75 new_params['extra_dynaexclude'] = ['user', 'club', 'state'] |
57 |
76 |
|
77 new_params['create_extra_dynafields'] = { |
|
78 'scope_path': forms.CharField(widget=forms.HiddenInput, |
|
79 required=True), |
|
80 'clean_link_id' : cleaning.clean_existing_user('link_id'), |
|
81 'clean_home_page' : cleaning.clean_url('home_page'), |
|
82 'clean_blog' : cleaning.clean_url('blog'), |
|
83 'clean_photo_url' : cleaning.clean_url('photo_url')} |
|
84 |
|
85 patterns = [(r'^%(url_name)s/(?P<access_type>invite)/%(lnp)s$', |
|
86 'soc.views.models.%(module_name)s.invite', |
|
87 'Create invite for %(name_plural)s'), |
|
88 (r'^%(url_name)s/(?P<access_type>accept_invite)/%(scope)s/%(lnp)s$', |
|
89 'soc.views.models.%(module_name)s.acceptInvite', |
|
90 'Accept invite for %(name_plural)s')] |
|
91 |
|
92 new_params['extra_django_patterns'] = patterns |
|
93 |
58 params = dicts.merge(params, new_params) |
94 params = dicts.merge(params, new_params) |
59 |
95 |
60 super(View, self).__init__(params=params) |
96 super(View, self).__init__(params=params) |
61 |
97 |
|
98 # create and store the special form for invited users |
|
99 updated_fields = { |
|
100 'link_id': forms.CharField(widget=widgets.ReadOnlyInput(), |
|
101 required=False)} |
|
102 |
|
103 invited_create_form = dynaform.extendDynaForm( |
|
104 dynaform = self._params['create_form'], |
|
105 dynafields = updated_fields) |
|
106 |
|
107 params['invited_create_form'] = invited_create_form |
|
108 |
|
109 |
|
110 def _editPost(self, request, entity, fields): |
|
111 """See base.View._editPost(). |
|
112 """ |
|
113 |
|
114 fields['user'] = fields['link_id'] |
|
115 fields['link_id'] = fields['user'].link_id |
|
116 |
|
117 club = club_logic.logic.getFromKeyName(fields['scope_path']) |
|
118 fields['club'] = club |
|
119 |
|
120 super(View, self)._editPost(request, entity, fields) |
|
121 |
|
122 |
|
123 @decorators.merge_params |
|
124 @decorators.check_access |
|
125 def acceptInvite(self, request, access_type, |
|
126 page_name=None, params=None, **kwargs): |
|
127 """Creates the page process an invite into a Club Admin. |
|
128 |
|
129 Args: |
|
130 request: the standard Django HTTP request object |
|
131 access_type : the name of the access type which should be checked |
|
132 context: dictionary containing the context for this view |
|
133 params: a dict with params for this View |
|
134 kwargs: the Key Fields for the specified entity |
|
135 """ |
|
136 |
|
137 # get the context for this webpage |
|
138 context = responses.getUniversalContext(request) |
|
139 context['page_name'] = page_name |
|
140 |
|
141 if request.method == 'POST': |
|
142 return self.acceptInvitePost(request, context, params, **kwargs) |
|
143 else: |
|
144 # request.method == 'GET' |
|
145 return self.acceptInviteGet(request, context, params, **kwargs) |
|
146 |
|
147 def acceptInviteGet(self, request, context, params, **kwargs): |
|
148 """Handles the GET request concerning the creation of a Club Admin via an |
|
149 invite. |
|
150 |
|
151 Args: |
|
152 request: the standard Django HTTP request object |
|
153 context: dictionary containing the context for this view |
|
154 params: a dict with params for this View |
|
155 kwargs: the Key Fields for the specified entity |
|
156 """ |
|
157 |
|
158 # create the form using the scope_path and link_id from kwargs as initial value |
|
159 fields = {'link_id' : kwargs['link_id'], |
|
160 'scope_path' : kwargs['scope_path']} |
|
161 form = params['invited_create_form'](initial=fields) |
|
162 |
|
163 # construct the appropriate response |
|
164 return super(View, self)._constructResponse(request, entity=None, |
|
165 context=context, form=form, params=params) |
|
166 |
|
167 def acceptInvitePost(self, request, context, params, **kwargs): |
|
168 """Handles the POST request concerning the creation of a Club Admin via an |
|
169 invite. |
|
170 |
|
171 Args: |
|
172 request: the standard Django HTTP request object |
|
173 context: dictionary containing the context for this view |
|
174 params: a dict with params for this View |
|
175 kwargs: the Key Fields for the specified entity |
|
176 """ |
|
177 |
|
178 # populate the form using the POST data |
|
179 form = params['invited_create_form'](request.POST) |
|
180 |
|
181 if not form.is_valid(): |
|
182 # return the invalid form response |
|
183 return self._constructResponse(request, entity=None, context=context, |
|
184 form=form, params=params) |
|
185 |
|
186 # collect the cleaned data from the valid form |
|
187 key_name, fields = soc.views.helper.forms.collectCleanedFields(form) |
|
188 |
|
189 # fill in the appropriate fields that were missing in the form |
|
190 fields['user'] = fields['link_id'] |
|
191 fields['link_id'] = fields['user'].link_id |
|
192 |
|
193 club = club_logic.logic.getFromKeyName(fields['scope_path']) |
|
194 fields['club'] = club |
|
195 fields['scope'] = club |
|
196 |
|
197 # make sure that this role becomes active once more in case this user |
|
198 # has been reinvited |
|
199 fields ['state'] = 'active' |
|
200 |
|
201 # get the key_name for the new entity |
|
202 key_fields = self._logic.getKeyFieldsFromDict(fields) |
|
203 key_name = self._logic.getKeyNameForFields(key_fields) |
|
204 |
|
205 # create new Club Admin entity |
|
206 entity = self._logic.updateOrCreateFromKeyName(fields, key_name) |
|
207 |
|
208 # redirect to the roles overview page |
|
209 return http.HttpResponseRedirect('/user/roles') |
|
210 |
|
211 |
|
212 @decorators.merge_params |
|
213 @decorators.check_access |
|
214 def invite(self, request, access_type, |
|
215 page_name=None, params=None, **kwargs): |
|
216 """Creates the page upon which a Club Admin can invite another Club Admin. |
|
217 |
|
218 Args: |
|
219 request: the standard Django HTTP request object |
|
220 access_type : the name of the access type which should be checked |
|
221 context: dictionary containing the context for this view |
|
222 params: a dict with params for this View |
|
223 kwargs: the Key Fields for the specified entity |
|
224 """ |
|
225 |
|
226 # get the context for this webpage |
|
227 context = responses.getUniversalContext(request) |
|
228 context['page_name'] = page_name |
|
229 |
|
230 if request.method == 'POST': |
|
231 return self.invitePost(request, context, params, **kwargs) |
|
232 else: |
|
233 # request.method == 'GET' |
|
234 return self.inviteGet(request, context, params, **kwargs) |
|
235 |
|
236 def inviteGet(self, request, context, params, **kwargs): |
|
237 """Handles the GET request concerning the view that creates an invite |
|
238 for becoming a Club Admin. |
|
239 |
|
240 Args: |
|
241 request: the standard Django HTTP request object |
|
242 page_name: the page name displayed in templates as page and header title |
|
243 params: a dict with params for this View |
|
244 kwargs: the Key Fields for the specified entity |
|
245 """ |
|
246 |
|
247 # set the role to the right name |
|
248 fields = {'role' : '%(module_name)s' %(params)} |
|
249 |
|
250 # get the request view parameters and initialize the create form |
|
251 request_params = request_view.view.getParams() |
|
252 form = request_params['create_form'](initial=fields) |
|
253 |
|
254 # construct the appropriate response |
|
255 return super(View, self)._constructResponse(request, entity=None, |
|
256 context=context, form=form, params=params) |
|
257 |
|
258 def invitePost(self, request, context, params, **kwargs): |
|
259 """Handles the POST request concerning the view that creates an invite |
|
260 for becoming a Club Admin. |
|
261 |
|
262 Args: |
|
263 request: the standard Django HTTP request object |
|
264 page_name: the page name displayed in templates as page and header title |
|
265 params: a dict with params for this View |
|
266 kwargs: the Key Fields for the specified entity |
|
267 """ |
|
268 |
|
269 # get the request view parameters and populate the form using POST data |
|
270 request_params = request_view.view.getParams() |
|
271 form = request_params['create_form'](request.POST) |
|
272 |
|
273 if not form.is_valid(): |
|
274 # return the invalid form response |
|
275 return self._constructResponse(request, entity=None, context=context, |
|
276 form=form, params=params) |
|
277 |
|
278 # collect the cleaned data from the valid form |
|
279 key_name, form_fields = soc.views.helper.forms.collectCleanedFields(form) |
|
280 |
|
281 # get the club entity for which this request is for from link_id in kwargs |
|
282 club = club_logic.logic.getForFields({'link_id' : kwargs['link_id']}, unique=True) |
|
283 |
|
284 # create the fields for the new request entity |
|
285 request_fields = {'link_id' : form_fields['link_id'].link_id, |
|
286 'scope' : club, |
|
287 'scope_path' : club.link_id, |
|
288 'role' : params['module_name'], |
|
289 'role_verbose' : params['name'], |
|
290 'group_accepted' : True, |
|
291 'completed' : False} |
|
292 |
|
293 # extract the key_name for the new request entity |
|
294 key_fields = request_logic.logic.getKeyFieldsFromDict(request_fields) |
|
295 key_name = request_logic.logic.getKeyNameForFields(key_fields) |
|
296 |
|
297 # create the request entity |
|
298 entity = request_logic.logic.updateOrCreateFromKeyName(request_fields, key_name) |
|
299 |
|
300 # TODO(ljvderijk) redirect to a more useful place like the club homepage |
|
301 return http.HttpResponseRedirect('/') |
62 |
302 |
63 |
303 |
64 view = View() |
304 view = View() |
65 |
305 |
|
306 acceptInvite = view.acceptInvite |
66 create = view.create |
307 create = view.create |
67 delete = view.delete |
308 delete = view.delete |
68 edit = view.edit |
309 edit = view.edit |
|
310 invite = view.invite |
69 list = view.list |
311 list = view.list |
70 public = view.public |
312 public = view.public |
71 export = view.export |
313 export = view.export |
72 |
314 |