def perm(app, request): def user_loader(id): return User(id=id, nickname='User%d' % id) def current_user_loader(): return user_loader(1) def users_loader(**kwargs): return [user_loader(id) for id in range(20)] app.config['PERM_URL_PREFIX'] = '/perm' app.config['PERM_ADMIN_USERNAME'] = '******' app.config['PERM_ADMIN_PASSWORD'] = '******' perm = Perm() perm.app = app perm.init_app(app) perm.user_loader(user_loader) perm.current_user_loader(current_user_loader) perm.users_loader(users_loader) db.create_all() return perm
# -*- coding: utf-8 -*- from collections import namedtuple from flask import Flask, g, render_template, render_template_string, abort from flask_sqlalchemy import SQLAlchemy from flask_perm import Perm from flask_script import Manager app = Flask(__name__) manager = Manager(app) db = SQLAlchemy() perm = Perm() app.config['DEBUG'] = True app.config['SECRET_KEY'] = 'secret key' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/flask_perm.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['PERM_ADMIN_ECHO'] = True db.app = app db.init_app(app) perm.app = app perm.init_app(app) perm.register_commands(manager) class User(namedtuple('User', 'id nickname')): pass @app.before_request def before_request(): g.user = User(**{'id': 1, 'nickname': 'user1'})