From 45145c53257df73e989d374eb4d20dc564d8eeb9 Mon Sep 17 00:00:00 2001 From: Gytha Ogg Date: Mon, 22 Jan 2024 07:03:56 +0100 Subject: [PATCH 1/3] Differentiate edit and view tables --- apis_ontology/tables.py | 36 ++++++ .../detail_views/detail_generic.html | 4 +- .../templates/apis_entities/edit_generic.html | 4 +- apis_ontology/templates/relations/list.html | 1 + .../templatetags/custom_relations.py | 116 ++++++++++++++++++ 5 files changed, 159 insertions(+), 2 deletions(-) create mode 100644 apis_ontology/tables.py create mode 100644 apis_ontology/templatetags/custom_relations.py diff --git a/apis_ontology/tables.py b/apis_ontology/tables.py new file mode 100644 index 0000000..e6c571a --- /dev/null +++ b/apis_ontology/tables.py @@ -0,0 +1,36 @@ +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 }}") + object = tables.TemplateColumn("{{ record.obj }}") + description = tables.TemplateColumn("{{ record.name }}") + edit = tables.TemplateColumn( + "Edit" + ) + delete = tables.TemplateColumn(template_name="tables/delete.html") + + class Meta: + model = Relation + fields = ["id", "subject", "description", "object", "edit"] + sequence = tuple(fields) + + +class CustomRelationTableView(RelationTable): + + id = tables.TemplateColumn("{{ record.id }}") + subject = tables.TemplateColumn("{{ record.subj }}") + object = tables.TemplateColumn("{{ record.obj }}") + description = tables.TemplateColumn("{{ record.name }}") + + class Meta: + model = Relation + fields = ["id", "subject", "description", "object"] + 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) From d6ffa31827b0b16c1a82a7cfa80e09c3af818bb8 Mon Sep 17 00:00:00 2001 From: Gytha Ogg Date: Mon, 22 Jan 2024 07:28:22 +0100 Subject: [PATCH 2/3] Added more info to the fields --- apis_ontology/tables.py | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/apis_ontology/tables.py b/apis_ontology/tables.py index e6c571a..8a1ee0a 100644 --- a/apis_ontology/tables.py +++ b/apis_ontology/tables.py @@ -5,9 +5,7 @@ class CustomRelationTableEdit(RelationTable): - id = tables.TemplateColumn( - "{{ record.id }}" - ) + id = tables.TemplateColumn("{{ record.id }}") subject = tables.TemplateColumn("{{ record.subj }}") object = tables.TemplateColumn("{{ record.obj }}") description = tables.TemplateColumn("{{ record.name }}") @@ -15,10 +13,24 @@ class CustomRelationTableEdit(RelationTable): "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", "edit"] + fields = [ + "id", + "subject", + "description", + "object", + "confidence", + "support_notes", + "tei_refs", + "edit", + ] sequence = tuple(fields) @@ -28,9 +40,22 @@ class CustomRelationTableView(RelationTable): subject = tables.TemplateColumn("{{ record.subj }}") object = tables.TemplateColumn("{{ record.obj }}") 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"] + fields = [ + "id", + "subject", + "description", + "object", + "confidence", + "support_notes", + "tei_refs", + ] exclude = ["edit", "delete"] sequence = tuple(fields) From 87a5c39178c432d5aa1d3ba42c52a817f2e64a8a Mon Sep 17 00:00:00 2001 From: Gytha Ogg Date: Mon, 22 Jan 2024 08:04:51 +0100 Subject: [PATCH 3/3] Attempting to link to entity; showing ID --- apis_ontology/tables.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/apis_ontology/tables.py b/apis_ontology/tables.py index 8a1ee0a..4882470 100644 --- a/apis_ontology/tables.py +++ b/apis_ontology/tables.py @@ -6,8 +6,12 @@ class CustomRelationTableEdit(RelationTable): id = tables.TemplateColumn("{{ record.id }}") - subject = tables.TemplateColumn("{{ record.subj }}") - object = tables.TemplateColumn("{{ record.obj }}") + 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" @@ -37,8 +41,12 @@ class Meta: class CustomRelationTableView(RelationTable): id = tables.TemplateColumn("{{ record.id }}") - subject = tables.TemplateColumn("{{ record.subj }}") - object = tables.TemplateColumn("{{ record.obj }}") + 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(