def reset_admin(): admin = User.query.filter_by(username=admin_user) admin.password = user_manager.hash_password(admin_pw) admin.roles = Role.query.all() admin.active = True db.session.commit() db.session.close()
def user_password_reset(username): from app import user_manager password_form = PasswordForm() success = False if password_form.validate_on_submit(): user = User.query.filter_by(username=username).first() user.password = user_manager.hash_password(password_form.password.data) db.session.add(user) db.session.commit() success = True return render_template('user_password_reset.html', form=password_form, success=success)
def add_user(username, roles=[]): roles = list(roles) print(f'Adding user={username}; roles={roles}') if not User.query.filter(User.username == username).first(): user = User( username=username, password=user_manager.hash_password( input(f"Insert password for {username}: ")), ) for role in roles: user.roles.append(Role(name=role)) db.session.add(user) db.session.commit() print(f"User {username} successfully added!") else: raise ConfigError(f"Username {username} already exists!")
def create_data(): """populate db with initial data""" role1 = Role(name='user') role2 = Role(name='admin') for i in xrange(1, 6): _user = User( username='******' % i, email='*****@*****.**' % i, is_enabled=True, active=True, first_name=random_name(), last_name=random_name(), password=user_manager.hash_password('Password1'), confirmed_at=datetime.utcnow(), ) _user.roles.append(role1) db.session.add(_user) db.session.commit()
#!/usr/bin/env python from app import app, db, user_manager import config from werkzeug.wsgi import DispatcherMiddleware # Preload the DB so we can drop it any time. from app.models import User, PatchSpec, FeatureSpec, Estimator u = User.ifNew(username="******") if u: u.password = user_manager.hash_password("jjpass") u.is_enabled = True db.session.add(u) u = User.ifNew(username="******") if u: u.password = user_manager.hash_password("genpass") u.is_enabled = True db.session.add(u) u = User.ifNew(username="******") if u: u.password = user_manager.hash_password("dariuspass") u.is_enabled = True db.session.add(u) ps = PatchSpec.ifNew(name='Sparse') if ps: ps.width = 400 ps.height = 400 ps.xoverlap = 0.5
def regenerate(): # ensure session is closed db.session.close() # regenerate all tables in the database, based on models db.drop_all() db.create_all() # add some initial data for sitename in site_names: site = Site(name=sitename) site.inbuildings_config = InbuildingsConfig(enabled=False, key="") db.session.add(site) for typename in asset_types: asset_type = AssetType(name=typename) db.session.add(asset_type) for pointname in point_types: point_type = PointType(name=pointname) db.session.add(point_type) site = Site.query.filter_by(name=site_names[0]).one() asset_type = AssetType.query.filter_by(name=asset_types[0]).one() asset = Asset(name=test_asset_name, site=site, type=asset_type, health=0, priority=1, notes='') # add one of each point type to dummy unit for pointtype in point_types: point = PointType.query.filter_by(name=pointtype).one() asset_point = AssetPoint(name=pointtype, type=point, asset=asset) asset.points.append(asset_point) # add and commit to db db.session.add(asset) db.session.commit() # map assets to algorithms map_all() # generate users admin = User(username=admin_user, password=user_manager.hash_password(admin_pw), active=True) test = User(username=test_user, password=user_manager.hash_password(test_pw), active=True) db.session.add(admin) db.session.add(test) # generate roles for role_name in roles: role = Role(name=role_name, label=role_name) admin.roles.append(role) db.session.commit() db.session.close()
def seed_confirmed_user(username, password): return User.create(username=username, is_enabled=True, password=user_manager.hash_password(password), confirmed_at = datetime.now())
def seed_nonconfirmed_user(username, password): return User.create(username=username, is_enabled=False, password=user_manager.hash_password(password))
def is_valid_password(self, password): from app import user_manager password = user_manager.hash_password(password) return user_manager.verify_password(password, self.password)
def set_password(self, password): from app import user_manager self.password = user_manager.hash_password(password)
def __init__(self, **kwargs): from app import user_manager if 'password' in kwargs: kwargs['password'] = user_manager.hash_password(kwargs['password']) super(User, self).__init__(**kwargs)