This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from acdh-oeaw/custom-relations-table
Custom relations table
- Loading branch information
Showing
5 changed files
with
192 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |