Skip to content

Commit

Permalink
Merge pull request #15 from acdh-oeaw/relations
Browse files Browse the repository at this point in the history
Relations
  • Loading branch information
gythaogg authored Apr 29, 2024
2 parents e3a669d + 1acc935 commit f6cb614
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 138 deletions.
83 changes: 78 additions & 5 deletions apis_ontology/tables.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import django_tables2 as tables
from apis_core.apis_entities.tables import AbstractEntityTable
from apis_core.generic.tables import GenericTable
from django_tables2.utils import A

from .models import Instance, Person, Place, TibScholRelationMixin, Work
from .templatetags.filter_utils import render_links
from .models import Instance, Person, Place, Work
from .templatetags.parse_comment import parse_comment

import django_tables2 as tables
import logging
from apis_core.apis_metainfo.models import RootObject

from django.utils.safestring import mark_safe


logger = logging.getLogger(__name__)


class PlaceTable(AbstractEntityTable):
Expand All @@ -13,7 +24,7 @@ class Meta:
exclude = ["desc"]

id = tables.Column(
linkify=lambda record: record.get_edit_url(),
linkify=lambda record: record.get_absolute_url(),
empty_values=[],
)

Expand All @@ -34,7 +45,7 @@ class Meta:
exclude = ["desc"]

id = tables.Column(
linkify=lambda record: record.get_edit_url(),
linkify=lambda record: record.get_absolute_url(),
empty_values=[],
)

Expand All @@ -49,7 +60,7 @@ class Meta:
exclude = ["desc"]

id = tables.Column(
linkify=lambda record: record.get_edit_url(),
linkify=lambda record: record.get_absolute_url(),
empty_values=[],
)
author = tables.Column(verbose_name="Author", accessor="author", orderable=False)
Expand All @@ -62,10 +73,72 @@ class Meta:
exclude = ["desc"]

id = tables.Column(
linkify=lambda record: record.get_edit_url(),
linkify=lambda record: record.get_absolute_url(),
empty_values=[],
)
author = tables.Column(verbose_name="Author", accessor="author", orderable=False)

def render_external_links(self, value):
return render_links(value)


class RelationsTable(GenericTable):
reverse = False

class Meta(GenericTable.Meta):
model = TibScholRelationMixin
fields = [
"id",
"name",
"obj",
"support_notes",
"zotero_refs",
"TEI",
]
exclude = ["view", "edit", "desc", "delete", "subj"]

name = tables.Column(verbose_name="Relationship")
obj = tables.Column(verbose_name="Object")

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Define the obj attribute based on the value of self.reverse

def render_name(self, record):
if self.context["object"].pk == record.subj.pk:
return record.name
elif self.context["object"].pk == record.obj.pk:
return record.reverse_name
else:
return ""

def render_support_notes(self, value):
return mark_safe(parse_comment(value))

def render_zotero_refs(self, value):
return mark_safe(parse_comment(value))

def render_obj(self, record):
# return str(record) + str(self.context["object"].pk)
if self.context["object"].pk == record.obj.pk:
# return str(RootObject.objects_inheritance.get_subclass(pk=record.subj.pk))
actual_obj = RootObject.objects_inheritance.get_subclass(pk=record.subj.pk)

else:
actual_obj = RootObject.objects_inheritance.get_subclass(pk=record.obj.pk)

return mark_safe(
"<a href='"
+ actual_obj.get_absolute_url()
+ "' target='_BLANK'>"
+ str(actual_obj)
+ "</a>"
)


class RelationsTableEdit(RelationsTable):
pass


class RelationsTableView(RelationsTable):
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{% extends "apis_core/apis_entities/abstractentity.html" %}
{% load django_tables2 %}
{% load apis_templatetags %}
{% load apiscore %}
{% load relationsng %}

{% block col-zero %}
<div class="card">
<div class="card-body">{% include "generic/partials/object_table.html" %}</div>
</div>
{% endblock col-zero %}

{% block col-one %}
<h4>Relations</h4>
{% related_entity_types as related_entity_types %}
<div class="container">
<!-- Nav tabs -->
<ul class="nav nav-tabs">
{% for ent_type in related_entity_types %}

