cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
user.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.cm.views.admin_cm.user
22 # @alldecoratedby{src.cm.utils.decorators.admin_cm_log} (except for \c add)
23 #
24 # @author Tomek SoĊ›nicki <tom.sosnicki@gmail.com>
25 #
26 from cm.utils.exception import CMException
27 from cm.models.user import User
28 from cm.utils.decorators import admin_cm_log, user_log
29 
30 
31 @user_log(log=True)
32 ##
33 #
34 # Adds specified CC1 User to current CM.
35 #
36 # @cmview_user
37 # @note This method is decorated by user_log decorator, not by admin_cm_log.
38 # This is caused by the fact that CLMAdmin doesn't need to be CMAdmin and
39 # despite not having rights to call CMAdmin functions he needs to call it on
40 # CMAdmin privileges.
41 #
42 # @param_post{user_id}
43 #
44 def add(caller_id, user_id):
45 
46  User.create(user_id)
47 
48 
49 @admin_cm_log(log=True)
50 ##
51 #
52 # Changes specified User's quota.
53 #
54 # @cmview_admin_cm
55 # @param_post{user_id}
56 # @param_post{memory}
57 # @param_post{cpu,int}
58 # @param_post{storage,int}
59 # @param_post{public_ip,int}
60 # @param_post{points,int}
61 #
62 def change_quota(caller_id, user_id, memory=None, cpu=None, storage=None, public_ip=None, points=None):
63 
64  user = User.get(user_id)
65 
66  user.memory = memory or user.memory
67  user.cpu = cpu or user.cpu
68  user.storage = storage or user.storage
69  user.public_ip = public_ip or user.public_ip
70  user.points = points or user.points
71 
72  try:
73  user.save()
74  except:
75  raise CMException('user_change_quota')
76 
77 
78 @admin_cm_log(log=True)
79 ##
80 #
81 # Changes quota of multiple users.
82 #
83 # @cmview_admin_cm
84 # @param_post{user_ids,list(int)}
85 # @param_post{memory,int} new RAM memory limit [MB]
86 # @param_post{cpu,int} new CPU's limit
87 # @param_post{storage,int} new storage space's limit [MB]
88 # @param_post{public_ip,int} new limit of public_ips
89 # @param_post{points,int} new monthly points limit
90 #
91 def multiple_change_quota(caller_id, user_ids, memory=None, cpu=None, storage=None, public_ip=None, points=None):
92 
93  for user_id in user_ids:
94  user = User.get(user_id)
95  user.memory = memory or user.memory
96  user.cpu = cpu or user.cpu
97  user.storage = storage or user.storage
98  user.public_ip = public_ip or user.public_ip
99  user.points = points or user.points
100  try:
101  user.save()
102  except:
103  raise CMException('user_change_quota')
104 
105 @admin_cm_log(log=True)
106 ##
107 #
108 # Get specified User's qouta.
109 #
110 # @cmview_admin_cm
111 # @param_post{user_id,int}
112 #
113 # @response{dict} Full User's quota
114 #
115 def get_quota(caller_id, user_id):
116  return User.get(user_id).long_dict
117 
118 
119 @admin_cm_log(log=False)
120 ##
121 #
122 # Returns all Users.
123 #
124 # @cmview_admin_cm
125 # @param_post{short,bool} caller's CM admin password
126 # @response{list(dict)} User.long_dict property for each User
127 #
128 def get_list(caller_id, short=False):
129  return [user.dict if short else user.long_dict for user in User.objects.all()]
130