def create_role_and_user(user_datastore): with app.app_context(): print "creating role..." admin_role = Role(name='admin') manager_role = Role(name='manager') lead_role = Role(name='lead') assembler_role = Role(name='assembler') db.session.add(admin_role) db.session.add(manager_role) db.session.add(lead_role) db.session.add(assembler_role) print "creating user..." special_users = [('Admin', '*****@*****.**', 'admin', admin_role, 1), ('Manager', '*****@*****.**', 'manager', manager_role, 1), ('Lead 1', '*****@*****.**', 'lead', lead_role, 1), ('Lead 2', '*****@*****.**', 'lead', lead_role, 1), ('Lead 3', '*****@*****.**', 'lead', lead_role, 2), ('Lead 4', '*****@*****.**', 'lead', lead_role, 3)] for u in special_users: admin_user = user_datastore.create_user(name=u[0], email=u[1], password=encrypt_password( u[2]), roles=[u[3]], shift_id=u[4]) first_names = [ 'Harry', 'Amelia', 'Oliver', 'Jack', 'Isabella', 'Charlie', 'Sophie', 'Mia', 'Jacob', 'Thomas', 'Emily', 'Lily', 'Ava', 'Isla', 'Alfie', 'Olivia', 'Jessica', 'Riley', 'William', 'James', 'Geoffrey', 'Lisa', 'Benjamin', 'Stacey', 'Lucy' ] last_names = [ 'Brown', 'Smith', 'Patel', 'Jones', 'Williams', 'Johnson', 'Taylor', 'Thomas', 'Roberts', 'Khan', 'Lewis', 'Jackson', 'Clarke', 'James', 'Phillips', 'Wilson', 'Ali', 'Mason', 'Mitchell', 'Rose', 'Davis', 'Davies', 'Rodriguez', 'Cox', 'Alexander' ] shifts = [ '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '3' ] for i in range(len(first_names)): tmp_email = first_names[i].lower() + "." + last_names[i].lower( ) + "@example.com" user_datastore.create_user(name=first_names[i] + " " + last_names[i], email=tmp_email, password=encrypt_password('user'), roles=[ assembler_role, ], shift_id=shifts[i]) db.session.commit() return
def roleDetail(id): ''' 角色详情 :param id: :return: ''' role = Role.query.get(id) if role is None: return jsonify(code=RET.DATAERR, msg="未查找到数据") #角色属性 attribute = Role.getAttr(id) #角色地区 site = Role.getSite(id) data = { "id": role.id, "name": role.name, "head_portrait": role.head_portrait, "gender": role.gender, "age": role.age, "site": site, "intro": role.intro, "image_list": role.image_list.split(","), "attribute": attribute, "update_time": site.update_time } return jsonify(code=RET.OK, data=data)
def setUp(self): self.app = create_app('testing') self.app_ctx = self.app.app_context() self.app_ctx.push() db.drop_all() db.create_all() Role.seed()
def role_add(user, data, params): devicetype = get_by_devicetypeid_or_404(user, data['deviceTypeId']) name = data['name'] check_unique_role_name(user, name, devicetype) is_device_role = (name == 'device') per_dict = generate_role_dict(devicetype, data['attributePermissions'], is_device_role) new_role = Role(name=name, owner=user, devicetype=devicetype, description=data.get('description', '')) new_role.permissions = per_dict new_role_id = new_role.roleid db.session.add(new_role) db.session.commit() return get_ok_response_body(data=dict(id=new_role_id))
def setUp(self): # 开始 准备 self.app = init_app('testing') self.app_ctx = self.app.app_context() # 推进app self.app_ctx.push() db.drop_all() db.create_all() Role.seed()
def build_sample_db(): """ Populate a small db with some example entries. """ import string import random db.drop_all() db.create_all() with app.app_context(): user_role = Role(name='user') super_user_role = Role(name='superuser') db.session.add(user_role) db.session.add(super_user_role) db.session.commit() test_user = user_datastore.create_user( name='admin', email='admin', password=hash_password('admin'), roles=[super_user_role, user_role]) test_user = user_datastore.create_user( name='zhang', email='zhang', password=hash_password('123123'), roles=[user_role]) roles = [user_role, super_user_role] # # first_names = [ # 'Harry', 'Amelia', 'Oliver', 'Jack', 'Isabella', 'Charlie', 'Sophie', 'Mia', # 'Jacob', 'Thomas', 'Emily', 'Lily', 'Ava', 'Isla', 'Alfie', 'Olivia', 'Jessica', # 'Riley', 'William', 'James', 'Geoffrey', 'Lisa', 'Benjamin', 'Stacey', 'Lucy' # ] # last_names = [ # 'Brown', 'Smith', 'Patel', 'Jones', 'Williams', 'Johnson', 'Taylor', 'Thomas', # 'Roberts', 'Khan', 'Lewis', 'Jackson', 'Clarke', 'James', 'Phillips', 'Wilson', # 'Ali', 'Mason', 'Mitchell', 'Rose', 'Davis', 'Davies', 'Rodriguez', 'Cox', 'Alexander' # ] # # for i in range(len(first_names)): # tmp_email = first_names[i].lower() + "." + last_names[i].lower() + "@example.com" # tmp_pass = ''.join(random.choice(string.ascii_lowercase + string.digits) for i in range(10)) # user_datastore.create_user( # first_name=first_names[i], # last_name=last_names[i], # email=tmp_email, # password=encrypt_password(tmp_pass), # roles=[user_role, ] # ) db.session.commit() return
def create_app(config_class=Config): app = Flask(__name__) app.config.from_object(config_class) db.init_app(app) migrate.init_app(app, db) login.init_app(app) moment.init_app(app) ma.init_app(app) sentry_sdk.init( dsn="https://[email protected]/1852554", integrations=[FlaskIntegration()]) with app.app_context(): from app.auth import bp as auth_bp app.register_blueprint(auth_bp, url_prefix='/auth') from app.main import bp as main_bp app.register_blueprint(main_bp, url_prefix='/') from app.basic_master import bp as basic_master_bp app.register_blueprint(basic_master_bp, url_prefix='/basic_master') from app.main_master import bp as main_master_bp app.register_blueprint(main_master_bp, url_prefix='/main_master') from app.transaction import bp as transaction_bp app.register_blueprint(transaction_bp, url_prefix='/transaction') from app.reports import bp as reports_bp app.register_blueprint(reports_bp, url_prefix='/reports') from app.production import bp as production_bp app.register_blueprint(production_bp, url_prefix='/production') # from app.employee import bp as emp_bp # app.register_blueprint(emp_bp, url_prefix='/employee') # from app.transaction import bp as trans_bp # app.register_blueprint(trans_bp, url_prefix='/transaction') db.create_all() from app.model import Role if Role(name="ADMIN").query.first(): pass else: db.session.add(Role(name='ADMIN')) db.session.commit() return app
def init_db(db): db.create_all() admin_role = Role(name='Admin') mod_role = Role(name='Moderator') user_role = Role(name='User') db.session.add_all([admin_role, mod_role, user_role]) for i in AdminAccount: adminaccount = User(name=i["name"], email=i["email"], role=admin_role, password=i["password"]) db.session.add(adminaccount) db.session.commit()
def deploy(): """Run deployment tasks.""" from flask_migrate import upgrade from app.model import Role, User # migrate database to latest revision upgrade() # create user roles Role.insert_roles() # create self-follows for all users User.add_self_follows()
def setUpClass(cls): try: cls.client = webdriver.Firefox() except: pass if cls.client: cls.app = create_app('testing') cls.app_ctx = cls.app.app_context() cls.app_ctx.push() db.drop_all() db.create_all() Role.seed() threading.Thread(target=cls.app.run).start()
def create_app(config_class=Config): app = Flask(__name__) app.config.from_object(config_class) db.init_app(app) migrate.init_app(app, db) login.init_app(app) # moment.init_app(app) ma.init_app(app) sentry_sdk.init( dsn="https://[email protected]/1759344", integrations=[FlaskIntegration()] ) with app.app_context(): from app.auth import bp as auth_bp app.register_blueprint(auth_bp, url_prefix='/auth') from app.main import bp as main_bp app.register_blueprint(main_bp, url_prefix='/') from app.master import bp as master_bp app.register_blueprint(master_bp, url_prefix='/master') from app.employee import bp as emp_bp app.register_blueprint(emp_bp, url_prefix='/employee') from app.transaction import bp as trans_bp app.register_blueprint(trans_bp, url_prefix='/transaction') from app.report import bp as report_bp app.register_blueprint(report_bp, url_prefix='/reports') db.create_all() from app.model import Role if Role(name="ADMIN").query.first(): pass else: db.session.add(Role(name='ADMIN')) db.session.commit() return app
def create_app(): app.app_context().push() password = '' app.config[ 'SQLALCHEMY_DATABASE_URI'] = 'mysql+mysqldb://root:' + password + '@127.0.0.1/library?charset=utf8mb4' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['WTF_CSRF_ENABLED'] = False app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER app.config['COVER_URL_PATH'] = COVER_URL_PATH import random, time secret_key = random._urandom(24) + bytes(str(time.clock()), encoding='utf-8') app.config['SECRET_KEY'] = secret_key db.init_app(app) from app.model import Role, User, Book_basic # db.drop_all() db.create_all() Role.insert_roles() User.insert_admin() User.insert_default_user() User.insert_teacher() Book_basic.insert_origin_books() bootstrap.init_app(app) login_manager.init_app(app) from app.main import main as main_blueprint from app.auth import auth as auth_blueprint app.register_blueprint(main_blueprint) app.register_blueprint(auth_blueprint) return app
def fillup_data(): admin_role = Role("admin", "admin") guest_role = Role("guest", "guest") admin_role.save() guest_role.save() admin_user = User("*****@*****.**", "admin", "admin", [admin_role]) guest_user = User("*****@*****.**", "guest", "guest", [guest_role]) admin_user.save() guest_user.save()
def roleAdd(): ''' 新增角色 :return:状态 ''' res_dir = request.get_json() if res_dir is None: return jsonify(code=RET.PARAMERR, msg="未接收到参数") name = res_dir.get("name") head_portrait = res_dir.get("head_portrait") gender = res_dir.get("gender") age = res_dir.get("age") attribute = res_dir.get("attribute") site = res_dir.get("site") intro = res_dir.get("intro") image_list = res_dir.get("image_list") if not all([name, head_portrait, gender, attribute, site]): return jsonify(code=RET.PARAMERR, msg="缺少参数") imgStr = '' if len(image_list) > 0: imgStr = ",".join(image_list) role = Role(name=name, head_portrait=head_portrait, gender=gender, age=age, attribute=attribute, site=site, intro=intro, image_list=imgStr) try: db.session.add(role) db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error(e) return jsonify(code=RET.DATAERR, msg="添加失败") return jsonify(code=RET.OK, msg="添加成功")
def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() Role.insert_roles()
def test_roles_and_permissions(self): Role.insert_roles() u = User(email='*****@*****.**', password='******') self.assertTrue(u.can(Permission.WRITE_ARITICLES)) self.assertFalse(u.can(Permission.MODERATE_COMMENTS))
def create_role(name, description=''): role = Role(name=name, description=description) db.session.add(role) db.session.commit()
def update_db(): migrate() from app.model import Role Role.seed()
def adddata(): db.drop_all() db.create_all() from app.model import Role Role.seed()
def deploy(): from app.model import Role upgrade() Role.seed()