def create_deployment(): # dynamically load new clusters class _DeploymentForm(DeploymentForm): pass _DeploymentForm.cluster.kwargs['choices'] = sorted( list( set(_DeploymentForm.cluster.kwargs['choices'] + [(c.id, c.name) for c in Cluster.query.all()]))) # when a new deploment is created, unselect a previous one reset_selected_deployment() form = _DeploymentForm() if form.validate_on_submit(): deployment = Deployment(cluster=Cluster.query.get(form.cluster.data)) del form.cluster form.populate_obj(deployment) db.session.add(deployment) db.session.commit() session['selected_deployment'] = deployment.name flash('Deployment {} created'.format(form.name.data)) audit_log('created deployment', deployment.name) return redirect(url_for('index')) context = get_context() return _render_template('generic_form.html', title='New Deployment', form=form, **context)
def edit_role(role_name): role = Role.query.filter_by(name=role_name).first() form = RoleEditForm(obj=role) # remove disabled keyword from previous calls try: del form.permissions.render_kw['disabled'] except KeyError: pass if form.validate_on_submit(): permissions = json.dumps(form.permissions.data) del form.permissions form.populate_obj(role) role.permissions = permissions db.session.add(role) db.session.commit() flash('Role {} updated'.format(form.name.data)) audit_log('updated role', role.name) return redirect(url_for('index')) # populate permissions multi selector based on db field (json string) if role.permissions: form.permissions.data = json.loads(role.permissions) if role.name == 'admin': form.permissions.render_kw['disabled'] = 'disabled' context = get_context() return _render_template('generic_form.html', title='Edit Role', form=form, **context)
def delete_site(site_name): site = get_db_site(site_name) site.is_deleted = True db.session.commit() audit_log('deleted site', site_name) flash('Site {} has been deleted'.format(site_name)) return redirect(url_for('index'))
def edit_site(site_name): form, template_vars = get_site_edit_form(site_name) if form.validate_on_submit(): if 'delete' in form._fields.keys() and form.delete.data: return redirect(url_for('delete_site', site_name=site_name)) if 'commit' in form._fields.keys() and form.commit.data: site = get_db_site(site_name) site.is_installed = True db.session.commit() flash('Site {} has been committed'.format(site_name)) audit_log('has committed site', site_name) return redirect(url_for('view_site', site_name=site_name)) if 'update' in form._fields.keys() and form.update.data: site = get_db_site(site_name) site.template_parameters = get_template_parameters( form, template_vars) audit_log('updated site:', site_name, '|', 'data:', site.template_parameters) db.session.commit() flash('Site {} has been updated'.format(site_name)) context = get_context() return _render_template('generic_form.html', title='Edit Site {}'.format(site_name), form=form, **context)
def delete_user(user_name): user = User.query.filter_by(username=user_name).first() user.is_deleted = True db.session.commit() action = 'deleted' flash('User {} {}'.format(user.username, action)) audit_log(action, 'user', user.username) return redirect(url_for('index'))
def edit_cluster(cluster_name): cluster = Cluster.query.filter_by(name=cluster_name).first() form = ClusterEditForm(obj=cluster) if form.validate_on_submit(): form.populate_obj(cluster) db.session.add(cluster) db.session.commit() flash('Cluster {} updated'.format(form.name.data)) audit_log('updated cluster', cluster.name) return redirect(url_for('index')) context = get_context() return _render_template( 'generic_form.html', title='Edit Cluster', form=form, **context)
def create_cluster(): form = ClusterCreateForm() if form.validate_on_submit(): cluster = Cluster() form.populate_obj(cluster) db.session.add(cluster) db.session.commit() flash('Cluster {} created'.format(form.name.data)) audit_log('created cluster', cluster.name) return redirect(url_for('index')) context = get_context() return _render_template( 'generic_form.html', title='New Cluster', form=form, **context)
def view_site(site_name): form, template_vars = get_site_edit_form(site_name) del (form.commit) del (form.update) for field in template_vars: getattr(form, field).render_kw = {'readonly': True} context = get_context() audit_log('viewed site', site_name) return _render_template('generic_form.html', title='View Site {}'.format(site_name), action='/site/{}'.format(site_name), form=form, **context)
def change_user(form, user, action): # store seleted role to add it properly to the user object later role = Role.query.filter_by(name=form.role.data).first() if form.password.data: password_hash = generate_password_hash(form.password.data) user.password_hash = password_hash del form.role del form.password form.populate_obj(user) user.role = role db.session.add(user) db.session.commit() flash('User {} {}'.format(user.username, action)) audit_log(action, 'user', user.username) return redirect(url_for('index'))
def create_role(): form = RoleCreateForm() if form.validate_on_submit(): role = Role(permissions=json.dumps(form.permissions.data)) del form.permissions form.populate_obj(role) db.session.add(role) db.session.commit() flash('Role {} created'.format(form.name.data)) audit_log('created role', role.name) return redirect(url_for('index')) context = get_context() return _render_template('generic_form.html', title='New Role', form=form, **context)
def login(): if current_user.is_authenticated: return redirect(url_for('index')) form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by( username=form.username.data, is_deleted=False).first() if user is None or not user.check_password(form.password.data): flash('Invalid username or password', 'danger') return redirect(url_for('login')) login_user(user) audit_log('logged in') next_page = request.args.get('next') if not next_page or url_parse(next_page).netloc != '': next_page = url_for('index') return redirect(next_page) return _render_template('login.html', title='Login - Hyper-128T', form=form)
def change_password(): form = PasswordChangeForm() if form.validate_on_submit(): error = '' if not current_user.check_password(form.current_password.data): error = 'Current password is not correct' if form.new_password.data != form.new_password_confirm.data: error = 'New passwords do not match' if not error: current_user.password_hash = generate_password_hash( form.new_password.data) db.session.commit() flash('Your password has been changed successfully') audit_log('changed password') return redirect(url_for('index')) flash(error, 'danger') context = get_context() return _render_template('generic_form.html', title='Change Password', form=form, form_type='basic', **context)
def export(): data = { 'users': get_users(), 'roles': get_roles(), 'deployments': get_deployments(), 'clusters': get_clusters(), } content = yaml.dump(data) filename = 'export-hyper-128t-webui-{}-{:%Y-%m-%d_%H-%M-%S}.yaml'.format( current_user.username, datetime.now()) audit_log('requested a data export') with open(os.path.join(app.config.get('EXPORTS_DIR'), filename), 'w') as fd: fd.write(content) return Response(content, mimetype='text/yaml', headers={ 'Content-disposition': 'attachment; filename={}'.format(filename) }) return redirect(url_for('index'))
def create_site(): selected_template = request.args.get('template') form = get_site_form(selected_template) if form.validate_on_submit(): site_name = form.name.data template_name = form.template.data # The used template should be copied to the data backend # to ensure changes on the template file don't interfere # with the uncommitted site. # Upcoming edits are soley done on this stored template content with open(os.path.join(TEMPLATE_DIR, template_name), 'rb') as fd: template_content = base64.b64encode(gzip.compress( fd.read())).decode('ascii') template_vars = get_template_variables(template_name) site = Site( name=site_name, template_name=template_name, template_content=template_content, template_parameters=get_template_parameters(form, template_vars), ) selected_deployment = get_selected_deployment() if selected_deployment: site.deployment = selected_deployment db.session.add(site) db.session.commit() flash('Site {} created'.format(form.name.data)) audit_log('created site:', site_name, '|', 'template:', template_name, '|', 'data:', site.template_parameters) return redirect(url_for('edit_site', site_name=site_name)) else: if form.errors: flash(form.errors) form.process() # needed to send the "selected" option to html context = get_context() return _render_template('generic_form.html', title='New Site', form=form, **context)
def logout(): reset_selected_deployment() audit_log('logged out') logout_user() return redirect(url_for('login'))