From 08df8532b202c1b46076ee19f1f748d97a2b4183 Mon Sep 17 00:00:00 2001 From: zhong Date: Tue, 5 Apr 2016 17:36:39 +0800 Subject: [PATCH 1/3] move quota group from database to json file --- src/httprest.py | 2 +- src/userManager.py | 156 ++++++++++++++++++++++++++--------- web/templates/admin.html | 7 +- web/templates/user_list.html | 14 ---- 4 files changed, 119 insertions(+), 60 deletions(-) diff --git a/src/httprest.py b/src/httprest.py index 08f11f9..5bc0dd3 100755 --- a/src/httprest.py +++ b/src/httprest.py @@ -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: diff --git a/src/userManager.py b/src/userManager.py index eaaaddd..c04f740 100755 --- a/src/userManager.py +++ b/src/userManager.py @@ -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): diff --git a/web/templates/admin.html b/web/templates/admin.html index de53b83..baddae8 100644 --- a/web/templates/admin.html +++ b/web/templates/admin.html @@ -66,7 +66,6 @@ - @@ -141,9 +140,9 @@ "columnDefs": [ { "render": function ( data, type, row ) { - return '' + 'Edit' + ''; + return '' + 'Edit' + ''; }, - "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; diff --git a/web/templates/user_list.html b/web/templates/user_list.html index c3f31bd..ae81585 100644 --- a/web/templates/user_list.html +++ b/web/templates/user_list.html @@ -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); - }); - } {% endblock %} From d409d2358e68180a98cbd8570d09c33910341c7a Mon Sep 17 00:00:00 2001 From: zhong Date: Wed, 6 Apr 2016 14:43:16 +0800 Subject: [PATCH 2/3] can remove quota group from web page can add quota with just one click --- src/httprest.py | 5 ++++- src/userManager.py | 23 ++++++++++++++++++++--- web/templates/admin.html | 19 ++++++++++++++++++- web/web.py | 8 +++++++- web/webViews/user/userlist.py | 13 +++++++++++++ 5 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/httprest.py b/src/httprest.py index 31f5eb1..56d6898 100755 --- a/src/httprest.py +++ b/src/httprest.py @@ -422,7 +422,10 @@ class DockletHttpHandler(http.server.BaseHTTPRequestHandler): result = G_usermgr.register(user = user, cur_user = cur_user) self.response(200, result) elif cmds[1] == 'groupadd': - result = G_usermgr.groupadd(name = form.getvalue('name', None), cur_user = cur_user) + result = G_usermgr.groupadd(form = form, cur_user = cur_user) + self.response(200, result) + elif cmds[1] == 'groupdel': + result = G_usermgr.groupdel(name = form.getvalue('name', None), cur_user = cur_user) self.response(200, result) elif cmds[1] == 'data': logger.info("handle request: user/data") diff --git a/src/userManager.py b/src/userManager.py index c04f740..c1d69b8 100755 --- a/src/userManager.py +++ b/src/userManager.py @@ -663,8 +663,8 @@ class userManager: @administration_required def groupadd(*args, **kwargs): - name = kwargs.get('name', None) - if (name == None): + form = kwargs.get('form') + if (form.getvalue("name") == None): return {"success":'false', "reason": "Empty group name"} #group_new = UserGroup(name) #db.session.add(group_new) @@ -672,7 +672,24 @@ class userManager: 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'}) + groups.append({'name':form.getvalue("name"), 'cpu':form.getvalue("cpu"), 'memory':form.getvalue("memory"), 'imageQuantity':form.getvalue("image"), 'lifeCycle':form.getvalue("lifecycle")}) + groupfile = open(fspath+"/global/group",'w') + groupfile.write(json.dumps(groups)) + groupfile.close() + return {"success":'true'} + + @administration_required + def groupdel(*args, **kwargs): + name = kwargs.get('name', None) + if (name == None): + return {"success":'false', "reason": "Empty group name"} + groupfile = open(fspath+"/global/group",'r') + groups = json.loads(groupfile.read()) + groupfile.close() + for group in groups: + if group['name'] == name: + groups.remove(group) + break groupfile = open(fspath+"/global/group",'w') groupfile.write(json.dumps(groups)) groupfile.close() diff --git a/web/templates/admin.html b/web/templates/admin.html index baddae8..19c016f 100644 --- a/web/templates/admin.html +++ b/web/templates/admin.html @@ -52,6 +52,22 @@ +
+ + +
+
+ + +
+
+ + +
+
+ + +
@@ -140,7 +156,8 @@ "columnDefs": [ { "render": function ( data, type, row ) { - return '' + 'Edit' + ''; + return '' + 'Edit' + '' + + '' + 'Delete' + ''; }, "targets": 5 }, diff --git a/web/web.py b/web/web.py index 5911d05..6f14843 100755 --- a/web/web.py +++ b/web/web.py @@ -20,7 +20,7 @@ from webViews.log import logger from flask import Flask, request, session, render_template, redirect, send_from_directory, make_response, url_for, abort from webViews.dashboard import dashboardView -from webViews.user.userlist import userlistView, useraddView, usermodifyView, groupaddView, userdataView, userqueryView +from webViews.user.userlist import userlistView, useraddView, usermodifyView, groupaddView, groupdelView, userdataView, userqueryView from webViews.user.userinfo import userinfoView from webViews.user.userActivate import userActivateView from webViews.user.grouplist import grouplistView, groupqueryView, groupdetailView, groupmodifyView @@ -328,6 +328,12 @@ def usermodify(): def groupadd(): return groupaddView.as_view() +@app.route("/group/delete//", methods=['POST', 'GET']) +@administration_required +def groupdel(groupname): + groupdelView.groupname = groupname + return groupdelView.as_view() + @app.route("/user/info/", methods=['GET', 'POST']) @login_required def userinfo(): diff --git a/web/webViews/user/userlist.py b/web/webViews/user/userlist.py index c3cc62a..5af0582 100644 --- a/web/webViews/user/userlist.py +++ b/web/webViews/user/userlist.py @@ -54,3 +54,16 @@ class groupaddView(normalView): def post(self): dockletRequest.post('/user/groupadd', request.form) return redirect('/admin/') + +class groupdelView(normalView): + @classmethod + def post(self): + data = { + "name" : self.groupname, + } + dockletRequest.post('/user/groupdel', data) + return redirect('/admin/') + + @classmethod + def get(self): + return self.post() From 32d56db3a636b211773bc214cda73b8856d85ca0 Mon Sep 17 00:00:00 2001 From: zhong Date: Wed, 6 Apr 2016 17:22:06 +0800 Subject: [PATCH 3/3] remove some useless notes --- src/model.py | 5 +---- src/userManager.py | 53 +++++----------------------------------------- 2 files changed, 6 insertions(+), 52 deletions(-) diff --git a/src/model.py b/src/model.py index c42e4e6..be2e65d 100755 --- a/src/model.py +++ b/src/model.py @@ -96,10 +96,7 @@ class User(db.Model): self.register_date = date else: self.register_date = datetime.utcnow() - if (UserGroup.query.filter_by(name=usergroup).first() != None): - self.user_group = usergroup - else: - self.user_group = "primary" + self.user_group = usergroup self.auth_method = auth_method def __repr__(self): diff --git a/src/userManager.py b/src/userManager.py index c1d69b8..727772e 100755 --- a/src/userManager.py +++ b/src/userManager.py @@ -125,12 +125,8 @@ class userManager: ''' try: User.query.all() - UserGroup.query.all() except: db.create_all() - root = UserGroup('root') - db.session.add(root) - db.session.commit() if password == None: #set a random password password = os.urandom(16) @@ -149,11 +145,6 @@ class userManager: path = env.getenv('DOCKLET_LIB') subprocess.call([path+"/userinit.sh", username]) db.session.commit() - admin = UserGroup('admin') - primary = UserGroup('primary') - 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 = [] @@ -365,7 +356,6 @@ class userManager: List informantion for oneself ''' user = kwargs['cur_user'] - #group = UserGroup.query.filter_by(name = user.user_group).first() groupfile = open(fspath+"/global/group",'r') groups = json.loads(groupfile.read()) groupfile.close() @@ -374,6 +364,11 @@ class userManager: if one_group['name'] == user.user_group: group = one_group break + else: + for one_group in groups: + if one_group['name'] == "primary": + group = one_group + break result = { "success": 'true', "data":{ @@ -466,7 +461,6 @@ class userManager: groupfile = open(fspath+"/global/group",'r') groups = json.loads(groupfile.read()) groupfile.close() - #allgroup = UserGroup.query.all() result = { "success": 'true', "data":[] @@ -482,17 +476,6 @@ class userManager: ] 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 @@ -519,18 +502,6 @@ class userManager: return result else: return {"success":False, "reason":"Group does not exist"} - #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): @@ -541,7 +512,6 @@ class userManager: groupfile = open(fspath+"/global/group",'r') groups = json.loads(groupfile.read()) groupfile.close() - #groups = UserGroup.query.all() result = { "groups": [], } @@ -554,7 +524,6 @@ 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() groupfile = open(fspath+"/global/group",'r') groups = json.loads(groupfile.read()) groupfile.close() @@ -571,15 +540,6 @@ class userManager: return {"success":'true'} else: return {"success":'false', "reason":"UserGroup does not exist"} - #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): @@ -666,9 +626,6 @@ class userManager: form = kwargs.get('form') if (form.getvalue("name") == None): return {"success":'false', "reason": "Empty group name"} - #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()
ID Name CPU Memory