<li class="nav-item">
<a class="nav-link {% if forloop.first %}active{% endif %}" data-toggle="tab"
href="#reltype{{forloop.counter0}}">{{ent_type |model_meta:"verbose_name" | title}}</a>
</li>
{% endfor %}
</ul>

<!-- Tab panes -->
<div class="tab-content">
{% for ent_type in related_entity_types %}
<div class="tab-pane container {% if forloop.first %}active{% endif %}" id="reltype{{forloop.counter0}}">
{% entity_relations with_type=ent_type as entity_relations %}
{% render_table entity_relations %}
</div>
{% endfor %}

</div>

</div>

{% endblock col-one %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% load django_tables2 %}
{% load apis_templatetags %}
{% load apiscore %}
{% load relationsng %}

<h4>Relations</h4>
{% related_entity_types as related_entity_types %}
<div class="container">
<!-- Nav tabs -->
<ul class="nav nav-tabs">
{% for ent_type in related_entity_types %}

<li class="nav-item">
<a class="nav-link {% if forloop.first %}active{% endif %}" data-toggle="tab"
href="#reltype{{forloop.counter0}}">{{ent_type |model_meta:"verbose_name" | title}}</a>
</li>
{% endfor %}
</ul>

<!-- Tab panes -->
<div class="tab-content">
{% for ent_type in related_entity_types %}
<div class="tab-pane container {% if forloop.first %}active{% endif %}" id="reltype{{forloop.counter0}}">
{% entity_relations with_type=ent_type as entity_relations %}
{% render_table entity_relations %}
</div>
{% endfor %}

</div>

</div>
9 changes: 4 additions & 5 deletions apis_ontology/templatetags/parse_comment.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import re
import logging
import re

logger = logging.getLogger(__name__)

from apis_core.apis_metainfo.models import RootObject
from apis_ontology.models import ZoteroEntry
from django import template
from django.contrib.contenttypes.models import ContentType

from apis_ontology.models import ZoteroEntry
from apis_core.apis_metainfo.models import RootObject

register = template.Library()
logger = logging.getLogger(__name__)


@register.filter
Expand Down
65 changes: 65 additions & 0 deletions apis_ontology/templatetags/relationsng.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from django import template
import logging
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q
from apis_core.relations.models import Relation
from apis_ontology.models import TibScholRelationMixin
from itertools import chain

from apis_ontology.tables import RelationsTableEdit
from django.db.models.query import QuerySet
from django_tables2 import RequestConfig
from django_tables2.tables import table_factory

logger = logging.getLogger(__name__)

register = template.Library()


@register.simple_tag(takes_context=True)
def related_entity_types(context):
entity = context["object"]
content_type = ContentType.objects.get_for_model(entity)
models = TibScholRelationMixin.__subclasses__()
relavent_models = set()
for m in models:
if ContentType.objects.get_for_model(m.subj_model) == content_type:
relavent_models.add(ContentType.objects.get_for_model(m.obj_model))

if ContentType.objects.get_for_model(m.obj_model) == content_type:
relavent_models.add(ContentType.objects.get_for_model(m.subj_model))

logger.debug("RELAVENT MODELS: %s", relavent_models)
return relavent_models


@register.simple_tag(takes_context=True)
def entity_relations(context, with_type):
entity = context["object"]
content_type = ContentType.objects.get_for_model(entity)
models = TibScholRelationMixin.__subclasses__()
forward_relations = []
reverse_relations = []
for m in models:
if (
ContentType.objects.get_for_model(m.subj_model) == content_type
and ContentType.objects.get_for_model(m.obj_model) == with_type
):
forward_relations.extend(m.objects.filter(subj=entity))
logger.debug(m.objects.filter(subj=entity))

if (
ContentType.objects.get_for_model(m.obj_model) == content_type
and ContentType.objects.get_for_model(m.subj_model) == with_type
):
reverse_relations.extend(m.objects.filter(obj=entity))
logger.debug(m.objects.filter(subj=entity))

table = RelationsTableEdit

return table(forward_relations + reverse_relations)


# + table(
# reverse_relations, reverse=True
# )
Loading

0 comments on commit f6cb614

Please sign in to comment.