def CreateDropRequest(self): try: db.drop_all() db.create_all() s = db.session() admin = Admin( admin_id=uuid.uuid4(), username=u'Admin', password=Config().get_md5('123456'), avatarUrl='', role_id=1 ) s.add(admin) s.commit() role_id = uuid.uuid4() role = Role(role_id=role_id, name=u'超级管理员', checkKey='[]') s.add(role) s.commit() self.__init_routes(init_route, '0', role_id) self.__init_menus(init_menu, '0', role_id) return True except Exception as e: print e return str(e.message)
def ImportSql(self, file): s = db.session() try: filename = 'TABLE%s.sql' % int(time.time() * 1000) config = Config() if not os.path.exists(sql_dir): os.mkdir(sql_dir) file_path = os.path.join(sql_dir, filename) file.save(file_path) sqlfromat = "%s -h%s -u%s -p%s -P%s %s <%s" sql = (sqlfromat % ('mysql ', config.host, config.admin, config.password, config.port, config.db, file_path)) db.drop_all() os.system(sql) return True except Exception as e: s.rollback() print e return str(e.message)
def CreateAdminRequest(self, params): ''' 新建管理员 ''' s = db.session() try: if params['role_id'] == 1: return str('不能设为为超级管理员') admin = s.query(Admin).filter(Admin.username == params['username']).first() if admin: return str('管理员已存在') item = Admin( admin_id=uuid.uuid4(), username=params['username'], password=Config().get_md5(params['password']), sex=int(params['sex']), nickname=params['nickname'], role_id=int(params['role_id']), avatarUrl=params['avatarUrl'] ) s.add(item) s.commit() return True except Exception as e: s.rollback() print e return str(e.message)
def CreateDropRequest(self, isInit, params=None): db.drop_all() db.create_all() s = db.session() try: s.add(InitSql(isInit=False)) s.commit() role_id = uuid.uuid4() role = Role( role_id=role_id, name=self.role_name, mark=default['role_mark'] ) s.add(role) s.commit() password = self.__get_code() if not isInit: admin = Admin( admin_id=uuid.uuid4(), username=self.user_name, password=Config().get_md5(password), avatarUrl='', role_id=role_id ) else: admin = Admin( admin_id=params['admin_id'], username=self.user_name, password=params['password'], avatarUrl='', role_id=role_id ) s.add(admin) s.commit() self.__init_menus(s, init_menu) folder = Folder( folder_id=uuid.uuid4(), admin_id=None, name=u'系统文件', is_sys=True ) s.add(folder) s.commit() sql = s.query(InitSql).one() sql.isInit = True s.commit() return { 'username': '******', 'password': password } except Exception as e: s.rollback() print e return str(e.message)
def ModifyAdminRequest(self, admin_id, params): ''' 修改管理员信息 ''' s = db.session() try: if params['role_id'] == 1: return str('不能设为为超级管理员') admin = s.query(Admin).filter(Admin.admin_id == admin_id).first() if not admin: return str('管理员不存在') if not admin.isLock: return str('管理员被禁用') AllowableFields = [ 'nickname', 'sex', 'role_id', 'avatarUrl', 'password' ] data = {} for i in params: if i in AllowableFields and params.has_key(i): if i == 'password': if params[i] != admin.password: data[i] = Config().get_md5(params[i]) else: data[i] = params[i] s.query(Admin).filter(Admin.admin_id == admin_id).update(data) s.commit() return True except Exception as e: print e s.rollback() return str(e.message)
def ExportSql(self, type=1): try: config = Config() if not os.path.exists(sql_dir): os.mkdir(sql_dir) os.chdir(sql_dir) filename = 'TABLE%s.sql' % int(time.time() * 1000) sqlfromat = "%s -h%s -u%s -p%s -P%s %s >%s" if type == 2: sqlfromat = "%s -h%s -u%s -p%s -P%s -d %s >%s" # 不包含数据 if type == 3: sqlfromat = "%s -h%s -u%s -p%s -P%s -t %s >%s" # 不包含表结构 sql = (sqlfromat % ('mysqldump ', config.host, config.admin, config.password, config.port, config.db, filename)) os.system(sql) return { 'path': os.path.join(sql_dir, filename), 'name': filename } except Exception as e: print e return str(e.message)
def init_app(app): # mysql 数据库连接数据 app.config['SQLALCHEMY_DATABASE_URI'] = Config().get_sql_url() # 禁用追踪对象的修改并且发送信号(会需要额外内存) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.init_app(app)
def init_app(app): # mysql 数据库连接数据 app.config['SQLALCHEMY_DATABASE_URI'] = Config().get_sql_url() # 禁用追踪对象的修改并且发送信号(会需要额外内存) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # 关闭连接池 app.config['SQLALCHEMY_POOL_SIZE'] = None global db db.init_app(app)
def ModifyAdminRequest(self, admin_id, params): ''' 修改管理员信息 ''' s = db.session() try: admin = s.query(Admin).filter(Admin.admin_id == admin_id).first() if not admin: return str('管理员不存在') if admin.is_disabled: return str('管理员被禁用') role = s.query(Role).filter( Role.role_id == params['role_id']).first() if not role: return str('角色不存在') if admin.role_id != role.role_id and role.mark == default[ 'role_mark']: return str('不能设为为超级管理员') admin.nickname = params['nickname'] admin.sex = params['sex'] admin.role_id = params['role_id'] admin.avatarUrl = params['avatarUrl'] admin.email = params['email'] admin.is_disabled = params['is_disabled'] if params['password'] != admin.password: admin.password = Config().get_md5(params['password']) user = copy.deepcopy(admin.to_json()) del user['id'] del user['create_time'] del user['update_time'] user['mark'] = role.mark s.commit() return user except Exception as e: print e s.rollback() return str(e.message)
def CreateDropRequest(self, isInit): try: s = db.session() if isInit: try: res = s.query(InitSql).first() if res.isInit: return str('已经初始化~~~') except: pass db.drop_all() db.create_all() admin = Admin(admin_id=uuid.uuid4(), username=u'Admin', password=Config().get_md5('123456'), avatarUrl='', role_id=1) s.add(admin) s.commit() role_id = uuid.uuid4() role = Role(role_id=role_id, name=u'超级管理员', checkKey='[]') s.add(role) s.commit() self.__init_routes(init_route, '0', role_id) self.__init_menus(init_menu, '0', role_id) if not isInit: sql = InitSql(isInit=True) s.add(sql) s.commit() return True except Exception as e: print e return str(e.message)
def GetAdminRequest(self, username, password): ''' 查询管理员 ''' s = db.session() try: admin = s.query(Admin).filter( Admin.username == username, Admin.password == Config().get_md5(password)).first() if not admin: return str('管理员不存在') if not admin.isLock: return str('管理员被禁用') data = admin.to_json() route = [] menu = [] interface = [] role = s.query(Role).filter(Role.id == admin.role_id).first() if role: for i in role.routes: item = i.to_json() if item['isLock']: route.append(item) for i in role.menus.filter(Menu.isLock == True).order_by( Menu.sort, Menu.id): menu.append(i.to_json()) interfaces = s.query(Interface).filter( Interface.menu_id == i.menu_id, Interface.isLock == True).all() interface = interface + [i.to_json() for i in interfaces] data['routes'] = route data['menus'] = menu data['interface'] = interface return data except Exception as e: print e return str(e.message)
def CreateAdminRequest(self, params): ''' 新建管理员 ''' s = db.session() try: role = s.query(Role).filter( Role.role_id == params['role_id']).first() if not role: return str('角色不存在') if role.mark == default['role_mark']: return str('不能设为为超级管理员') admin = s.query(Admin).filter( Admin.username == params['username']).first() if admin: return str('管理员已存在') item = Admin(admin_id=uuid.uuid4(), username=params['username'], password=Config().get_md5(params['password']), sex=int(params['sex']), email=params['email'], nickname=params['nickname'], avatarUrl=params['avatarUrl'], role_id=params['role_id'], is_disabled=params['is_disabled']) s.add(item) role.admins.append(item) s.commit() return True except Exception as e: s.rollback() print e return str(e.message)
def GetAdminRequest(self, username, password): ''' 登录管理员 ''' s = db.session() try: admin = s.query(Admin).filter(Admin.username == username).first() if not admin: return str('管理员不存在') is_lock = s.query(LoginLock).filter( LoginLock.user_id == admin.admin_id).first() if is_lock and is_lock.number >= base_info['lock_times']: if datetime.datetime.now() < is_lock.lock_time: return str( '账号锁定中, 请在%s分钟后重试' % (is_lock.lock_time - datetime.datetime.now()).minute) if admin.password != Config().get_md5(password): number = 1 if is_lock: number = is_lock.number + 1 if number >= base_info['lock_times']: add_minutes = (number - base_info['lock_times']) + 1 is_lock.lock_time = datetime.datetime.now( ) + datetime.timedelta(minutes=add_minutes) is_lock.number = number else: s.add( LoginLock(lock_id=uuid.uuid4(), user_id=admin.admin_id, flag=False, number=number, ip=request.headers['X-Real-Ip'] if request.headers.has_key('X-Real-Ip') else request.remote_addr)) s.commit() if number - base_info['lock_times'] == 0: return str('密码不正确, 您的账号已经被锁定, 请在%s分钟后重试' % add_minutes) else: return str('密码不正确, 还有%s次机会' % (base_info['lock_times'] - number)) if admin.is_disabled: return str('管理员被禁用') menu = [] interface = [] role = s.query(Role).filter(Role.role_id == admin.role_id).first() if role: if role.mark == default['role_mark']: interface = [ value.to_json() for value in s.query(Interface).all() ] menu = [value.to_json() for value in s.query(Menu).all()] else: for i in role.menus.order_by(Menu.sort, Menu.id): menu.append(i.to_json()) for i in role.interfaces: interface.append(i.to_json()) user = copy.deepcopy(admin.to_json()) del user['id'] del user['create_time'] del user['update_time'] user['mark'] = role.mark # 登录成功删除掉原来的锁定记录 if is_lock: s.delete(is_lock) s.commit() return {'menus': menu, 'interface': interface, 'user': user} except Exception as e: print e return str(e.message)
@LastEditTime: 2020-04-28 09:55:26 ''' import sys import os curPath = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(curPath)[0] sys.path.append(rootPath) from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_script import Manager from flask_migrate import Migrate, MigrateCommand from conf.setting import Config app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = Config().get_sql_url() app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) from models.salary import * from models.system import * from models.log import * # 初始化 migrate # 两个参数一个是 Flask 的 app,一个是数据库 db migrate = Migrate(app, db) # 初始化管理器 manager = Manager(app) # 添加 db 命令,并与 MigrateCommand 绑定