def create_hostcategory(session): ''' Create some HostCategory to play with for the tests ''' item = model.HostCategory( host_id=1, category_id=1, always_up2date=True, ) session.add(item) item = model.HostCategory( host_id=1, category_id=2, always_up2date=True, ) session.add(item) item = model.HostCategory( host_id=2, category_id=1, always_up2date=False, ) session.add(item) item = model.HostCategory( host_id=2, category_id=2, always_up2date=False, ) session.add(item) session.commit()
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, )