Skip to content

Commit

Permalink
feat: store the name of the field in the annotation
Browse files Browse the repository at this point in the history
... and use this information to make the highlighter more flexible- it
is now possible to use the highlighter for multiple fields in one model
instance.

Closes: #43
  • Loading branch information
b1rger committed Apr 11, 2024
1 parent 79c820c commit e801219
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
21 changes: 21 additions & 0 deletions apis_highlighter/migrations/0003_annotation_text_field_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 4.2.10 on 2024-03-21 13:58

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
(
"apis_highlighter",
"0002_rename_annotation_project_annotation_project_and_more",
),
]

operations = [
migrations.AddField(
model_name="annotation",
name="text_field_name",
field=models.CharField(default="text"),
),
]
1 change: 1 addition & 0 deletions apis_highlighter/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Annotation(models.Model):
)
text_object_id = models.PositiveIntegerField(null=True)
text_content_object = GenericForeignKey("text_content_type", "text_object_id")
text_field_name = models.CharField(default="text")

user = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)

Expand Down
1 change: 1 addition & 0 deletions apis_highlighter/static/js/highlighter.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ highlighttexts.forEach(text => {
var annotationdata = {};
annotationdata.text_object_id = highlight_text.dataset.text_object_id;
annotationdata.text_content_type_id = highlight_text.dataset.text_content_type_id;
annotationdata.text_field_name = highlight_text.dataset.text_field_name;
annotationdata.project_id = highlight_text.dataset.project_id;
annotationdata.orig_string = selection.toString();
annotationdata.start = start;
Expand Down
13 changes: 8 additions & 5 deletions apis_highlighter/templatetags/apis_highlighter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ def overlap(range_one, range_two) -> bool:
return bool(r)


@register.filter()
def highlight_text(obj, request=None, fieldname="text", project_id=None):
@register.simple_tag
def highlight_text(obj, request=None, field_name="text", project_id=None):
if project_id is None:
default_project = getattr(
settings,
Expand All @@ -24,8 +24,10 @@ def highlight_text(obj, request=None, fieldname="text", project_id=None):
project_id = request.GET.get("highlighter_project", default_project)

ct = ContentType.objects.get_for_model(obj)
annotations = Annotation.objects.filter(text_content_type=ct, text_object_id=obj.id)
args = [ct.id, obj.id]
annotations = Annotation.objects.filter(
text_content_type=ct, text_object_id=obj.id, text_field_name=field_name
)
args = [ct.id, obj.id, field_name]
if project_id:
annotations = annotations.filter(project__id=project_id)
args.append(project_id)
Expand All @@ -35,12 +37,13 @@ def highlight_text(obj, request=None, fieldname="text", project_id=None):
f"<div class='highlight-text'"
f" data-text_object_id='{obj.id}'"
f" data-text_content_type_id='{ct.id}'"
f" data-text_field_name='{field_name}'"
f" data-project_id='{project_id}'"
f" data-source='{annotations_url}'>"
)
suffix = "</div>"

text = getattr(obj, fieldname)
text = getattr(obj, field_name)
annotated_ranges = []
for ann in annotations.order_by("-start"):
if not any(map(lambda x: overlap((ann.start, ann.end), x), annotated_ranges)):
Expand Down
4 changes: 2 additions & 2 deletions apis_highlighter/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

urlpatterns = [
path(
"annotations/<text_content_type>/<text_object_id>",
"annotations/<text_content_type>/<text_object_id>/<text_field_name>",
views.AnnotationsView.as_view(),
name="annotations",
),
path(
"annotations/<text_content_type>/<text_object_id>/<project_id>",
"annotations/<text_content_type>/<text_object_id>/<text_field_name>/<project_id>",
views.AnnotationsView.as_view(),
name="annotations",
),
Expand Down
7 changes: 6 additions & 1 deletion apis_highlighter/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ def dispatch(self, request, *args, **kwargs):
# return 404 if not exist
self.object = ct.get_object_for_this_type(pk=text_object_id)
self.project_id = kwargs.get("project_id")
self.field_name = kwargs.get("text_field_name")

return super().dispatch(request, *args, **kwargs)

def get(self, request, *args, **kwargs):
return HttpResponse(highlight_text(self.object, project_id=self.project_id))
return HttpResponse(
highlight_text(
self.object, field_name=self.field_name, project_id=self.project_id
)
)


class AnnotationDelete(DeleteView):
Expand Down

0 comments on commit e801219

Please sign in to comment.