Skip to content

Commit

Permalink
feat: show relations of nested places in Place detail page
Browse files Browse the repository at this point in the history
closes #49
  • Loading branch information
gythaogg committed Nov 18, 2024
1 parent 2f3645b commit 0b6eadf
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 2 deletions.
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
@@ -1,5 +1,5 @@
{% extends "apis_core/apis_entities/e53_place_detail.html" %}

{% 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 %}
44 changes: 44 additions & 0 deletions apis_ontology/templatetags/nested_relations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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):
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))

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:
nested_rels.extend(
relations_from(Place.objects.get(pk=nested_loc), relation_type)
)

return [*rels, *nested_rels]

0 comments on commit 0b6eadf

Please sign in to comment.