Add application database and some functions.

This commit is contained in:
zhuyj17 2016-09-11 17:06:52 +08:00
parent 44cfecae1c
commit 2bbca0b9d8
3 changed files with 80 additions and 3 deletions

38
src/beansapplicationmgr.py Executable file
View File

@ -0,0 +1,38 @@
from model import db,User,ApplyMsg
class ApplicationMgr:
def __init__(self):
try:
ApplyMsg.query.all()
except:
db.create_all()
def apply(self,username,number,reason):
applymsg = ApplyMsg(username,number,reason)
db.session.add(applymsg)
db.session.commit()
def query(self,username):
applymsgs = ApplyMsg.query.filter_by(username=username).all()
return list(eval(str(applymsgs)))
def queryUnRead(self):
applymsgs = ApplyMsg.query.filter_by(status="Processing").all()
return list(eval(str(applymsgs)))
def agree(self,msgid):
try:
applymsg = ApplyMsg.query.get(msgid)
except:
return
applymsg.status = "Agreed"
db.session.commit()
def reject(self,msgid):
try:
applymsg = ApplyMsg.query.get(msgid)
except:
return
applymsg.status = "Rejected"
db.session.commit()

View File

@ -23,7 +23,7 @@ import os
import http.server, cgi, json, sys, shutil
from socketserver import ThreadingMixIn
import nodemgr, vclustermgr, etcdlib, network, imagemgr, notificationmgr
import userManager
import userManager,beansapplicationmgr
import monitor,traceback
import threading
import sysmgr
@ -512,6 +512,23 @@ def listphynodes_monitor(cur_user, user, form):
res['allnodes'] = G_nodemgr.get_allnodes()
return json.dumps({'success':'true', 'monitor':res})
@app.route("/beans/<issue>/", methods=['POST'])
@login_required
def beans_apply(cur_user,user,form,issue):
global G_applicationmgr
if issue == 'apply':
number = form.get("number",None)
reason = form.get("reason",None)
if number is None or reason is None:
return json.dumps({'success':'false', 'message':'Number and reason can\'t be null.'})
G_applicationmgr.apply(user,number,reason)
return json.dumps({'success':'True'})
elif issue == 'applymsgs':
applymsgs = G_applicationmgr.query(user)
return json.dumps({'success':'True','applymsgs':str(applymsgs)})
else:
return json.dumps({'success':'false','message':'Unsupported URL!'})
@app.route("/user/modify/", methods=['POST'])
@login_required
@ -848,6 +865,7 @@ if __name__ == '__main__':
global G_clustername
global G_sysmgr
global G_historymgr
global G_applicationmgr
# move 'tools.loadenv' to the beginning of this file
fs_path = env.getenv("FS_PREFIX")
@ -955,6 +973,7 @@ if __name__ == '__main__':
master_collector = monitor.Master_Collector(G_nodemgr,ipaddr+":"+str(masterport))
master_collector.start()
logger.info("master_collector started")
G_applicationmgr = beansapplicationmgr.ApplicationMgr()
# server = http.server.HTTPServer((masterip, masterport), DockletHttpHandler)
logger.info("starting master server")

View File

@ -40,7 +40,10 @@ fsdir = env.getenv('FS_PREFIX')
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+fsdir+'/global/sys/UserTable.db'
app.config['SQLALCHEMY_BINDS'] = {'history': 'sqlite:///'+fsdir+'/global/sys/HistoryTable.db'}
app.config['SQLALCHEMY_BINDS'] = {
'history': 'sqlite:///'+fsdir+'/global/sys/HistoryTable.db',
'beansapplication': 'sqlite:///'+fsdir+'/global/sys/BeansApplication.db'
}
try:
secret_key_file = open(env.getenv('FS_PREFIX') + '/local/token_secret_key.txt')
app.secret_key = secret_key_file.read()
@ -92,7 +95,7 @@ class User(db.Model):
self.department = department
self.truename = truename
self.tel = tel
self.beans = 10000
self.beans = 100
if (date != None):
self.register_date = date
else:
@ -236,3 +239,20 @@ class History(db.Model):
def __repr__(self):
return "{\"id\":\"%d\",\"vnode\":\"%s\",\"action\":\"%s\",\"runningtime\":\"%d\",\"cputime\":\"%f\",\"billing\":\"%d\",\"actionTime\":\"%s\"}" % (self.id, self.vnode, self.action, self.runningtime, self.cputime, self.billing, self.actionTime.strftime("%Y-%m-%d %H:%M:%S"))
class ApplyMsg(db.Model):
__bind_key_ = 'beansapplication'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(10))
number = db.Column(db.Integer)
reason = db.Column(db.String(200))
status = db.Column(db.String(10))
def __init__(self,username, number, reason):
self.username = username
self.number = number
self.reason = reason
self.status = "Processing"
def __repr__(self):
return "{\"id\":\"%d\", \"username\":\"%s\", \"number\": \"%d\", \"reason\":\"%s\", \"status\":\"%s\"}" % (self.id, self.username, self.number, self.reason, self.status)