Skip to content

Commit

Permalink
Merge pull request #9 from acdh-oeaw/custom-relations-table
Browse files Browse the repository at this point in the history
Custom relations table
  • Loading branch information
gythaogg committed Jan 22, 2024
2 parents c1e6c82 + 87a5c39 commit 0f98f06
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 2 deletions.
69 changes: 69 additions & 0 deletions apis_ontology/tables.py
Original file line number Diff line number Diff line change
@@ -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(
"<a href='/entities/entity/{{ record.subj_model }}/{{ record.subj.pk }}'>{{record.subj}} ({{record.subj.pk}})</a>"
)
object = tables.TemplateColumn(
"<a href='/entities/entity/{{ record.obj_model }}/{{ record.obj.pk }}'>{{record.obj}} ({{record.obj.pk}})</a>"
)
description = tables.TemplateColumn("{{ record.name }}")
edit = tables.TemplateColumn(
"<a href='{% url 'apis:relationupdate' record.id %}'>Edit</a>"
)
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("<a href='#{{record.tei_refs}}'>TEI</a>")

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(
"<a href='/entities/entity/{{ record.subj_model }}/{{ record.subj.pk }}'>{{record.subj}} ({{record.subj.pk}})</a>"
)
object = tables.TemplateColumn(
"<a href='/entities/entity/{{ record.obj_model }}/{{ record.obj.pk }}'>{{record.obj}} ({{record.obj.pk}})</a>"
)
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("<a href='#{{record.tei_refs}}'>TEI</a>")

class Meta:
model = Relation
fields = [
"id",
"subject",
"description",
"object",
"confidence",
"support_notes",
"tei_refs",
]
exclude = ["edit", "delete"]
sequence = tuple(fields)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

{% load relations %}

{% load custom_relations %}

{% relations_css %}
{% block title %}{{ object }}{% endblock %}

Expand Down Expand Up @@ -318,7 +320,7 @@ <h4 class="card-title">
</div>
<div role="tabcard">
<div class="card-body">
{% relations_table instance=object tocontenttype=contenttype as table %}
{% custom_relations_table_view instance=object tocontenttype=contenttype as table %}
{% render_table table %}
</div>
</div>
Expand Down
4 changes: 3 additions & 1 deletion apis_ontology/templates/apis_entities/edit_generic.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
{{ block.super }}
<script src="https://unpkg.com/htmx.org@latest/dist/htmx.js"></script>
{% load relations %}
{% load custom_relations %}

{% relations_css %}
{% if highlighter_active %}
<link rel="stylesheet"
Expand Down Expand Up @@ -209,7 +211,7 @@ <h4 class="card-title">
</div>
<div id="collapse{{ forloop.counter }}" class="card-collapse collapse" role="tabcard" aria-labelledby="heading{{ forloop.counter }}">
<div id="tab_{{ obj.2 }}" class="card-body">
{% 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 %}
Expand Down
1 change: 1 addition & 0 deletions apis_ontology/templates/relations/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ <h1 class="text-center">{{ model.name }}{{ object }}</h1>
{% endif %}

{% if form %}

{{ form.errors }}
{{ form.non_field_errors }}
{% load crispy_forms_tags %}
Expand Down
116 changes: 116 additions & 0 deletions apis_ontology/templatetags/custom_relations.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 0f98f06

Please sign in to comment.