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) result = G_usermgr.groupList(cur_user = cur_user)
self.response(200, result) self.response(200, result)
elif cmds[1] == 'groupQuery': 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"): if (result.get('success', None) == None or result.get('success', None) == "false"):
self.response(301,result) self.response(301,result)
else: else:

View File

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

View File

@ -66,7 +66,6 @@
<table id="myGroupTable" class="table table-striped table-bordered"> <table id="myGroupTable" class="table table-striped table-bordered">
<thead> <thead>
<tr> <tr>
<th>ID</th>
<th>Name</th> <th>Name</th>
<th>CPU</th> <th>CPU</th>
<th>Memory</th> <th>Memory</th>
@ -141,9 +140,9 @@
"columnDefs": [ "columnDefs": [
{ {
"render": function ( data, type, row ) { "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){ function setFormGroup(arg){
$.post("/group/query/", $.post("/group/query/",
{ {
ID: arg, name: arg
}, },
function(data,status){ function(data,status){
var result = eval("("+data+")").data; var result = eval("("+data+")").data;

View File

@ -255,19 +255,5 @@
$("#mDescription").val(result.description); $("#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> </script>
{% endblock %} {% endblock %}