17 """Views for Role profiles. |
17 """Views for Role profiles. |
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 from django.utils.translation import ugettext_lazy |
28 from django.utils.translation import ugettext_lazy |
27 |
29 |
28 from soc.logic import dicts |
30 from soc.logic import dicts |
|
31 from soc.logic.models import request as request_logic |
29 from soc.logic.models import user as user_logic |
32 from soc.logic.models import user as user_logic |
|
33 from soc.logic.helper import request as request_helper |
30 from soc.views import helper |
34 from soc.views import helper |
31 from soc.views import out_of_band |
35 from soc.views import out_of_band |
32 from soc.views.helper import access |
36 from soc.views.helper import access |
|
37 from soc.views.helper import decorators |
33 from soc.views.helper import redirects |
38 from soc.views.helper import redirects |
|
39 from soc.views.helper import responses |
34 from soc.views.models import base |
40 from soc.views.models import base |
|
41 from soc.views.models import request as request_view |
35 from soc.views.models import user as user_view |
42 from soc.views.models import user as user_view |
36 |
43 |
37 import soc.models.request |
44 import soc.models.request |
38 import soc.views.helper.lists |
45 import soc.views.helper.lists |
39 import soc.views.helper.responses |
46 import soc.views.helper.responses |
40 import soc.views.helper.widgets |
47 import soc.views.helper.widgets |
41 |
|
42 |
|
43 class RequestForm(helper.forms.BaseForm): |
|
44 """Django form displayed when creating a new invititation/request. |
|
45 """ |
|
46 |
|
47 class Meta: |
|
48 """Inner Meta class that defines some behavior for the form. |
|
49 """ |
|
50 |
|
51 #: db.Model subclass for which the form will gather information |
|
52 model = soc.models.request.Request |
|
53 |
|
54 #: Exclude pretty much everything, model=None would |
|
55 #: also remove the help text etc. |
|
56 exclude = ['requester', 'to', 'role', |
|
57 'accepted', 'declined'] |
|
58 |
|
59 requester = forms.CharField(widget=helper.widgets.ReadOnlyInput()) |
|
60 |
|
61 role = forms.CharField(widget=helper.widgets.ReadOnlyInput()) |
|
62 |
|
63 to = forms.CharField(widget=helper.widgets.ReadOnlyInput()) |
|
64 |
48 |
65 |
49 |
66 class View(base.View): |
50 class View(base.View): |
67 """Views for all entities that inherit from Role. |
51 """Views for all entities that inherit from Role. |
68 |
52 |
78 Args: |
62 Args: |
79 params: This dictionary should be filled with the parameters |
63 params: This dictionary should be filled with the parameters |
80 """ |
64 """ |
81 |
65 |
82 new_params = {} |
66 new_params = {} |
83 |
67 # TODO(ljvderijk) add request and process_request |
84 patterns = [(r'^%(url_name)s/(?P<access_type>invite)/%(lnp)s$', |
68 patterns = [(r'^%(url_name)s/(?P<access_type>invite)/%(scope)s$', |
85 'soc.views.models.%(module_name)s.invite', |
69 'soc.views.models.%(module_name)s.invite', |
86 'Invite %(name_short)s')] |
70 'Create invite for %(name_plural)s'), |
|
71 (r'^%(url_name)s/(?P<access_type>accept_invite)/%(scope)s/%(lnp)s$', |
|
72 'soc.views.models.%(module_name)s.acceptInvite', |
|
73 'Accept invite for %(name_plural)s')] |
87 |
74 |
88 new_params['extra_django_patterns'] = patterns |
75 new_params['extra_django_patterns'] = patterns |
89 new_params['scope_redirect'] = redirects.getInviteRedirect |
76 new_params['scope_redirect'] = redirects.getInviteRedirect |
90 |
77 |
91 params = dicts.merge(params, new_params) |
78 params = dicts.merge(params, new_params) |
92 |
79 |
93 super(View, self).__init__(params=params) |
80 super(View, self).__init__(params=params) |
94 |
81 |
|
82 @decorators.merge_params |
|
83 @decorators.check_access |
95 def invite(self, request, access_type, |
84 def invite(self, request, access_type, |
96 page_name=None, params=None, *args, **kwargs): |
85 page_name=None, params=None, **kwargs): |
97 """Displays the request promotion to Role page. |
86 """Creates the page on which an invite can be send out. |
98 """ |
87 |
99 |
88 Args: |
100 new_params = {} |
89 request: the standard Django HTTP request object |
101 |
90 access_type : the name of the access type which should be checked |
102 group_scope = kwargs['link_id'] |
91 context: dictionary containing the context for this view |
103 |
92 params: a dict with params for this View |
104 new_params['list_action'] = (redirects.getCreateRequestRedirect, |
93 kwargs: the Key Fields for the specified entity |
105 {'group_scope' : group_scope, |
94 """ |
106 'url_name' : self._params['url_name']}) |
95 |
107 new_params['list_description'] = \ |
96 # get the context for this webpage |
108 self.DEF_INVITE_INSTRUCTION_MSG_FMT % self._params |
97 context = responses.getUniversalContext(request) |
109 new_params['logic'] = user_logic.logic |
98 context['page_name'] = page_name |
110 |
99 |
111 new_params = dicts.merge(params, new_params) |
100 if request.method == 'POST': |
112 params = dicts.merge(new_params, user_view.view._params) |
101 return self.invitePost(request, context, params, **kwargs) |
113 rights = params['rights'] |
102 else: |
114 |
103 # request.method == 'GET' |
115 try: |
104 return self.inviteGet(request, context, params, **kwargs) |
116 access.checkAccess(access_type, request, rights, args, kwargs) |
105 |
117 except out_of_band.Error, error: |
106 def inviteGet(self, request, context, params, **kwargs): |
118 return helper.responses.errorResponse(error, request) |
107 """Handles the GET request concerning the view that creates an invite |
119 |
108 for attaining a certain Role. |
120 content = helper.lists.getListContent(request, params) |
109 |
121 contents = [content] |
110 Args: |
122 |
111 request: the standard Django HTTP request object |
123 return self._list(request, params, contents, page_name) |
112 page_name: the page name displayed in templates as page and header title |
124 |
113 params: a dict with params for this View |
|
114 kwargs: the Key Fields for the specified entity |
|
115 """ |
|
116 |
|
117 # set the role to the right name |
|
118 fields = {'role' : '%(module_name)s' %(params)} |
|
119 |
|
120 # get the request view parameters and initialize the create form |
|
121 request_params = request_view.view.getParams() |
|
122 form = request_params['create_form'](initial=fields) |
|
123 |
|
124 # construct the appropriate response |
|
125 return super(View, self)._constructResponse(request, entity=None, |
|
126 context=context, form=form, params=params) |
|
127 |
|
128 def invitePost(self, request, context, params, **kwargs): |
|
129 """Handles the POST request concerning the view that creates an invite |
|
130 for attaining a certain Role. |
|
131 |
|
132 Args: |
|
133 request: the standard Django HTTP request object |
|
134 page_name: the page name displayed in templates as page and header title |
|
135 params: a dict with params for this View |
|
136 kwargs: the Key Fields for the specified entity |
|
137 """ |
|
138 |
|
139 # get the request view parameters and populate the form using POST data |
|
140 request_params = request_view.view.getParams() |
|
141 form = request_params['create_form'](request.POST) |
|
142 |
|
143 if not form.is_valid(): |
|
144 # return the invalid form response |
|
145 return self._constructResponse(request, entity=None, context=context, |
|
146 form=form, params=params) |
|
147 |
|
148 # collect the cleaned data from the valid form |
|
149 key_name, form_fields = soc.views.helper.forms.collectCleanedFields(form) |
|
150 |
|
151 # get the group entity for which this request is via the scope_path |
|
152 group_key_fields = kwargs['scope_path'].rsplit('/',1) |
|
153 |
|
154 if len(group_key_fields) == 1: |
|
155 # there is only a link_id |
|
156 fields = {'link_id' : group_key_fields[0]} |
|
157 else: |
|
158 # there is a scope_path and link_id |
|
159 fields = {'scope_path' : group_key_fields[0], |
|
160 'link_id' : group_key_fields[1]} |
|
161 |
|
162 group = params['group_logic'].getForFields(fields, unique=True) |
|
163 |
|
164 if group.scope_path: |
|
165 request_scope_path = '%s/%s' % [group.scope_path, group.link_id] |
|
166 else: |
|
167 request_scope_path = group.link_id |
|
168 |
|
169 # create the fields for the new request entity |
|
170 request_fields = {'link_id' : form_fields['link_id'].link_id, |
|
171 'scope' : group, |
|
172 'scope_path' : request_scope_path, |
|
173 'role' : params['module_name'], |
|
174 'role_verbose' : params['name'], |
|
175 'state' : 'group_accepted'} |
|
176 |
|
177 # extract the key_name for the new request entity |
|
178 key_fields = request_logic.logic.getKeyFieldsFromDict(request_fields) |
|
179 key_name = request_logic.logic.getKeyNameForFields(key_fields) |
|
180 |
|
181 # create the request entity |
|
182 entity = request_logic.logic.updateOrCreateFromKeyName(request_fields, key_name) |
|
183 |
|
184 # TODO(ljvderijk) redirect to a more useful place like the group homepage |
|
185 return http.HttpResponseRedirect('/') |
|
186 |
|
187 |
|
188 @decorators.merge_params |
|
189 @decorators.check_access |
|
190 def acceptInvite(self, request, access_type, |
|
191 page_name=None, params=None, **kwargs): |
|
192 """Creates the page process an invite into a Role. |
|
193 |
|
194 Args: |
|
195 request: the standard Django HTTP request object |
|
196 access_type : the name of the access type which should be checked |
|
197 context: dictionary containing the context for this view |
|
198 params: a dict with params for this View |
|
199 kwargs: the Key Fields for the specified entity |
|
200 """ |
|
201 |
|
202 # get the context for this webpage |
|
203 context = responses.getUniversalContext(request) |
|
204 context['page_name'] = page_name |
|
205 |
|
206 if request.method == 'POST': |
|
207 return self.acceptInvitePost(request, context, params, **kwargs) |
|
208 else: |
|
209 # request.method == 'GET' |
|
210 return self.acceptInviteGet(request, context, params, **kwargs) |
|
211 |
|
212 def acceptInviteGet(self, request, context, params, **kwargs): |
|
213 """Handles the GET request concerning the creation of a Role via an |
|
214 invite. |
|
215 |
|
216 Args: |
|
217 request: the standard Django HTTP request object |
|
218 context: dictionary containing the context for this view |
|
219 params: a dict with params for this View |
|
220 kwargs: the Key Fields for the specified entity |
|
221 """ |
|
222 |
|
223 # create the form using the scope_path and link_id from kwargs as initial value |
|
224 fields = {'link_id' : kwargs['link_id'], |
|
225 'scope_path' : kwargs['scope_path']} |
|
226 form = params['invited_create_form'](initial=fields) |
|
227 |
|
228 # construct the appropriate response |
|
229 return super(View, self)._constructResponse(request, entity=None, |
|
230 context=context, form=form, params=params) |
|
231 |
|
232 def acceptInvitePost(self, request, context, params, **kwargs): |
|
233 """Handles the POST request concerning the creation of a Role via an |
|
234 invite. |
|
235 |
|
236 Args: |
|
237 request: the standard Django HTTP request object |
|
238 context: dictionary containing the context for this view |
|
239 params: a dict with params for this View |
|
240 kwargs: the Key Fields for the specified entity |
|
241 """ |
|
242 |
|
243 # populate the form using the POST data |
|
244 form = params['invited_create_form'](request.POST) |
|
245 |
|
246 if not form.is_valid(): |
|
247 # return the invalid form response |
|
248 return self._constructResponse(request, entity=None, context=context, |
|
249 form=form, params=params) |
|
250 |
|
251 # collect the cleaned data from the valid form |
|
252 key_name, fields = soc.views.helper.forms.collectCleanedFields(form) |
|
253 |
|
254 # call the post process method |
|
255 self._acceptInvitePost(fields, request, context, params, **kwargs) |
|
256 |
|
257 group_logic = params['group_logic'] |
|
258 group_entity = group_logic.getFromKeyName(fields['scope_path']) |
|
259 fields['scope'] = group_entity |
|
260 |
|
261 # make sure that this role becomes active once more in case this user |
|
262 # has been reinvited |
|
263 fields ['state'] = 'active' |
|
264 |
|
265 # get the key_name for the new entity |
|
266 key_fields = self._logic.getKeyFieldsFromDict(fields) |
|
267 key_name = self._logic.getKeyNameForFields(key_fields) |
|
268 |
|
269 # create new Role entity |
|
270 entity = self._logic.updateOrCreateFromKeyName(fields, key_name) |
|
271 |
|
272 # mark the request as completed |
|
273 request_helper.completeRequestForRole(entity, params['module_name']) |
|
274 |
|
275 # redirect to the roles overview page |
|
276 return http.HttpResponseRedirect('/user/roles') |
|
277 |
|
278 |
|
279 def _acceptInvitePost(self, fields, request, context, params, **kwargs): |
|
280 """ Used to post-process data after the fields have been cleaned. |
|
281 |
|
282 Args: |
|
283 fields : the cleaned fields from the role form |
|
284 request: the standard Django HTTP request object |
|
285 context: dictionary containing the context for this view |
|
286 params: a dict with params for this View |
|
287 kwargs: the Key Fields for the specified entity |
|
288 """ |
|
289 pass |