Ejemplo n.º 1
0
def host_category_url_new(host_id, hc_id):
    """ Create a new host_category_url.
    """
    hostobj = mmlib.get_host(SESSION, host_id)

    if hostobj is None:
        flask.abort(404, 'Host not found')

    if not (is_site_admin(flask.g.fas_user, hostobj.site)
            or is_mirrormanager_admin(flask.g.fas_user)):
        flask.abort(403, 'Access denied')

    hcobj = mmlib.get_host_category(SESSION, hc_id)

    if hcobj is None:
        flask.abort(404, 'Host/Category not found')

    host_cat_ids = [cat.id for cat in hostobj.categories]

    if hcobj.id not in host_cat_ids:
        flask.abort(404, 'Category not associated with this host')

    categories = mmlib.get_categories(SESSION)

    form = forms.AddHostCategoryUrlForm()

    if form.validate_on_submit():

        host_category_u = model.HostCategoryUrl()
        host_category_u.host_category_id = hcobj.id
        form.populate_obj(obj=host_category_u)

        url = form.url.data.strip()
        if url.endswith('/'):
            url = url[:-1]
        host_category_u.url = url

        SESSION.add(host_category_u)

        try:
            SESSION.flush()
            flask.flash('Host Category URL added')
        except SQLAlchemyError as err:
            SESSION.rollback()
            flask.flash('Could not add Category URL to the host')
            APP.logger.debug('Could not add Category URL to the host')
            APP.logger.exception(err)

        SESSION.commit()
        return flask.redirect(
            flask.url_for('host_category', host_id=host_id, hc_id=hc_id))

    return flask.render_template(
        'host_category_url_new.html',
        form=form,
        host=hostobj,
        hostcategory=hcobj,
    )
Ejemplo n.º 2
0
def host_category_url_new(host_id, hc_id):
    """ Create a new host_category_url.
    """
    hostobj = mmlib.get_host(SESSION, host_id)

    if hostobj is None:
        flask.abort(404, 'Host not found')

    if not (is_site_admin(flask.g.fas_user, hostobj.site)
            or is_mirrormanager_admin(flask.g.fas_user)):
        flask.abort(403, 'Access denied')

    hcobj = mmlib.get_host_category(SESSION, hc_id)

    if hcobj is None:
        flask.abort(404, 'Host/Category not found')

    host_cat_ids = [cat.id for cat in hostobj.categories]

    if hcobj.id not in host_cat_ids:
        flask.abort(404, 'Category not associated with this host')

    categories = mmlib.get_categories(SESSION)

    form = forms.AddHostCategoryUrlForm()

    if form.validate_on_submit():

        host_category_u = model.HostCategoryUrl()
        host_category_u.host_category_id = hcobj.id
        form.populate_obj(obj=host_category_u)

        url = form.url.data.strip()
        if url.endswith('/'):
            url = url[:-1]
        host_category_u.url = url

        SESSION.add(host_category_u)

        try:
            SESSION.flush()
            flask.flash('Host Category URL added')
        except SQLAlchemyError as err:
            SESSION.rollback()
            flask.flash('Could not add Category URL to the host')
            APP.logger.debug('Could not add Category URL to the host')
            APP.logger.exception(err)

        SESSION.commit()
        return flask.redirect(
            flask.url_for('host_category', host_id=host_id, hc_id=hc_id))

    return flask.render_template(
        'host_category_url_new.html',
        form=form,
        host=hostobj,
        hostcategory=hcobj,
    )
Ejemplo n.º 3
0
def host_category(host_id, hc_id):
    """ View a host_category.
    """
    hostobj = mmlib.get_host(SESSION, host_id)

    if hostobj is None:
        flask.abort(404, 'Host not found')

    if not (is_site_admin(flask.g.fas_user, hostobj.site)
            or is_mirrormanager_admin(flask.g.fas_user)):
        flask.abort(403, 'Access denied')

    hcobj = mmlib.get_host_category(SESSION, hc_id)

    if hcobj is None:
        flask.abort(404, 'Host/Category not found')

    host_cat_ids = [cat.id for cat in hostobj.categories]

    if hcobj.id not in host_cat_ids:
        flask.abort(404, 'Category not associated with this host')

    categories = mmlib.get_categories(SESSION)

    form = forms.EditHostCategoryForm(obj=hcobj)

    if form.validate_on_submit() and is_mirrormanager_admin(flask.g.fas_user):

        form.populate_obj(obj=hcobj)

        try:
            SESSION.flush()
            flask.flash('Host Category updated')
        except SQLAlchemyError as err:  # pragma: no cover
            # We cannot check this because the code check before updating
            # and therefore the only situation where it could fail is a
            # failure at the DB server level itself.
            SESSION.rollback()
            flask.flash('Could not update Category to the host')
            APP.logger.debug('Could not update Category to the host')
            APP.logger.exception(err)

        SESSION.commit()
        return flask.redirect(
            flask.url_for('host_category', host_id=hostobj.id, hc_id=hcobj.id))

    return flask.render_template(
        'host_category.html',
        form=form,
        host=hostobj,
        hostcategory=hcobj,
    )
