cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
group.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.wi.views.user.group
22 #
23 # @author Krzysztof Danielowski
24 # @author Piotr W√≥jcik
25 # @date 01.04.2012
26 #
27 from django.shortcuts import render_to_response
28 from django.template import RequestContext
29 from django.template.loader import render_to_string
30 from django.utils.translation import ugettext as _
31 from django.views.decorators.csrf import csrf_protect
32 
33 from wi.utils import messages_ajax, parsing
34 from wi.utils.decorators import django_view
35 from wi.utils.decorators import user_permission
36 from wi.utils.messages_ajax import ajax_request
37 from wi.utils.states import user_groups_states_reversed
38 from wi.utils.views import prep_data
39 
40 
41 @django_view
42 @ajax_request
43 @user_permission
44 @csrf_protect
45 ##
46 #
47 # Ajax view for sending group request membership.
48 #
49 def grp_ajax_send_request(request, group_id, template_name='generic/simple.html'):
50  if request.method == 'POST':
51  prep_data(('user/group/join_request/', {'group_id': group_id}), request.session)
52 
53  return messages_ajax.success(_('You have successfully sent a request.'))
54  else:
55  return messages_ajax.success(render_to_string(template_name,
56  {'confirmation': _('Yes'),
57  'text': _('Do you want to send a request?')},
58  context_instance=RequestContext(request)))
59 
60 
61 @django_view
62 @ajax_request
63 @user_permission
64 ##
65 #
66 # Ajax view for fetching user's group list.
67 #
68 def grp_ajax_get_table_my(request):
69  if request.method == 'GET':
70  rest_data = prep_data({'own_groups': 'user/group/list_own_groups/',
71  'my_groups': 'user/group/list_groups/'}, request.session)
72 
73  leader = parsing.parse_own_groups(rest_data)
74 
75  for item in rest_data['my_groups']:
76  item['amILeader'] = True if item['group_id'] in leader else False
77 
78  return messages_ajax.success(rest_data['my_groups'])
79 
80 
81 @django_view
82 @ajax_request
83 @user_permission
84 ##
85 #
86 # Ajax view for fetching all group list.
87 #
89  if request.method == 'GET':
90  all_groups = prep_data('user/group/get_list/', request.session)
91 
92  for item in all_groups:
93  item['user_statusName'] = unicode(user_groups_states_reversed[item['user_status']])
94 
95  return messages_ajax.success(all_groups)
96 
97 
98 @django_view
99 @user_permission
100 ##
101 #
102 # Group details page view.
103 #
104 def grp_details(request, group_id, template_name='groups/group_details.html'):
105  rest_data = prep_data({'groups': 'user/group/list_groups/',
106  'group_data': ('user/group/get_by_id/', {'group_id': group_id})},
107  request.session)
108 
109  group = rest_data['group_data']
110  my_groups = parsing.parse_groups_ids(rest_data)
111 
112  group['leader_self'] = True if group['leader_id'] == request.session['user'].user_id else False
113  group['member'] = True if group['group_id'] in my_groups else False
114 
115  return render_to_response(template_name, {'group': group}, context_instance=RequestContext(request))
116 
117 
118 @django_view
119 @ajax_request
120 @user_permission
121 ##
122 #
123 # Ajax view for fetching group details.
124 #
125 def grp_ajax_grp_details_table(request, group_id):
126  if request.method == 'GET':
127  user = request.session['user']
128  rest_data = prep_data({'group_data': ('user/group/get_by_id/', {'group_id': group_id}),
129  'members': ('user/group/list_members/', {'group_id': group_id}),
130  'requests': ('user/group/list_requests/', {'group_id': group_id})},
131  request.session)
132 
133  leader_id = rest_data['group_data'].get('leader_id')
134  leader = True
135  if leader_id == '':
136  leader = False
137  else:
138  if int(user.user_id) != int(leader_id):
139  leader = False
140 
141  for item in rest_data['members']:
142  item['request'] = False
143  item['leader'] = leader
144  item['self'] = True if int(item['user_id']) == int(user.user_id) else False
145  item['group_id'] = group_id
146  for item in rest_data['requests']:
147  item['request'] = True
148  item['leader'] = leader
149  item['self'] = True if int(item['user_id']) == int(user.user_id) else False
150  item['group_id'] = group_id
151 
152  return messages_ajax.success([{'name':_('Members:'), 'items': rest_data['members']},
153  {'name':_('Requests:'), 'items': rest_data['requests']}])
154