Skip to content

Commit

Permalink
Merge pull request #12 from acdh-oeaw/link-comments
Browse files Browse the repository at this point in the history
Link comments
  • Loading branch information
gythaogg authored Apr 23, 2024
2 parents 4925d65 + dfa5cdc commit 90d772e
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 4 deletions.
6 changes: 3 additions & 3 deletions apis_ontology/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class Meta:
verbose_name_plural = _("Persons")

def __str__(self):
return f"{self.name}"
return f"{self.name} ({self.pk})"


class Place(
Expand Down Expand Up @@ -138,7 +138,7 @@ class Meta:
verbose_name_plural = _("Works")

def __str__(self):
return f"{self.name}"
return f"{self.name} ({self.pk})"


class Instance(
Expand Down Expand Up @@ -228,7 +228,7 @@ class Meta:
verbose_name_plural = _("Instances")

def __str__(self):
return f"{self.name}"
return f"{self.name} ({self.pk})"


class ZoteroEntry(GenericModel, models.Model):
Expand Down
2 changes: 1 addition & 1 deletion apis_ontology/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from apis_core.apis_entities.tables import AbstractEntityTable
from django_tables2.utils import A

from .templatetags.linkify_list import render_links, render_list_field
from .templatetags.linkify_list import render_links
from .models import Instance, Person, Place, Work


Expand Down
23 changes: 23 additions & 0 deletions apis_ontology/templates/generic/partials/object_table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{% load apisgeneric %}
{% load filter_utils %}
{% load parse_comment %}

<table class="table table-hover">
{% modeldict object as d %}
{% for key, value in d.items %}
<tr>

<th>
{{ key.verbose_name | title}}
</th>

<td>
{% if key|endswith:'.comments' %}
{{ value | parse_comment | safe}}
{% else %}
{{ value }}
{% endif %}
</td>
</tr>
{% endfor %}
</table>
9 changes: 9 additions & 0 deletions apis_ontology/templatetags/filter_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django import template

register = template.Library()


@register.filter
def endswith(value, suffix):
"""Custom filter to check if value ends with the specified suffix."""
return str(value).endswith(suffix)
60 changes: 60 additions & 0 deletions apis_ontology/templatetags/parse_comment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import re
import logging

logger = logging.getLogger(__name__)

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()


@register.filter
def parse_comment(value):
def custom_replace(match):
if match.group("text"):
# Handle <<text [ZoteroID]>>
text = match.group("text")
zotero_id = match.group("zotero_id")
replacement = f'<a target="_BLANK" href="https://www.zotero.org/groups/4394244/tibschol/items/{zotero_id}/item-details#">{text}</a>'
elif match.group("zotero_id_only"):
# Handle [ZoteroID]
zotero_id = match.group("zotero_id_only")
try:
zotero_obj = ZoteroEntry.objects.filter(zoteroId=zotero_id)[0]
link_text = (
zotero_obj.shortTitle if zotero_obj.shortTitle else zotero_id
)
return f'<a target="_BLANK" href="https://www.zotero.org/groups/4394244/tibschol/items/{zotero_id}/item-details#">{link_text}</a>'
except Exception as e:
logger.error(f"Error finding cached Zotero entry with ID %s", zotero_id)
logger.error(repr(e))
return f'<a target="_BLANK" href="https://www.zotero.org/groups/4394244/tibschol/items/{zotero_id}/item-details#">{zotero_id}</a>'
elif match.group("entity_id"):
# Handle (ID: number)
entity_id = match.group("entity_id")
try:
root_obj = RootObject.objects_inheritance.get_subclass(pk=entity_id)
ct = ContentType.objects.get_for_model(root_obj)
return f'<a target="_BLANK" href="/apis/apis_ontology.{ct.name}/{root_obj.pk}">{root_obj}</a>'

except Exception as e:
logger.error("Error finding entity #%s", entity_id)
logger.error(repr(e))
return f'<a target="_BLANK" href="/entity/{entity_id}">{entity_id}</a>'
else:
# If no specific group is matched, return the original match
replacement = match.group(0)

return replacement

# Define the regex pattern to capture different groups
combined_pattern = r"<<(?P<text>.*?) \[(?P<zotero_id>[A-Z0-9]+)\]>>|\[(?P<zotero_id_only>[A-Z0-9]+)\]|\(ID:\s*(?P<entity_id>\d+)\)"

# Apply substitutions using the combined pattern and custom replacement function
transformed_value = re.sub(combined_pattern, custom_replace, value)

return transformed_value

0 comments on commit 90d772e

Please sign in to comment.