Ejemplo n.º 4
0
def host_category(host_id, hc_id):
    """ View a host_category.
    """
    hostobj = mmlib.get_host(SESSION, host_id)

    if hostobj is None:
        flask.abort(404, 'Host not found')

    if not (is_site_admin(flask.g.fas_user, hostobj.site)
            or is_mirrormanager_admin(flask.g.fas_user)):
        flask.abort(403, 'Access denied')

    hcobj = mmlib.get_host_category(SESSION, hc_id)

    if hcobj is None:
        flask.abort(404, 'Host/Category not found')

    host_cat_ids = [cat.id for cat in hostobj.categories]

    if hcobj.id not in host_cat_ids:
        flask.abort(404, 'Category not associated with this host')

    categories = mmlib.get_categories(SESSION)

    form = forms.EditHostCategoryForm(obj=hcobj)

    if form.validate_on_submit() and is_mirrormanager_admin(flask.g.fas_user):

        form.populate_obj(obj=hcobj)

        try:
            SESSION.flush()
            flask.flash('Host Category updated')
        except SQLAlchemyError as err:  # pragma: no cover
            # We cannot check this because the code check before updating
            # and therefore the only situation where it could fail is a
            # failure at the DB server level itself.
            SESSION.rollback()
            flask.flash('Could not update Category to the host')
            APP.logger.debug('Could not update Category to the host')
            APP.logger.exception(err)

        SESSION.commit()
        return flask.redirect(
            flask.url_for('host_category', host_id=hostobj.id, hc_id=hcobj.id))

    return flask.render_template(
        'host_category.html',
        form=form,
        host=hostobj,
        hostcategory=hcobj,
    )
Ejemplo n.º 5
0
def host_category_new(host_id):
    """ Create a new host_category.
    """
    hostobj = mmlib.get_host(SESSION, host_id)

    if hostobj is None:
        flask.abort(404, 'Host not found')

    if not (is_site_admin(flask.g.fas_user, hostobj.site)
            or is_mirrormanager_admin(flask.g.fas_user)):
        flask.abort(403, 'Access denied')

    categories = mmlib.get_categories(SESSION)

    form = forms.AddHostCategoryForm(categories=categories)

    if flask.request.method == 'POST':
        try:
            form.category_id.data = int(form.category_id.data)
        except ValueError:
            pass

    if form.validate_on_submit():

        host_category = model.HostCategory()
        host_category.host_id = hostobj.id
        form.populate_obj(obj=host_category)
        host_category.category_id = int(host_category.category_id)
        SESSION.add(host_category)

        try:
            SESSION.commit()
            flask.flash('Host Category added')
            return flask.redirect(
                flask.url_for(
                    'host_category',
                    host_id=hostobj.id,
                    hc_id=host_category.id))
        except SQLAlchemyError as err:
            SESSION.rollback()
            flask.flash('Could not add Category to the host')
            APP.logger.debug('Could not add Category to the host')
            APP.logger.exception(err)

    return flask.render_template(
        'host_category_new.html',
        form=form,
        host=hostobj,
    )
Ejemplo n.º 6
0
def host_category_new(host_id):
    """ Create a new host_category.
    """
    hostobj = mmlib.get_host(SESSION, host_id)

    if hostobj is None:
        flask.abort(404, 'Host not found')

    if not (is_site_admin(flask.g.fas_user, hostobj.site)
            or is_mirrormanager_admin(flask.g.fas_user)):
        flask.abort(403, 'Access denied')

    categories = mmlib.get_categories(SESSION)

    form = forms.AddHostCategoryForm(categories=categories)

    if flask.request.method == 'POST':
        try:
            form.category_id.data = int(form.category_id.data)
        except ValueError:
            pass

    if form.validate_on_submit():

        host_category = model.HostCategory()
        host_category.host_id = hostobj.id
        form.populate_obj(obj=host_category)
        host_category.category_id = int(host_category.category_id)
        SESSION.add(host_category)

        try:
            SESSION.commit()
            flask.flash('Host Category added')
            return flask.redirect(
                flask.url_for(
                    'host_category',
                    host_id=hostobj.id,
                    hc_id=host_category.id))
        except SQLAlchemyError as err:
            SESSION.rollback()
            flask.flash('Could not add Category to the host')
            APP.logger.debug('Could not add Category to the host')
            APP.logger.exception(err)

    return flask.render_template(
        'host_category_new.html',
        form=form,
        host=hostobj,
    )
