Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: show relations of nested places in Place detail page #190

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions apis_ontology/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ class RelationNameColumn(CustomTemplateColumn):
orderable = False


class RelationSubjectColumn(CustomTemplateColumn):
template_name = "columns/relation_subject.html"
verbose_name = "Subject"
orderable = False


class RelationPredicateColumn(CustomTemplateColumn):
template_name = "columns/relation_predicate.html"
verbose_name = "Object"
Expand Down Expand Up @@ -224,6 +230,22 @@ class Meta(GenericTable.Meta):
per_page = 1000


class PlaceRelationsTable(TibScholEntityMixinRelationsTable):
subject = RelationSubjectColumn()

class Meta(TibScholEntityMixinRelationsTable.Meta):
pass

field_order = [
"subject",
"relation",
"predicate",
"support_notes",
"zotero_refs",
"tei_refs",
]


class RelationsTable(TibScholEntityMixinRelationsTable):
reverse = False

Expand Down
2 changes: 1 addition & 1 deletion apis_ontology/templates/apis_ontology/place_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
</div>
{% endblock %}
{% block relations-include %}
{% include "relations/list_relations_include_tabs.html" %}
{% include "relations/place_list_relations_include_tabs.html" %}
{% endblock relations-include %}
2 changes: 1 addition & 1 deletion apis_ontology/templates/apis_ontology/place_form.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "apis_core/apis_entities/abstractentity_form.html" %}

{% block relations-include %}
{% include "relations/list_relations_include_tabs.html" with edit=True %}
{% include "relations/place_list_relations_include_tabs.html" with edit=True %}
{% endblock relations-include %}
9 changes: 9 additions & 0 deletions apis_ontology/templates/columns/relation_subject.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% if record.forward %}
<a href="{{ record.subj.get_absolute_url }}" style="{{ highlight_style }}">
{{ record.subj }}
</a>
{% else %}
<a href="{{ record.obj.get_absolute_url }}" style="{{ highlight_style }}">
{{ record.obj }}
</a>
{% endif %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{% extends "relations/list_relations_include_tabs.html" %}

{% load nested_relations %}


{% block display-relations %}
{% relations_from_place from_obj=object include_places_within=True as relations %}
{{ block.super }}
{% endblock display-relations %}
48 changes: 48 additions & 0 deletions apis_ontology/templatetags/nested_relations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import logging
from django import template
from django.utils.safestring import mark_safe
from django.contrib.contenttypes.models import ContentType
from apis_ontology.models import Place, PlaceIsLocatedWithinPlace
from apis_core.relations.templatetags.relations import relations_from

register = template.Library()

logger = logging.getLogger(__name__)


@register.simple_tag
def relations_from_place(
from_obj, relation_type: ContentType = None, include_places_within=False
):

def locations_contained_in(place_id):
try:
nested_places = [
rel.subj_object_id
for rel in set(
(PlaceIsLocatedWithinPlace.objects.filter(obj_object_id=place_id))
)
]
for p in nested_places:
nested_places.extend(locations_contained_in(p))
except Place.DoesNotExist:
pass
return nested_places

rels = relations_from(from_obj, relation_type)
if not include_places_within:
return rels

nested_locs = locations_contained_in(from_obj.pk)
if not nested_locs:
return rels

nested_rels = []
for nested_loc in nested_locs:
try:
nested_rels.extend(
relations_from(Place.objects.get(pk=nested_loc), relation_type)
)
except Place.DoesNotExist:
pass
return [*rels, *nested_rels]