def clearall(): # 清除数据库以及删除程序包 from iot_pkg.core import create_db from iot_pkg import settings db = create_db() db.drop_all() click.echo("Dropped the database") try: file_list = os.listdir(settings.FILE_PATH) for file in file_list: _file = os.path.join(settings.FILE_PATH, file) os.remove(_file) click.echo("Dropped file: %s" % file) except Exception as e: click.echo("删除文件失败") click.echo(str(e)) click.echo("请手动删除所有文件") try: cache_files = os.listdir(settings.CACHE_PATH) for file in cache_files: _file = os.path.join(settings.CACHE_PATH, file) os.remove(_file) click.echo("Dropped cache: %s" % file) except Exception as e: click.echo("删除缓存失败") click.echo(str(e)) click.echo("请手动删除所有缓存数据") finally: click.echo("Done...")
def initdb(): # 初始化数据库表 from iot_pkg.core import create_db db = create_db() pprint.pprint(db.get_tables_for_bind()) db.drop_all() db.create_all() click.echo("Initialized the database")
def update(): from iot_pkg.core import create_db from iot_pkg.models.proj import Project from iot_pkg.utils import get_random_string db = create_db() projects = Project.query.all() for proj in projects: proj.uid = proj.make_uid() db.session.commit() click.echo('Done...')
# -*- coding: utf-8 -*- import functools from flask_restful import request from iot_pkg.settings import USER_SESSION_KEY from iot_pkg.contrib.session import Session from iot_pkg.core import create_db db = create_db() def login_required(func): @functools.wraps(func) def wrapper(*args, **kwargs): user_session_id = request.values.get(USER_SESSION_KEY) or \ request.cookies.get(USER_SESSION_KEY) or \ request.headers.get(USER_SESSION_KEY) if not user_session_id: return {"code": "401", "msg": "Unauthorized"} user = Session.get(user_session_id) if not user: Session.delete(user_session_id) return {"code": "401", "msg": "Unauthorized"} setattr(request, USER_SESSION_KEY, user_session_id) user = db.session.merge(user) request.user = user return func(*args, **kwargs) return wrapper
def dropdb(): # 删除数据库 from iot_pkg.core import create_db db = create_db() db.drop_all() click.echo("Dropped the database")