diff --git a/apis_ontology/tables.py b/apis_ontology/tables.py new file mode 100644 index 0000000..4882470 --- /dev/null +++ b/apis_ontology/tables.py @@ -0,0 +1,69 @@ +import django_tables2 as tables +from apis_core.relations.tables import RelationTable +from apis_core.relations.models import Relation + + +class CustomRelationTableEdit(RelationTable): + + id = tables.TemplateColumn("{{ record.id }}") + subject = tables.TemplateColumn( + "{{record.subj}} ({{record.subj.pk}})" + ) + object = tables.TemplateColumn( + "{{record.obj}} ({{record.obj.pk}})" + ) + description = tables.TemplateColumn("{{ record.name }}") + edit = tables.TemplateColumn( + "Edit" + ) + delete = tables.TemplateColumn(template_name="tables/delete.html") + confidence = tables.TemplateColumn("{{ record.confidence }}") + support_notes = tables.TemplateColumn( + "{{ record.support_notes|default:''|truncatechars:30 }}\n{{record.notes|default:''|truncatechars:30}}" + ) + tei_refs = tables.TemplateColumn("TEI") + + class Meta: + model = Relation + fields = [ + "id", + "subject", + "description", + "object", + "confidence", + "support_notes", + "tei_refs", + "edit", + ] + sequence = tuple(fields) + + +class CustomRelationTableView(RelationTable): + + id = tables.TemplateColumn("{{ record.id }}") + subject = tables.TemplateColumn( + "{{record.subj}} ({{record.subj.pk}})" + ) + object = tables.TemplateColumn( + "{{record.obj}} ({{record.obj.pk}})" + ) + description = tables.TemplateColumn("{{ record.name }}") + confidence = tables.TemplateColumn("{{ record.confidence }}") + support_notes = tables.TemplateColumn( + "{{ record.support_notes|default:''|truncatechars:30 }}\n{{record.notes|default:''|truncatechars:30}}" + ) + tei_refs = tables.TemplateColumn("TEI") + + class Meta: + model = Relation + fields = [ + "id", + "subject", + "description", + "object", + "confidence", + "support_notes", + "tei_refs", + ] + exclude = ["edit", "delete"] + sequence = tuple(fields) diff --git a/apis_ontology/templates/apis_entities/detail_views/detail_generic.html b/apis_ontology/templates/apis_entities/detail_views/detail_generic.html index d0aabba..3ded00f 100644 --- a/apis_ontology/templates/apis_entities/detail_views/detail_generic.html +++ b/apis_ontology/templates/apis_entities/detail_views/detail_generic.html @@ -6,6 +6,8 @@ {% load relations %} +{% load custom_relations %} + {% relations_css %} {% block title %}{{ object }}{% endblock %} @@ -318,7 +320,7 @@

- {% relations_table instance=object tocontenttype=contenttype as table %} + {% custom_relations_table_view instance=object tocontenttype=contenttype as table %} {% render_table table %}
diff --git a/apis_ontology/templates/apis_entities/edit_generic.html b/apis_ontology/templates/apis_entities/edit_generic.html index c129167..96a1c5c 100644 --- a/apis_ontology/templates/apis_entities/edit_generic.html +++ b/apis_ontology/templates/apis_entities/edit_generic.html @@ -5,6 +5,8 @@ {{ block.super }} {% load relations %} +{% load custom_relations %} + {% relations_css %} {% if highlighter_active %}
- {% relations_table instance=instance tocontenttype=contenttype as table %} + {% custom_relations_table_edit instance=instance tocontenttype=contenttype as table %} {% render_table table %} {% relations_links instance=instance tocontenttype=contenttype htmx=True %} diff --git a/apis_ontology/templates/relations/list.html b/apis_ontology/templates/relations/list.html index 82c6d4e..584056f 100644 --- a/apis_ontology/templates/relations/list.html +++ b/apis_ontology/templates/relations/list.html @@ -15,6 +15,7 @@

{{ model.name }}{{ object }}

{% endif %} {% if form %} + {{ form.errors }} {{ form.non_field_errors }} {% load crispy_forms_tags %} diff --git a/apis_ontology/templatetags/custom_relations.py b/apis_ontology/templatetags/custom_relations.py new file mode 100644 index 0000000..56d0ede --- /dev/null +++ b/apis_ontology/templatetags/custom_relations.py @@ -0,0 +1,116 @@ +from django import template +from apis_core.relations import utils +from django.contrib.contenttypes.models import ContentType +from django.db.models import Q +from apis_ontology.tables import CustomRelationTableEdit, CustomRelationTableView +from django_tables2.tables import table_factory + +register = template.Library() + + +@register.simple_tag +def custom_relations_table_edit(relationtype=None, instance=None, tocontenttype=None): + """ + List all relations of type `relationtype` that go from `instance` to + something with type `contenttype`. + If no `tocontenttype` is passed, it lists all relations from and to + instance. + If no `relationtype` is passed, it lists all relations. + """ + model = None + existing_relations = list() + + if tocontenttype: + model = tocontenttype.model_class() + + if relationtype: + relation_types = [relationtype] + else: + # special case: when the contenttype is the same as the contenttype of + # the instance, we don't want *all* the relations where the instance + # occurs, but only those where it occurs together with another of its + # type + if instance and ContentType.objects.get_for_model(instance) == tocontenttype: + relation_types = utils.relation_content_types(combination=(model, model)) + else: + relation_types = utils.relation_content_types(any_model=model) + + for rel in relation_types: + if instance: + existing_relations.extend( + list( + rel.model_class().objects.filter(Q(subj=instance) | Q(obj=instance)) + ) + ) + else: + existing_relations.extend(list(rel.model_class().objects.all())) + + cssid = "table" + if model: + cssid += f"_{tocontenttype.name}" + else: + cssid += "_relations" + attrs = { + "class": "table table-hover table-striped table-condensed", + "hx-swap-oob": "true", + "id": cssid, + } + + table = CustomRelationTableEdit + if model: + table = table_factory(model, CustomRelationTableEdit) + return table(existing_relations, attrs=attrs) + + +@register.simple_tag +def custom_relations_table_view(relationtype=None, instance=None, tocontenttype=None): + """ + List all relations of type `relationtype` that go from `instance` to + something with type `contenttype`. + If no `tocontenttype` is passed, it lists all relations from and to + instance. + If no `relationtype` is passed, it lists all relations. + """ + model = None + existing_relations = list() + + if tocontenttype: + model = tocontenttype.model_class() + + if relationtype: + relation_types = [relationtype] + else: + # special case: when the contenttype is the same as the contenttype of + # the instance, we don't want *all* the relations where the instance + # occurs, but only those where it occurs together with another of its + # type + if instance and ContentType.objects.get_for_model(instance) == tocontenttype: + relation_types = utils.relation_content_types(combination=(model, model)) + else: + relation_types = utils.relation_content_types(any_model=model) + + for rel in relation_types: + if instance: + existing_relations.extend( + list( + rel.model_class().objects.filter(Q(subj=instance) | Q(obj=instance)) + ) + ) + else: + existing_relations.extend(list(rel.model_class().objects.all())) + + cssid = "table" + if model: + cssid += f"_{tocontenttype.name}" + else: + cssid += "_relations" + attrs = { + "class": "table table-hover table-striped table-condensed", + "hx-swap-oob": "true", + "id": cssid, + } + + table = CustomRelationTableView + if model: + table = table_factory(model, CustomRelationTableView) + return table(existing_relations, attrs=attrs)