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.clm.views.user.group
22 # @alldecoratedby{src.clm.utils.decorators.user_log}
23 #
24 
25 from clm.models.user import User
26 from clm.models.group import Group
27 from clm.models.user_group import UserGroup
28 from clm.models.message import Message
29 from clm.utils import log, message
30 from clm.utils.cm import CM
31 from clm.utils.decorators import user_log
32 from clm.utils.exception import CLMException
33 from common.states import group_states, image_access # , image_types
34 import json
35 
36 
37 @user_log(log=True)
38 ##
39 #
40 # Creates new Group of Users. Caller becomes its leader. He also becomes a
41 # member of that Group with @val{ok} state.
42 #
43 # @clmview_user
44 # @param_post{name,string}
45 # @param_post{description,string}
46 #
47 def create(cm_id, caller_id, name, description):
48  user = User.get(caller_id)
49 
50  # create group
51  group = Group()
52  group.leader = user
53  group.name = name
54  group.desc = description
55  group.save()
56 
57  # create first membership
58  mem = UserGroup()
59  mem.user = user
60  mem.group = group
61  mem.status = group_states['ok']
62  try:
63  mem.save()
64  except:
65  raise CLMException('group_create')
66 
67 
68 @user_log(log=True)
69 ##
70 #
71 # Sends request for acceptation in specified Groupfor caller.
72 # Adds caller to members with 'waiting' state.
73 #
74 # @clmview_user
75 # @param_post{group_id,int} id of the Group, which caller wants to become member of
76 #
77 def join_request(cm_id, caller_id, group_id):
78  group = Group.get(group_id)
79  user = User.get(caller_id)
80 
81  mem = UserGroup()
82  mem.user = user
83  mem.group = group
84  mem.status = group_states['waiting']
85 
86  message.info(group.leader_id, 'group_request', params={'first_name': user.first, 'last_name': user.last, 'group_name': group.name, 'group_id': group.id})
87  try:
88  mem.save()
89  except:
90  raise CLMException('group_request')
91 
92 
93 @user_log(log=False)
94 ##
95 #
96 # Returns Group.dict property of each existing Groups, supplemented by
97 # callers membership status: @val{ok}, @val{waiting} or @val{not member}
98 # under @val{user_status} key.
99 #
100 # @clmview_user
101 # @response{list(dict)} Group.dict property for each group, supplemented by
102 # @val{user_status} key.
103 #
104 def get_list(cm_id, caller_id):
105  user = User.get(caller_id)
106  waiting = []
107  ok = []
108 
109  for ug in UserGroup.objects.filter(user_id__exact=user.id):
110  if ug.status == group_states['waiting']:
111  waiting.append(ug.group_id)
112  elif ug.status == group_states['ok']:
113  ok.append(ug.group_id)
114  groups = []
115  for g in Group.objects.all():
116  d = g.dict
117  if g.id in ok:
118  d['user_status'] = group_states['ok']
119  elif g.id in waiting:
120  d['user_status'] = group_states['waiting']
121  else:
122  d['user_status'] = group_states['not member']
123  groups.append(d)
124 
125  return groups
126 
127 
128 @user_log(log=False)
129 ##
130 #
131 # Returns list of caller's Groups (only those where caller is accepted).
132 #
133 # @clmview_user
134 # @response{list(dict)} Group.dict property for each caller's Group
135 #
136 def list_groups(cm_id, caller_id):
137  user = User.get(caller_id)
138  groups = []
139  for g in user.group_set.all():
140  d = g.dict
141  d['status'] = group_states['ok']
142  groups.append(d)
143 
144  return groups
145 
146 
147 @user_log(log=False)
148 ##
149 #
150 # Method returns members of the group with id @prm{group_id}.
151 #
152 # @clmview_user
153 # @param_post{group_id,int} id of the Group that we get list of
154 #
155 # @response{list(dict)} dicts describing users belonging to specifie group.
156 #
157 def list_members(cm_id, caller_id, group_id):
158 
159  # group_id is sent in the URL
160  group = Group.objects.get(pk=group_id)
161 
162  return [u.dict for u in group.users.filter(usergroup__status__exact=group_states['ok'])]
163 
164 
165 @user_log(log=True) # XXX
166 ##
167 #
168 # Method returns list of the groups caller is leader of.
169 #
170 # @clmview_user
171 # @response{list(dict)} dicts describing groups led by caller
172 #
173 def list_own_groups(cm_id, caller_id):
174  user = User.get(caller_id)
175 
176  # returns all the groups where the user is the leader
177  return [g.dict for g in user.own_groups]
178 
179 
180 @user_log(log=False)
181 ##
182 #
183 # Function returns list of the users requesting acceptation
184 # in group with id @prm{group_id}.
185 #
186 # @clmview_user
187 # @param_post{group_id,int} id of the group which we check membership
188 # requests for
189 #
190 # @response{list(dict)} dicts describing requesting users
191 #
192 def list_requests(cm_id, caller_id, group_id):
193 
194  # group_id is sent in the URL
195  group = Group.objects.get(pk=group_id)
196 
197  return [u.dict for u in group.users.filter(usergroup__status__exact=group_states['waiting'])]
198 
199 
200 @user_log(log=True)
201 ##
202 #
203 # Method deletes specified Group.
204 #
205 # @clmview_user
206 # @param_post{group_id,int} id of the Group to delete
207 #
208 def delete(cm_id, caller_id, group_id):
209 
210  group = Group.get(group_id)
211 
212  resp = CM(cm_id).send_request("user/system_image/get_list/", caller_id=caller_id, group_id=[group_id], access=image_access['group'])
213  if resp['status'] != 'ok':
214  return resp['data']
215 
216  log.debug(caller_id, 'groups %s' % resp)
217 
218  user = User.get(caller_id)
219  # set private all the system images that belong to the group
220  for img in resp['data']:
221  resp = CM(cm_id).sendRequest(cm_id, caller_id, "user/system_image/set_private/", system_image_id=img['image_id'], leader_groups=[g.group_id for g in user.own_groups])
222  log.debug(caller_id, 'image set private %s' % resp['data'])
223  if resp['status'] != 'ok':
224  return resp['data']
225 
226  try:
227  group.delete()
228  except:
229  raise CLMException('group_delete')
230 
231 
232 @user_log(log=True)
233 ##
234 #
235 # Method edits specified Group.
236 #
237 # @param_post{group_id,string} id of the group to edit
238 # @param_post{name,string} group's name
239 # @param_post{description,string} group's description
240 #
241 def edit(cm_id, caller_id, group_id, name=None, description=None):
242  group = Group.get(group_id)
243  if name:
244  group.name = name
245  if description:
246  group.desc = description
247  try:
248  group.save()
249  except:
250  raise CLMException('group_edit')
251 
252 
253 # TODO: not tested
254 @user_log(log=False)
255 ##
256 #
257 # Method activates @prm{user_id} user in group @prm{group_id}. Activated
258 # user gains access to IsoImage-s shared by that group.
259 #
260 # @param_post{user_id,int} id of the user to activate
261 # @param_post{group_id,int} id of the group in which user must be activated
262 #
263 def activate_user(cm_id, caller_id, user_id, group_id):
264  # check that the caller is leader
265  User.is_leader(caller_id, group_id)
266 
267  try:
268  mem = UserGroup.objects.filter(group_id__exact=group_id).filter(user_id__exact=user_id).filter(status__exact=group_states['waiting'])[0]
269  except:
270  raise CLMException('user2group_get')
271 
272  mem.status = group_states['ok']
273  for m in Message.objects.filter(user_id__exact=caller_id).filter(code__exact='group_request'):
274  if json.loads(m.params).get('group_id', None) == id:
275  m.delete()
276  try:
277  mem.save()
278  except:
279  raise CLMException('user_activate')
280 
281 
282 @user_log(log=True)
283 ##
284 #
285 # Method deletes membership of the specified user in specific group,
286 #
287 # @param_post{user_id,int} id of the user to delete from group
288 # @param_post{group_id,int} id of the managed group
289 #
290 def delete_user(cm_id, caller_id, user_id, group_id):
291  if caller_id != user_id:
292  User.is_leader(caller_id, group_id)
293 
294  try:
295  mem = UserGroup.objects.filter(group_id__exact=group_id).filter(user_id__exact=user_id)[0]
296  except:
297  raise CLMException('user2group_get')
298 
299  for m in Message.objects.filter(user_id__exact=caller_id).filter(code__exact='group_request'):
300  log.debug(caller_id, 'message params %s' % m.params)
301  if json.loads(m.params).get('group_id', None) == id:
302  log.debug(caller_id, 'delete message for group %s' % id)
303  m.delete()
304 
305  try:
306  mem.delete()
307  except:
308  raise CLMException('group_delete_user')
309 
310 
311 @user_log(log=True)
312 ##
313 #
314 # Function changes owner of the specified group. Only owner may be the caller,
315 # otherwise exception is thrown. @prm{user_id} becomes new Group's leader.
316 #
317 # @param_post{user_id,int} id of the new owner
318 # @param_post{group_id,int} id of the managed Group
319 #
320 def change_owner(cm_id, caller_id, user_id, group_id):
321  # check that the caller is leader
322  User.is_leader(caller_id, group_id)
323 
324  group = Group.get(group_id)
325  new_leader = User.get(user_id)
326 
327  group.leader = new_leader
328 
329  try:
330  group.save()
331  except:
332  raise CLMException('group_change_owner')
333 
334 
335 @user_log(log=False)
336 ##
337 #
338 # Method returns requested group.
339 #
340 # @param_post{group_id,int} id of the requested Group
341 #
342 # @response{dict} requested Group's details
343 #
344 def get_by_id(cm_id, caller_id, group_id):
345 
346  return Group.get(group_id).dict
347