Add beans to User table

This commit is contained in:
zhuyj17 2016-07-31 15:39:04 +08:00
parent 538d9642a8
commit c1454e14d7
5 changed files with 44 additions and 5 deletions

View File

@ -880,7 +880,7 @@ if __name__ == '__main__':
if etcdclient.isdir("_lock")[0]: if etcdclient.isdir("_lock")[0]:
etcdclient.deldir("_lock") etcdclient.deldir("_lock")
G_usermgr = userManager.userManager('root','unias1616') G_usermgr = userManager.userManager('root')
if mode == "new": if mode == "new":
G_usermgr.initUsage() G_usermgr.initUsage()
G_notificationmgr = notificationmgr.NotificationMgr() G_notificationmgr = notificationmgr.NotificationMgr()

View File

@ -72,7 +72,7 @@ class User(db.Model):
register_date = db.Column(db.String(10)) register_date = db.Column(db.String(10))
user_group = db.Column(db.String(50)) user_group = db.Column(db.String(50))
auth_method = db.Column(db.String(10)) auth_method = db.Column(db.String(10))
beans = db.Column(db.Integer)
def __init__(self, username, password, avatar="default.png", nickname = "", description = "", status = "init", def __init__(self, username, password, avatar="default.png", nickname = "", description = "", status = "init",
e_mail = "" , student_number = "", department = "", truename = "", tel="", date = None, usergroup = "primary" e_mail = "" , student_number = "", department = "", truename = "", tel="", date = None, usergroup = "primary"
@ -92,6 +92,7 @@ class User(db.Model):
self.department = department self.department = department
self.truename = truename self.truename = truename
self.tel = tel self.tel = tel
self.beans = 1000
if (date != None): if (date != None):
self.register_date = date self.register_date = date
else: else:
@ -100,7 +101,8 @@ class User(db.Model):
self.auth_method = auth_method self.auth_method = auth_method
def __repr__(self): def __repr__(self):
return '<User %r>' % self.username #return '<User %r>' % (self.username)
return '<User %r beans %r>' % (self.username,self.beans)
#token will expire after 3600s #token will expire after 3600s
def generate_auth_token(self, expiration = 3600): def generate_auth_token(self, expiration = 3600):
@ -120,7 +122,6 @@ class User(db.Model):
user = User.query.get(data['id']) user = User.query.get(data['id'])
return user return user
class UserGroup(db.Model): class UserGroup(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50)) name = db.Column(db.String(50))

View File

@ -3,7 +3,7 @@
import subprocess,re,os,etcdlib,psutil,math,sys import subprocess,re,os,etcdlib,psutil,math,sys
import time,threading,json,traceback,platform import time,threading,json,traceback,platform
from model import db,VNode,History from model import db,VNode,History,User
from log import logger from log import logger
monitor_hosts = {} monitor_hosts = {}
@ -111,6 +111,15 @@ class Container_Collector(threading.Thread):
db.session.commit() db.session.commit()
logger.warning(err) logger.warning(err)
workercinfo[vnode_name]['basic_info']['billing'] = nowbillingval workercinfo[vnode_name]['basic_info']['billing'] = nowbillingval
owner_name = get_owner(vnode_name)
owner = User.query.filter_by(username=owner_name).first()
if owner is None:
logger.warning("Error!!! Billing User %s doesn't exist!" % (owner_name))
else:
logger.info("Billing User:"+str(owner))
owner.beans -= math.ceil(billingval)
db.session.commit()
logger.info("Billing User:"+str(owner))
def collect_containerinfo(self,container_name): def collect_containerinfo(self,container_name):
global workerinfo global workerinfo

17
tools/alterUserTable.py Normal file
View File

@ -0,0 +1,17 @@
import sys
sys.path.append("../src/")
from model import db,User
users = User.query.all()
db.drop_all(bind='__all__')
print(users)
setattr(User,'beans',db.Column(db.Integer))
db.create_all(bind='__all__')
for user in users:
newuser = User(user.username,user.password,user.avatar,user.nickname,user.description,user.status,
user.e_mail,user.student_number,user.department,user.truename,user.tel,user.register_date,
user.user_group,user.auth_method)
newuser.beans = 1000
db.session.add(newuser)
db.session.commit()

12
tools/test.py Normal file
View File

@ -0,0 +1,12 @@
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
DB_CONNECT_STRING = "sqlite:////opt/docklet/global/sys/UserTable.db"
engine = create_engine(DB_CONNECT_STRING,echo=True)
DB_Session = sessionmaker(bind=engine)
session = DB_Session()
print(session.execute('Select * from User').fetchall())
#print(session.execute('Alter table User add beans integer'))
#print(session.execute('update User set beans=1000'))
print(session.execute('Select * from User').fetchall())