Esempio n. 1
0
def create_hostcountry(session):
    ''' Create some HostCountry to play with for the tests
    '''
    item = model.HostCountry(
        host_id=1,
        country_id=2,
    )
    session.add(item)
    item = model.HostCountry(
        host_id=2,
        country_id=1,
    )
    session.add(item)

    session.commit()
Esempio n. 2
0
def host_country_new(host_id):
    """ Create a new host_country.
    """
    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')

    form = forms.AddHostCountryForm()
    if form.validate_on_submit():
        country_name = form.country.data
        country = mmlib.get_country_by_name(SESSION, country_name)
        if country is None:
            flask.flash('Invalid country code')
            return flask.render_template(
                'host_country_new.html',
                form=form,
                host=hostobj,
            )

        host_country = model.HostCountry()
        host_country.host_id = hostobj.id
        host_country.country_id = country.id
        SESSION.add(host_country)

        try:
            SESSION.flush()
            flask.flash('Host Country added')
        except SQLAlchemyError as err:  # pragma: no cover
            # We cannot check this as there is no unique constraint in the
            # table. So the only situation where it could fail is a failure
            # at the DB server level itself.
            SESSION.rollback()
            flask.flash('Could not add Country to the host')
            APP.logger.debug('Could not add Country to the host')
            APP.logger.exception(err)

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

    return flask.render_template(
        'host_country_new.html',
        form=form,
        host=hostobj,
    )
Esempio n. 3
0
def host_country_new(host_id):
    """ Create a new host_country.
    """
    hostobj = mmlib.get_host(SESSION, host_id)

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

    form = forms.AddHostCountryForm()
    if form.validate_on_submit():
        country_name = form.country.data
        country = mmlib.get_country_by_name(SESSION, country_name)
        if country is None:
            flask.flash('Invalid country code')
            return flask.render_template(
                'host_country_new.html',
                form=form,
                host=hostobj,
            )

        host_country = model.HostCountry()
        host_country.host_id = hostobj.id
        host_country.country_id = country.id
        SESSION.add(host_country)

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

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

    return flask.render_template(
        'host_country_new.html',
        form=form,
        host=hostobj,
    )