def hierarchy_insert(category: str) -> Union[str, Response]: form = build_form('hierarchy', code=category) form.classes.choices = Type.get_class_choices() if form.validate_on_submit(): if Type.check_hierarchy_exists(form.name.data): flash(_('error name exists'), 'error') return render_template('display_form.html', form=form) try: Transaction.begin() type_ = Entity.insert('type', sanitize(form.name.data)) Type.insert_hierarchy( type_, # type: ignore category, form.classes.data, is_multiple(form, category)) type_.update(process_form_data(form, type_)) Transaction.commit() except Exception as e: # pragma: no cover Transaction.rollback() logger.log('error', 'database', 'transaction failed', e) flash(_('error transaction'), 'error') abort(418) flash(_('entity created'), 'info') return redirect(f"{url_for('type_index')}#menu-tab-{category}") return render_template( 'display_form.html', form=form, manual_page='entity/type', title=_('types'), crumbs=[ [_('types'), url_for('type_index')], f'+ {uc_first(_(category))}'])
def hierarchy_update(id_: int) -> Union[str, Response]: hierarchy = g.types[id_] if hierarchy.category in ('standard', 'system'): abort(403) form = build_form('hierarchy', hierarchy) form.classes.choices = Type.get_class_choices(hierarchy) linked_entities = set() has_multiple_links = False for entity in get_entities_linked_to_type_recursive(id_, []): if entity.id in linked_entities: has_multiple_links = True break linked_entities.add(entity.id) if hasattr(form, 'multiple') and has_multiple_links: form.multiple.render_kw = {'disabled': 'disabled'} if form.validate_on_submit(): if form.name.data != hierarchy.name and Type.get_types(form.name.data): flash(_('error name exists'), 'error') else: Transaction.begin() try: Type.update_hierarchy(hierarchy, sanitize(form.name.data), form.classes.data, multiple=(hierarchy.category == 'value' or (hasattr(form, 'multiple') and form.multiple.data) or has_multiple_links)) hierarchy.update(process_form_data(form, hierarchy)) Transaction.commit() except Exception as e: # pragma: no cover Transaction.rollback() logger.log('error', 'database', 'transaction failed', e) flash(_('error transaction'), 'error') abort(418) flash(_('info update'), 'info') tab = 'value' if g.types[id_].category == 'value' else 'custom' return redirect( f"{url_for('type_index')}#menu-tab-{tab}_collapse-{hierarchy.id}") form.multiple = hierarchy.multiple table = Table(paging=False) for class_name in hierarchy.classes: count = Type.get_form_count(hierarchy, class_name) table.rows.append([ g.classes[class_name].label, format_number(count) if count else link( _('remove'), url_for( 'remove_class', id_=hierarchy.id, class_name=class_name)) ]) return render_template('display_form.html', form=form, table=table, manual_page='entity/type', title=_('types'), crumbs=[[_('types'), url_for('type_index')], hierarchy, _('edit')])