32 from soc.logic.models import user as user_logic |
32 from soc.logic.models import user as user_logic |
33 from soc.views.helper import decorators |
33 from soc.views.helper import decorators |
34 from soc.views.helper import lists as list_helper |
34 from soc.views.helper import lists as list_helper |
35 from soc.views.helper import redirects |
35 from soc.views.helper import redirects |
36 from soc.views.helper import widgets |
36 from soc.views.helper import widgets |
37 from soc.views.models import base |
37 from soc.views.models import presence |
|
38 from soc.views.models import document as document_view |
38 from soc.views.models.request import view as request_view |
39 from soc.views.models.request import view as request_view |
39 |
40 from soc.views.sitemap import sidebar |
40 |
41 |
41 class View(base.View): |
42 |
|
43 class View(presence.View): |
42 """View methods for the Group model. |
44 """View methods for the Group model. |
43 """ |
45 """ |
44 |
46 |
45 # TODO(ljvderijk) add sidebar entry for listRequests to each group |
47 # TODO(ljvderijk) add sidebar entry for listRequests to each group |
46 |
48 |
257 role_view: The view that needs to be added to role_views. |
259 role_view: The view that needs to be added to role_views. |
258 """ |
260 """ |
259 |
261 |
260 role_views = self._params['role_views'] |
262 role_views = self._params['role_views'] |
261 role_views[role_name] = role_view |
263 role_views[role_name] = role_view |
|
264 |
|
265 def getExtraMenus(self, params=None): |
|
266 """Returns the extra menu's for this view. |
|
267 |
|
268 A menu item is generated for each group that the user has an active |
|
269 role for. The public page for each group is added as menu item, |
|
270 as well as all public documents for that group. |
|
271 |
|
272 Args: |
|
273 params: a dict with params for this View. |
|
274 """ |
|
275 |
|
276 params = dicts.merge(params, self._params) |
|
277 logic = params['logic'] |
|
278 |
|
279 # get the current user |
|
280 user_entity = user_logic.logic.getForCurrentAccount() |
|
281 |
|
282 # set fields to match every active role this user has |
|
283 fields = {'user': user_entity, |
|
284 'state' : 'active'} |
|
285 |
|
286 # get the role views and start filling group_entities |
|
287 role_views = self._params['role_views'] |
|
288 role_descriptions = {} |
|
289 |
|
290 for role_name in role_views.keys(): |
|
291 # get the view for a specific role |
|
292 role_view = role_views[role_name] |
|
293 |
|
294 # get the params for this view |
|
295 role_view_params = role_view.getParams() |
|
296 |
|
297 # get the logic for this view |
|
298 role_logic = role_view_params['logic'] |
|
299 |
|
300 # get all the active roles that this user has |
|
301 roles = role_logic.getForFields(fields) |
|
302 |
|
303 # for each role that we find |
|
304 for role in roles: |
|
305 |
|
306 # get the Key Name of the group |
|
307 group_key_name = role.scope.key().name() |
|
308 |
|
309 # try to get an existing description |
|
310 existing_role_descriptions = role_descriptions.get(group_key_name) |
|
311 |
|
312 if existing_role_descriptions: |
|
313 # add this description to existing roles |
|
314 existing_roles = existing_role_descriptions['roles'] |
|
315 existing_roles[role_name] = role |
|
316 else: |
|
317 # create a description of this role |
|
318 role_description = {'roles' : {role_name: role}, |
|
319 'group' : role.scope} |
|
320 |
|
321 # add the new entry to our dictionary |
|
322 role_descriptions[group_key_name] = role_description |
|
323 |
|
324 # get the document view params to add the group's documents to the menu |
|
325 doc_params = document_view.view.getParams() |
|
326 |
|
327 menus = [] |
|
328 |
|
329 # for each role description in our collection |
|
330 for role_description in role_descriptions.itervalues(): |
|
331 #start with an empty menu |
|
332 menu = {} |
|
333 |
|
334 # get the group for this role description |
|
335 group_entity = role_description['group'] |
|
336 |
|
337 # set the menu header name |
|
338 menu['heading'] = '%s %s' %(params['name'], |
|
339 group_entity.short_name) |
|
340 |
|
341 # TODO add homepage thing to groups |
|
342 doc_items = document_view.view.getMenusForScope(group_entity, params) |
|
343 doc_items = sidebar.getSidebarMenu(doc_items, params=doc_params) |
|
344 |
|
345 # get the group specific items |
|
346 group_items = self._getExtraMenuItems(role_description, params) |
|
347 group_items = sidebar.getSidebarMenu(group_items, params=self._params) |
|
348 |
|
349 menu['items'] = doc_items + group_items |
|
350 |
|
351 menus.append(menu) |
|
352 |
|
353 return menus |
|
354 |
|
355 def _getExtraMenuItems(self, role_description, params=None): |
|
356 """Used to implement group instance specific needs for the side menu |
|
357 |
|
358 Args: |
|
359 role_description : dict containing all the roles which is a dict of |
|
360 name and the role entity to which it belongs. Also |
|
361 group contains the group entity to which these roles |
|
362 belong. |
|
363 params: a dict with params for this View. |
|
364 """ |
|
365 return [] |