Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
def create_user():
    form = UserCreateForm()
    if form.validate_on_submit():
        user = User()
        return change_user(form, user, 'created')
    context = get_context()
    return _render_template('generic_form.html',
                            title='New User',
                            form=form,
                            **context)
Ejemplo n.º 5
0
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)
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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)
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
def edit_user(user_name):
    form = UserEditForm()
    user = User.query.filter_by(username=user_name).first()
    if form.validate_on_submit():
        if form.update.data:
            return change_user(form, user, 'updated')
        if form.delete.data:
            return redirect(url_for('delete_user', user_name=user.username))

    form = UserEditForm(obj=user)
    form.role.data = user.role.name
    context = get_context()
    return _render_template('generic_form.html',
                            title='Edit User',
                            form=form,
                            **context)
Ejemplo n.º 10
0
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)
Ejemplo n.º 11
0
def purge():
    form = PurgeForm()
    objects = {}
    objects['Sites'] = list(Site.query.filter_by(is_deleted=True))
    objects['Deployments'] = list(Deployment.query.filter_by(is_deleted=True))
    objects['Users'] = list(User.query.filter_by(is_deleted=True))
    num_objects = sum([len(v) for k, v in objects.items()])
    if form.validate_on_submit():
        for l in objects.values():
            for item in l:
                db.session.delete(item)
        db.session.commit()
        return redirect(url_for('index'))
    context = get_context()
    return _render_template('purge.html', title='Purge Deleted Objects',
                            form=form, deleted_objects=objects,
                            num_objects=num_objects, **context)
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
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)
Ejemplo n.º 14
0
def index():
    context = get_context()
    return _render_template('index.html', title='Home', **context)