def db_add_user(config, user_email): """Adding user""" if not os.path.isfile(config.DB_FILE): print('[WARNING] File [{}] doesn\'t exist.'.format(config.DB_FILE)) sys.exit(1) app = create_app(config_object=config) with app.app_context(): DB.init_app(app) user = User.query.filter_by(email=user_email).first() if user: print('[WARNING] User [{}] is already added. '.format(user_email)) sys.exit(0) admin = User(email=user_email, password=BCRYPT.generate_password_hash(uuid.uuid4().hex), gdpr_version=config.GDPR_VERSION, is_active=True) DB.session.add(admin) DB.session.commit() print( '[SUCCESS] Admin user was set. For activation, you should reset password.' ) sys.exit(0)
def create_access_rights(config): """Create access rights""" with open('rights.yaml', 'r') as stream: roles_rights = yaml.load(stream) if not os.path.isfile(config.DB_FILE): print('[WARNING] File [{}] doesn\'t exist.'.format(config.DB_FILE)) sys.exit(1) app = create_app(config_object=config) with app.app_context(): DB.init_app(app) for group in roles_rights['rights']: for rule in roles_rights['rights'][group]: rights = Rights(name=get_rights_name(group, rule['permission']), group=group, permission=rule['permission'], description=rule['description']) DB.session.add(rights) DB.session.commit() print('[SUCCESS] Rights imported to [{}] file'.format(config.DB_FILE)) sys.exit(0)
def rights_check(config, rights_file): """Check of rights version""" if not os.path.isfile(rights_file): print('[Warning] File [{}] doesn\'t exist.'.format(rights_file)) sys.exit(1) with open(rights_file, 'r') as stream: roles_rights = yaml.load(stream) app = create_app(config_object=config) with app.app_context(): DB.init_app(app) internal = Internal.query.order_by( Internal.rights_version.desc()).first() if internal.rights_version != roles_rights['version']: print( '[WARNING] Rights version [{}] in file [{}] differs from proper [{}].' .format(roles_rights['version'], rights_file, internal.rights_version)) sys.exit(0) print('[SUCCESS] Rights in [{}] file is in correct version [{}].'.format( rights_file, internal.rights_version))
def configure_extensions(app): """ This function configures all extensions used in app. """ DB.init_app(app) with app.app_context(): DB.create_all() DB.session.commit()
def import_rights(config, rights_file): """Import rights to database :param config: Configuration object :param rights_file: File with rights and roles definitions :return: """ with open(rights_file, 'r') as stream: roles_rights = yaml.load(stream) if not os.path.isfile(config.DB_FILE): print('[WARNING] File [{}] doesn\'t exist.'.format(config.DB_FILE)) sys.exit(1) app = create_app(config_object=config) with app.app_context(): DB.init_app(app) rr_scheme = {'rights': [], 'roles': [], 'role_rights': []} for role in roles_rights['roles']: rr_scheme['roles'].append(Role(name=get_role_name(role))) for group in roles_rights['rights']: for rule in roles_rights['rights'][group]: rr_scheme['rights'].append( Rights(name=get_rights_name(group, rule['permission']), group=group, permission=rule['permission'], description=rule['description'])) for role in rule['roles']: rr_scheme['role_rights'].append( RoleRights(role=get_role_name(role), rights=get_rights_name( group, rule['permission']))) for rights in rr_scheme['rights']: DB.session.add(rights) for roles in rr_scheme['roles']: DB.session.add(roles) for role_rights in rr_scheme['role_rights']: DB.session.add(role_rights) DB.session.commit() print('[SUCCESS] Rights imported to [{}] file'.format(config.DB_FILE))
def db_init(config): """Creation of database""" if os.path.isfile(config.DB_FILE): print('[WARNING] File [{}] already exists.'.format(config.DB_FILE)) sys.exit(0) if not os.path.exists(os.path.dirname(config.DB_FILE)): os.makedirs(os.path.dirname(config.DB_FILE)) app = create_app(config_object=config) with app.app_context(): DB.init_app(app) DB.create_all() DB.session.commit() print('[SUCCESS] File [{}] created.'.format(config.DB_FILE)) sys.exit(0)
def register_extensions(app): """Register Flask extensions.""" BCRYPT.init_app(app) DB.init_app(app) MAIL.init_app(app) BOOTSTRAP.init_app(app) bootstrapcdn = WebCDN( "https://stackpath.bootstrapcdn.com/bootstrap/3.3.7/") bootswatchcdn = WebCDN( "https://stackpath.bootstrapcdn.com/bootswatch/3.3.7/") app.extensions['bootstrap']['cdns'].update({ 'bootstrapcdn': bootstrapcdn, 'bootswatchcdn': bootswatchcdn }) CSRF.init_app(app)
def db_check(config): """Check of database""" if not os.path.isfile(config.DB_FILE): print('[WARNING] File [{}] doesn\'t exist.'.format(config.DB_FILE)) sys.exit(1) app = create_app(config_object=config) with app.app_context(): DB.init_app(app) internal = Internal.query.order_by(Internal.db_version.desc()).first() if internal.db_version != config.DB_VERSION: print( '[WARNING] Schema version [{}] in file [{}] differs from proper [{}].' .format(internal.db_version, config.DB_FILE, config.DB_VERSION)) sys.exit(0) print('[SUCCESS] Schema in [{}] file is in correct version [{}].'.format( config.DB_FILE, config.DB_VERSION))
def db_init(config): """Creation of database""" if os.path.isfile(config.DB_FILE): print('[WARNING] File [{}] already exists.'.format(config.DB_FILE)) sys.exit(0) if not os.path.exists(os.path.dirname(config.DB_FILE)): os.makedirs(os.path.dirname(config.DB_FILE)) app = create_app(config_object=config) with app.app_context(): DB.init_app(app) DB.create_all() internal = Internal(db_version=config.DB_VERSION, rights_version=0, updated_at=datetime.now()) DB.session.add(internal) DB.session.commit() print('[SUCCESS] File [{}] created.'.format(config.DB_FILE))
def register_extensions(app): """Register Flask extensions.""" DB.init_app(app) API.init_app(app)
def register_extensions(flask_app: Flask) -> None: DB.init_app(flask_app)