def type_delete_recursive(id_: int) -> Union[str, Response]: class DeleteRecursiveTypesForm(FlaskForm): confirm_delete = BooleanField( _("I'm sure to delete this type, it's subs and links"), default=False, validators=[InputRequired()]) save = SubmitField(_('delete types and remove all links')) type_ = g.types[id_] root = g.types[type_.root[0]] if type_.root else None if type_.category in ('standard', 'system', 'place') and not root: abort(403) form = DeleteRecursiveTypesForm() if form.validate_on_submit() and form.confirm_delete.data: for sub_id in Type.get_all_sub_ids(type_): g.types[sub_id].delete() type_.delete() flash(_('types deleted'), 'info') logger.log_user(id_, 'Recursive type delete') return redirect( url_for('view', id_=root.id) if root else url_for('type_index')) tabs = { 'info': Tab('info', content=_( 'Warning: this type has subs and/or links to entities ' '(see tabs). Please check if you want to delete these subs ' 'and links too.'), form=form), 'subs': Tab('subs', entity=type_), 'entities': Tab('entities', entity=type_) } for sub_id in Type.get_all_sub_ids(type_): sub = g.types[sub_id] tabs['subs'].table.rows.append([link(sub), sub.count, sub.description]) for item in get_entities_linked_to_type_recursive(type_.id, []): data = [link(item), item.class_.label, item.description] tabs['entities'].table.rows.append(data) crumbs = [[_('types'), url_for('type_index')]] if root: crumbs += [g.types[type_id] for type_id in type_.root] crumbs += [type_, _('delete')] return render_template('tabs.html', tabs=tabs, crumbs=crumbs)
def check_single_type_duplicates() -> list[dict[str, Any]]: from openatlas.models.type import Type from openatlas.models.entity import Entity data = [] for type_ in g.types.values(): if type_.root or type_.multiple or type_.category == 'value': continue # pragma: no cover type_ids = Type.get_all_sub_ids(type_) if not type_ids: continue # pragma: no cover for id_ in Db.check_single_type_duplicates(type_ids): offending_types = [] entity = Entity.get_by_id(id_, types=True) for entity_types in entity.types: if g.types[entity_types.root[0]].id != type_.id: continue # pragma: no cover offending_types.append(entity_types) data.append({ 'entity': entity, 'type': type_, 'offending_types': offending_types }) return data
def get_sub_ids(id_: int, subs: list[Any]) -> list[Any]: new_subs = Type.get_all_sub_ids(g.types[id_]) subs.extend(new_subs) for sub in new_subs: get_sub_ids(sub, subs) return subs