def admin_store(): form = AdminCreateForm(request.form) if form.validate(): admin = Admin() admin.create(data=form.data) return Success(message="操作成功!") return Fail(message=form.first_error)
def new_user(self, first_name, last_name, email, password, is_lecturer): user = Admin( first_name=first_name, last_name=last_name, email=email, password=password, is_lecturer=is_lecturer ) user.save() return user
def createadmin(username, password): admin = Admin() admin.username = username admin.set_password(password) db.session.add(admin) db.session.commit() print('success!')
def create_app(): app = Flask(__name__) app.config.from_object('app.config') register_blueprint(app) db.init_app(app) mail.init_app(app) login_manager.init_app(app) login_manager.login_view = 'web.login' login_manager.login_message = '请先登录或注册' # 第一次运行成功之后可以把34-45行注释掉 with app.app_context(): db.create_all(app=app) if not Admin.query.filter_by(nickname='admin').first(): ad = Admin() ad.nickname = 'admin' ad.role = 'super' ad.password = generate_password_hash('123456') with db.auto_commit(): db.session.add(ad) return app
def init(username, password): """初始化数据库 admin 分类""" click.echo('初始化数据库中...') db.create_all() with db.auto_commit(): admin = Admin.query.first() if admin is not None: click.echo('管理员账号已存在,更新中...') admin.username = username admin.set_password(password) else: click.echo('创建默认管理员数据中...') admin = Admin( username=username, blog_title='Br Blog', blog_sub_title="Genius only means hard-working all one's life.", name='Jack Zhang', about='''永不放弃是人生要成功的一大因素, 只要能够坚持, 锲而不舍, 终会到达成功的彼岸的。''' ) admin.set_password(password) db.session.add(admin) category = Category.query.first() if category is None: click.echo('创建默认分类...') category = Category(name='Default') db.session.add(category) click.echo('Done.')
def create_admin(): """ 创建管理员 """ log('程序执行创建添加管理员') for i in range(1, 10): n = str(i) form = dict(name='admin' + str(i), pwd=n.zfill(3), role_id=1) admin = Admin(form) log('a', admin) admin.save()
def add(): form = AdminForm() if form.validate_on_submit(): data = form.data admin_count = Admin.query.filter_by(name=data["name"]).count() if admin_count == 1: flash("管理员已经存在", 'err') return redirect(url_for('.add')) admin = Admin(data) admin.save() flash("管理员添加成功!", "ok") redirect(url_for('.add')) return render_template('admin/administrator/add.html', form=form)
def post(self): args = get_args() if not args: return send_message("Parâmetros inválidos!", 422) try: admin = Admin(args["nome"], args["email"], args["password"]) admin.add(admin) query = Admin.query.get(admin.id) except SQLAlchemyError as e: return rollback(e, db) else: return schema.dump(query).data, 201
def admin_add(): form = AdminAddForm(request.form) roles = Role.query.all() if request.method == 'POST' and form.validate(): with db.auto_commit(): admin = Admin() admin.set_attr(form.data) admin.is_super = AdminTypeEnum.NO_SUPER db.session.add(admin) Oplog('添加管理员:' + admin.name) flash('添加管理员成功~', 'ok') return redirect(url_for('admin.admin_add')) return render_template('admin/admin_add.html', form=form, roles=roles)
def post(self): args = get_args() if not args: return response("Invalid parameters", 422) try: admin = Admin(args["name"], args["email"], args["password"]) admin.add(admin) query = Admin.query.get(admin.id) except SQLAlchemyError as e: return rollback(e, db) else: return schema.dump(query).data, 201
def fake_admin(): with db.auto_commit(): data = Admin( username='******', blog_title='Br Blog', blog_sub_title = "Genius only means hard-working all one's life.", name = 'Jack Zhang', about = '''永不放弃是人生要成功的一大因素, 只要能够坚持, 锲而不舍, 终会到达成功的彼岸的。''' ) data.set_password('adminadmin') db.session.add(data)
def create_admin(account, pwd, repwd): """ 创建初始 admin :return: """ if pwd == repwd: with db.auto_commit(): admin = Admin() admin.account = account admin.password = pwd db.session.add(admin) click.echo(account + ' 已成功创建') else: click.echo('两次密码不同,请重新输入') return
def fight_comment(): comment = request.form["fight_text"] fight_db = Admin(fight_word=comment) db.session.add(fight_db) db.session.commit() flash("応援コメントを保存しました") return redirect(url_for("admin"))
def clear_comment(): comment = request.form["clear_text"] clear_db = Admin(clear_word=comment) db.session.add(clear_db) db.session.commit() flash("お疲れ様コメントを保存しました") return redirect(url_for("admin"))
def admin_delete(ids): ids = ids.split('-') if '1' in ids: return Fail(message="网站创始人不能删除") admins = Admin.get_all_in_ids(ids=ids) for admin in admins: admin.destroy() return Success(message="成功删除")
def post(self): data = request.get_json() username = data.get('username') password = data.get('password') role = data.get('role') if db.session.query(Admin).filter(Admin.username == username).count(): return make_response(jsonify({"msg": "管理员已存在"}), 400) safe_password = generate_password_hash(password) admin = Admin(username=username, password=safe_password, role_id=role) db.session.add(admin) db.session.commit() return jsonify({})
def admin_update(id): edit_one_field = request.form.get('edit_one_field', None) if not edit_one_field: form = AdminEditForm(formdata=request.form, id=id) else: form = AdminEditOneKeyForm(formdata=request.form, id=id) if not form.validate(): return Fail(message=form.first_error) admin = Admin.get_by_id(id=id) admin.update(form.data, edit_one_field) return Success(message="操作成功!")
def admin_add(): form = AdminForm() from werkzeug.security import generate_password_hash if form.validate_on_submit(): data = form.data admin = Admin(name=data["name"], pwd=generate_password_hash(data["pwd"]), role_id=data["role_id"], is_super=1) db.session.add(admin) db.session.commit() flash("添加管理员成功!", "ok") return render_template("admin/admin_add.html", form=form)
def post(self): login_id = self.get_argument('login_id') pwd = self.get_argument('pwd') name = self.get_argument('name') admin = Admin() admin.name = name admin.login_id = login_id admin.password = hashlib.md5(pwd.encode('utf-8')).hexdigest() admin.purview = 0 admin.deleted = 0 session.add(admin) session.commit()
def create_admin(name, password, email): """ 设置管理员 :param name: :param password: :param email: :return: """ with db.auto_commit(): admin = Admin() admin.name = name admin.password = password admin.email = email admin.avatar = '/static/admin/images/guest.png' admin.role = Role.query.get(1) db.session.add(admin) print("成功添加管理员,用户名:%s 密码:%s 邮箱:%s" % (name, password, email))
def create_default_data(): """ 添加默认数据 @return: """ DEFAULT_ROLE = ('超级管理员', '运维管理员', '审计员', '访客') from app.models.admin import Admin, Role from werkzeug.security import generate_password_hash # 添加角色 目前角色权限控制作为预留 for role in DEFAULT_ROLE: if not db.session.query(Role).filter(Role.name == role).first(): db.session.add(Role(name=role)) db.session.commit() superuser_role_id = db.session.query( Role.id).filter(Role.name == '超级管理员').first() if not db.session.query(Admin).filter( Admin.username == 'superuser').first(): db.session.add( Admin(username='******', password=generate_password_hash('admin'), role_id=superuser_role_id)) db.session.commit()
# 离线脚本,创建超级管理员 import uuid from app import create_app from app.libs.enum import AdminTypeEnum from app.models.admin import Admin from app.models.base import db from app.models.user import User app = create_app() with app.app_context(): with db.auto_commit(): # 创建一个超级管理员 admin_exists = Admin.query.filter_by(name='admin').first() if not admin_exists: admin = Admin() admin.name = 'admin' admin.pwd = 'admin' admin.is_super = AdminTypeEnum.IS_SUPER db.session.add(admin) for v in range(1, 4): with db.auto_commit(): user = User() user.name = 'fanm' + str(v) user.pwd = '123456' user.email = 'fanm' + str(v) + '@gmail.com' user.phone = '1234567891' + str(v) user.info = 'happy' user.face = 'avatar/' + str(v) + '.jpg' user.uuid = str(uuid.uuid4().hex) db.session.add(user)
def validate_account(self, field): username = field.data admin = Admin.find_by(name=username) if admin == None: raise ValidationError("帐号不存在!")
# -*- coding: utf-8 -*- """ @description: @copyright: (c) 2019/3/28 18:17 by Henry. """ from datetime import datetime __author__ = 'Henry' from app import create_app from app.models.base import db from app.models.admin import Admin app = create_app() with app.app_context(): with db.auto_commit(): # 创建一个超级管理员 user = Admin() user.nickname = 'Admin' user.username = '******' user.password = '******' user.email = '*****@*****.**' db.session.add(user)
def admin_lists(): total, result = Admin.get_list_all() return AjaxResponse(data=result, count=total)
def admin_edit(id): admin = Admin.get_by_id(id) roles = Role.get_all() return render_template('admin/admin/edit.html', admin=admin, roles=roles)