diff --git a/apis_ontology/models.py b/apis_ontology/models.py index 0ffff6f..6eba277 100644 --- a/apis_ontology/models.py +++ b/apis_ontology/models.py @@ -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( @@ -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( @@ -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): diff --git a/apis_ontology/tables.py b/apis_ontology/tables.py index 401f494..3d02381 100644 --- a/apis_ontology/tables.py +++ b/apis_ontology/tables.py @@ -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 diff --git a/apis_ontology/templates/generic/partials/object_table.html b/apis_ontology/templates/generic/partials/object_table.html new file mode 100644 index 0000000..8bc8a1a --- /dev/null +++ b/apis_ontology/templates/generic/partials/object_table.html @@ -0,0 +1,23 @@ +{% load apisgeneric %} +{% load filter_utils %} +{% load parse_comment %} + + + {% modeldict object as d %} + {% for key, value in d.items %} + + + + + + + {% endfor %} +
+ {{ key.verbose_name | title}} + + {% if key|endswith:'.comments' %} + {{ value | parse_comment | safe}} + {% else %} + {{ value }} + {% endif %} +
diff --git a/apis_ontology/templatetags/filter_utils.py b/apis_ontology/templatetags/filter_utils.py new file mode 100644 index 0000000..0230cf8 --- /dev/null +++ b/apis_ontology/templatetags/filter_utils.py @@ -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) diff --git a/apis_ontology/templatetags/parse_comment.py b/apis_ontology/templatetags/parse_comment.py new file mode 100644 index 0000000..347c241 --- /dev/null +++ b/apis_ontology/templatetags/parse_comment.py @@ -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 = match.group("text") + zotero_id = match.group("zotero_id") + replacement = f'{text}' + 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'{link_text}' + except Exception as e: + logger.error(f"Error finding cached Zotero entry with ID %s", zotero_id) + logger.error(repr(e)) + return f'{zotero_id}' + 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'{root_obj}' + + except Exception as e: + logger.error("Error finding entity #%s", entity_id) + logger.error(repr(e)) + return f'{entity_id}' + 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.*?) \[(?P[A-Z0-9]+)\]>>|\[(?P[A-Z0-9]+)\]|\(ID:\s*(?P\d+)\)" + + # Apply substitutions using the combined pattern and custom replacement function + transformed_value = re.sub(combined_pattern, custom_replace, value) + + return transformed_value