move quota group from database to json file

This commit is contained in:
zhong 2016-04-05 17:36:39 +08:00
parent 405d12cce1
commit 08df8532b2
4 changed files with 119 additions and 60 deletions

View File

@ -434,7 +434,7 @@ class DockletHttpHandler(http.server.BaseHTTPRequestHandler):
result = G_usermgr.groupList(cur_user = cur_user)
self.response(200, result)
elif cmds[1] == 'groupQuery':
result = G_usermgr.groupQuery(ID = form.getvalue("ID", '3'), cur_user = cur_user)
result = G_usermgr.groupQuery(name = form.getvalue("name"), cur_user = cur_user)
if (result.get('success', None) == None or result.get('success', None) == "false"):
self.response(301,result)
else:

View File

@ -19,10 +19,12 @@ from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from datetime import datetime
import json
email_from_address = env.getenv('EMAIL_FROM_ADDRESS')
admin_email_address = env.getenv('ADMIN_EMAIL_ADDRESS')
PAM = pam.pam()
fspath = env.getenv('FS_PREFIX')
if (env.getenv('EXTERNAL_LOGIN').lower() == 'true'):
from plugin import external_receive
@ -152,6 +154,14 @@ class userManager:
db.session.add(admin)
db.session.add(primary)
db.session.commit()
if not os.path.exists(fspath+"/global/group"):
groupfile = open(fspath+"/global/group",'w')
groups = []
groups.append({'name':'root', 'cpu':'100000', 'memory':'2000', 'imageQuantity':'10', 'lifeCycle':'24'})
groups.append({'name':'admin', 'cpu':'100000', 'memory':'2000', 'imageQuantity':'10', 'lifeCycle':'24'})
groups.append({'name':'primary', 'cpu':'100000', 'memory':'2000', 'imageQuantity':'10', 'lifeCycle':'24'})
groupfile.write(json.dumps(groups))
groupfile.close()
def auth_local(self, username, password):
password = hashlib.sha512(password.encode('utf-8')).hexdigest()
@ -355,7 +365,15 @@ class userManager:
List informantion for oneself
'''
user = kwargs['cur_user']
group = UserGroup.query.filter_by(name = user.user_group).first()
#group = UserGroup.query.filter_by(name = user.user_group).first()
groupfile = open(fspath+"/global/group",'r')
groups = json.loads(groupfile.read())
groupfile.close()
group = None
for one_group in groups:
if one_group['name'] == user.user_group:
group = one_group
break
result = {
"success": 'true',
"data":{
@ -373,10 +391,10 @@ class userManager:
"register_date" : "%s"%(user.register_date),
"group" : user.user_group,
"groupinfo": {
"cpu": group.cpu,
"memory": group.memory,
"imageQuantity": group.imageQuantity,
"lifeCycle":group.lifeCycle,
"cpu": group['cpu'],
"memory": group['memory'],
"imageQuantity": group['imageQuantity'],
"lifeCycle":group['lifeCycle'],
},
},
}
@ -445,44 +463,74 @@ class userManager:
Usage: list(cur_user = token_from_auth)
List all groups for an administrator
'''
allgroup = UserGroup.query.all()
groupfile = open(fspath+"/global/group",'r')
groups = json.loads(groupfile.read())
groupfile.close()
#allgroup = UserGroup.query.all()
result = {
"success": 'true',
"data":[]
}
for group in allgroup:
for group in groups:
groupinfo = [
group.id,
group.name,
group.cpu,
group.memory,
group.imageQuantity,
group.lifeCycle,
group['name'],
group['cpu'],
group['memory'],
group['imageQuantity'],
group['lifeCycle'],
'',
]
result["data"].append(groupinfo)
#for group in allgroup:
# groupinfo = [
# group.id,
# group.name,
# group.cpu,
# group.memory,
# group.imageQuantity,
# group.lifeCycle,
# '',
# ]
# result["data"].append(groupinfo)
return result
@administration_required
def groupQuery(*args, **kwargs):
'''
Usage: groupQuery(id = XXX, cur_user = token_from_auth)
Usage: groupQuery(name = XXX, cur_user = token_from_auth)
List a group for an administrator
'''
group = UserGroup.query.filter_by(id = kwargs['ID']).first()
if (group == None):
groupfile = open(fspath+"/global/group",'r')
groups = json.loads(groupfile.read())
groupfile.close()
for group in groups:
if group['name'] == kwargs['name']:
result = {
"success":'true',
"data":{
"name" : group['name'] ,
"cpu" : group['cpu'] ,
"memory" : group['memory'],
"imageQuantity" : group['imageQuantity'],
"lifeCycle" : group['lifeCycle'],
}
}
return result
else:
return {"success":False, "reason":"Group does not exist"}
result = {
"success":'true',
"data":{
"name" : group.name ,
"cpu" : group.cpu ,
"memory" : group.memory,
"imageQuantity" : group.imageQuantity,
"lifeCycle" : group.lifeCycle,
}
}
return result
#group = UserGroup.query.filter_by(id = kwargs['ID']).first()
#result = {
# "success":'true',
# "data":{
# "name" : group.name ,
# "cpu" : group.cpu ,
# "memory" : group.memory,
# "imageQuantity" : group.imageQuantity,
# "lifeCycle" : group.lifeCycle,
# }
#}
#return result
@administration_required
def groupListName(*args, **kwargs):
@ -490,12 +538,15 @@ class userManager:
Usage: grouplist(cur_user = token_from_auth)
List all group names for an administrator
'''
groups = UserGroup.query.all()
groupfile = open(fspath+"/global/group",'r')
groups = json.loads(groupfile.read())
groupfile.close()
#groups = UserGroup.query.all()
result = {
"groups": [],
}
for group in groups:
result["groups"].append(group.name)
result["groups"].append(group['name'])
return result
@administration_required
@ -503,16 +554,32 @@ class userManager:
'''
Usage: groupModify(newValue = dict_from_form, cur_user = token_from_auth)
'''
group_modify = UserGroup.query.filter_by(name = kwargs['newValue'].getvalue('groupname', None)).first()
if (group_modify == None):
#group_modify = UserGroup.query.filter_by(name = kwargs['newValue'].getvalue('groupname', None)).first()
groupfile = open(fspath+"/global/group",'r')
groups = json.loads(groupfile.read())
groupfile.close()
for group in groups:
if group['name'] == kwargs['newValue'].getvalue('groupname',None):
form = kwargs['newValue']
group['cpu'] = form.getvalue('cpu', '')
group['memory'] = form.getvalue('memory', '')
group['imageQuantity'] = form.getvalue('image', '')
group['lifeCycle'] = form.getvalue('lifecycle', '')
groupfile = open(fspath+"/global/group",'w')
groupfile.write(json.dumps(groups))
groupfile.close()
return {"success":'true'}
else:
return {"success":'false', "reason":"UserGroup does not exist"}
form = kwargs['newValue']
group_modify.cpu = form.getvalue('cpu', '')
group_modify.memory = form.getvalue('memory', '')
group_modify.imageQuantity = form.getvalue('image', '')
group_modify.lifeCycle = form.getvalue('lifecycle', '')
db.session.commit()
return {"success":'true'}
#if (group_modify == None):
# return {"success":'false', "reason":"UserGroup does not exist"}
#form = kwargs['newValue']
#group_modify.cpu = form.getvalue('cpu', '')
#group_modify.memory = form.getvalue('memory', '')
#group_modify.imageQuantity = form.getvalue('image', '')
#group_modify.lifeCycle = form.getvalue('lifecycle', '')
#db.session.commit()
#return {"success":'true'}
@administration_required
def modify(*args, **kwargs):
@ -599,9 +666,16 @@ class userManager:
name = kwargs.get('name', None)
if (name == None):
return {"success":'false', "reason": "Empty group name"}
group_new = UserGroup(name)
db.session.add(group_new)
db.session.commit()
#group_new = UserGroup(name)
#db.session.add(group_new)
#db.session.commit()
groupfile = open(fspath+"/global/group",'r')
groups = json.loads(groupfile.read())
groupfile.close()
groups.append({'name':name, 'cpu':'100000', 'memory':'2000', 'imageQuantity':'10', 'lifeCycle':'24'})
groupfile = open(fspath+"/global/group",'w')
groupfile.write(json.dumps(groups))
groupfile.close()
return {"success":'true'}
def queryForDisplay(*args, **kwargs):

View File

@ -66,7 +66,6 @@
<table id="myGroupTable" class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>CPU</th>
<th>Memory</th>
@ -141,9 +140,9 @@
"columnDefs": [
{
"render": function ( data, type, row ) {
return '<a class="btn btn-info btn-sm" data-toggle="modal" data-target="#ModifyGroupModal" onClick="javascript:setFormGroup(' + row[0] + ');">' + 'Edit' + '</a>';
return '<a class="btn btn-info btn-sm" data-toggle="modal" data-target="#ModifyGroupModal" onClick="javascript:setFormGroup('+ "'" + row[0] + "'" + ');">' + 'Edit' + '</a>';
},
"targets": 6
"targets": 5
},
]
@ -158,7 +157,7 @@
function setFormGroup(arg){
$.post("/group/query/",
{
ID: arg,
name: arg
},
function(data,status){
var result = eval("("+data+")").data;

View File

@ -255,19 +255,5 @@
$("#mDescription").val(result.description);
});
}
function setFormGroup(arg){
$.post("/group/query/",
{
ID: arg,
},
function(data,status){
var result = eval("("+data+")").data;
$("#mGroupname").val(result.name);
$("#mCpu").val(result.cpu);
$("#mMemory").val(result.memory);
$("#mImage").val(result.imageQuantity);
$("#mLifecycle").val(result.lifeCycle);
});
}
</script>
{% endblock %}