Ejemplo n.º 7
0
def host_category_url_new(host_id, hc_id):
    """ Create a new host_category_url.
    """
    hostobj = mmlib.get_host(SESSION, host_id)

    if hostobj is None:
        flask.abort(404, 'Host not found')

    hcobj = mmlib.get_host_category(SESSION, hc_id)

    if hcobj is None:
        flask.abort(404, 'Host/Category not found')

    categories = mmlib.get_categories(SESSION)

    form = forms.AddHostCategoryUrlForm()

    if form.validate_on_submit():

        host_category_u = model.HostCategoryUrl()
        host_category_u.host_category_id = hcobj.id
        form.populate_obj(obj=host_category_u)
        SESSION.add(host_category_u)

        try:
            SESSION.flush()
            flask.flash('Host Category URL added')
        except SQLAlchemyError as err:
            SESSION.rollback()
            flask.flash('Could not add Category URL to the host')
            APP.logger.debug('Could not add Category URL to the host')
            APP.logger.exception(err)

        SESSION.commit()
        return flask.redirect(
            flask.url_for('host_category', host_id=host_id, hc_id=hc_id))

    return flask.render_template(
        'host_category_url_new.html',
        form=form,
        host=hostobj,
        hostcategory=hcobj,
    )
Ejemplo n.º 8
0
def host_category_url_new(host_id, hc_id):
    """ Create a new host_category_url.
    """
    hostobj = mmlib.get_host(SESSION, host_id)

    if hostobj is None:
        flask.abort(404, 'Host not found')

    hcobj = mmlib.get_host_category(SESSION, hc_id)

    if hcobj is None:
        flask.abort(404, 'Host/Category not found')

    categories = mmlib.get_categories(SESSION)

    form = forms.AddHostCategoryUrlForm()

    if form.validate_on_submit():

        host_category_u = model.HostCategoryUrl()
        host_category_u.host_category_id = hcobj.id
        form.populate_obj(obj=host_category_u)
        SESSION.add(host_category_u)

        try:
            SESSION.flush()
            flask.flash('Host Category URL added')
        except SQLAlchemyError as err:
            SESSION.rollback()
            flask.flash('Could not add Category URL to the host')
            APP.logger.debug('Could not add Category URL to the host')
            APP.logger.exception(err)

        SESSION.commit()
        return flask.redirect(
            flask.url_for('host_category', host_id=host_id, hc_id=hc_id))

    return flask.render_template(
        'host_category_url_new.html',
        form=form,
        host=hostobj,
        hostcategory=hcobj,
    )
Ejemplo n.º 9
0
def host_category(host_id, hc_id):
    """ View a host_category.
    """
    hostobj = mmlib.get_host(SESSION, host_id)

    if hostobj is None:
        flask.abort(404, 'Host not found')

    hcobj = mmlib.get_host_category(SESSION, hc_id)

    if hcobj is None:
        flask.abort(404, 'Host/Category not found')

    categories = mmlib.get_categories(SESSION)

    form = forms.EditHostCategoryForm(obj=hcobj)

    if form.validate_on_submit():

        form.populate_obj(obj=hcobj)

        try:
            SESSION.flush()
            flask.flash('Host Category updated')
        except SQLAlchemyError as err:
            SESSION.rollback()
            flask.flash('Could not update Category to the host')
            APP.logger.debug('Could not update Category to the host')
            APP.logger.exception(err)

        SESSION.commit()
        return flask.redirect(
            flask.url_for('host_category', host_id=hostobj.id, hc_id=hcobj.id))

    return flask.render_template(
        'host_category.html',
        form=form,
        host=hostobj,
        hostcategory=hcobj,
    )
Ejemplo n.º 10
0
def host_category(host_id, hc_id):
    """ View a host_category.
    """
    hostobj = mmlib.get_host(SESSION, host_id)

    if hostobj is None:
        flask.abort(404, 'Host not found')

    hcobj = mmlib.get_host_category(SESSION, hc_id)

    if hcobj is None:
        flask.abort(404, 'Host/Category not found')

    categories = mmlib.get_categories(SESSION)

    form = forms.EditHostCategoryForm(obj=hcobj)

    if form.validate_on_submit():

        form.populate_obj(obj=hcobj)

        try:
            SESSION.flush()
            flask.flash('Host Category updated')
        except SQLAlchemyError as err:
            SESSION.rollback()
            flask.flash('Could not update Category to the host')
            APP.logger.debug('Could not update Category to the host')
            APP.logger.exception(err)

        SESSION.commit()
        return flask.redirect(
            flask.url_for('host_category', host_id=hostobj.id, hc_id=hcobj.id))

    return flask.render_template(
        'host_category.html',
        form=form,
        host=hostobj,
        hostcategory=hcobj,
    )