Skip to content

Commit

Permalink
Merge pull request #58 from chnm/feature/annotation-translations
Browse files Browse the repository at this point in the history
refactor: Include annotation tools for translated text
  • Loading branch information
hepplerj authored Nov 21, 2024
2 parents c7f8002 + 91db249 commit 7c68816
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 26 deletions.
5 changes: 4 additions & 1 deletion manuscript/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,12 @@ class StanzaTranslatedAdmin(admin.ModelAdmin):
list_display = ("stanza", "stanza_text", "language")
search_fields = ("stanza", "stanza_text")
list_filter = ("language",)
inlines = [TextAnnotationInline]

class Media:
css = {"all": ("css/text_annotations.css",)}
js = ("js/text_annotations.js",)

# Register to the admin interface.

admin.site.register(Library, LibraryAdmin)
admin.site.register(EditorialStatus, EditorialStatusAdmin)
Expand Down
35 changes: 16 additions & 19 deletions manuscript/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,28 @@
@ensure_csrf_cookie
def create_annotation(request):
try:
# Validate the request
if not request.headers.get("X-Requested-With") == "XMLHttpRequest":
raise ValueError("AJAX required")

# Get required fields
stanza_id = request.POST.get("stanza_id")
object_id = request.POST.get("stanza_id")
selected_text = request.POST.get("selected_text")
annotation_text = request.POST.get("annotation")
annotation_type = request.POST.get("annotation_type")
model_type = request.POST.get("model_type", "stanza")

# Validate all required fields are present
if not all([stanza_id, selected_text, annotation_text, annotation_type]):
# Validate required fields
if not all([object_id, selected_text, annotation_text, annotation_type]):
missing_fields = [
field
for field, value in {
"stanza_id": stanza_id,
"stanza_id": object_id,
"selected_text": selected_text,
"annotation": annotation_text,
"annotation_type": annotation_type,
}.items()
if not value
]

logger.error(f"Missing required fields: {missing_fields}")
return JsonResponse(
{
Expand All @@ -66,25 +65,23 @@ def create_annotation(request):
status=400,
)

# Get the stanza
try:
stanza = Stanza.objects.get(id=stanza_id)
except Stanza.DoesNotExist:
logger.error(f"Stanza not found: {stanza_id}")
return JsonResponse(
{"success": False, "error": f"Stanza not found: {stanza_id}"},
status=404,
)
# Get the appropriate model and object
if model_type == "stanzatranslated":
content_type = ContentType.objects.get_for_model(StanzaTranslated)
annotated_object = get_object_or_404(StanzaTranslated, id=object_id)
else:
content_type = ContentType.objects.get_for_model(Stanza)
annotated_object = get_object_or_404(Stanza, id=object_id)

# Create the annotation
annotation = TextAnnotation.objects.create(
content_type=ContentType.objects.get_for_model(Stanza),
object_id=stanza.id,
content_type=content_type,
object_id=object_id,
selected_text=selected_text,
annotation=annotation_text,
annotation_type=annotation_type,
from_pos=json.loads(request.POST.get("from_pos", "0")),
to_pos=json.loads(request.POST.get("to_pos", "0")),
from_pos=request.POST.get("from_pos"),
to_pos=request.POST.get("to_pos"),
)

return JsonResponse(
Expand Down
16 changes: 12 additions & 4 deletions static/js/text_annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,14 @@ document.addEventListener("DOMContentLoaded", function () {
annotationGroup.appendChild(annotateButton);
toolbar.appendChild(annotationGroup);

// Function to get stanza ID (keeping existing implementation)
// Function to get stanza ID
function getStanzaId() {
const urlMatch = window.location.pathname.match(/\/stanza\/(\d+)\//);
if (urlMatch) return urlMatch[1];
const urlMatch = window.location.pathname.match(
/\/(stanza|stanzatranslated)\/(\d+)/,
);
if (urlMatch) return urlMatch[2];

const form = document.querySelector("#stanza_form");
const form = document.querySelector("#stanza_form, #stanzatranslated_form");
if (form) {
const objectId =
form.getAttribute("data-object-id") ||
Expand Down Expand Up @@ -273,6 +275,12 @@ document.addEventListener("DOMContentLoaded", function () {

// Prepare the data
const formData = new FormData();
formData.append(
"model_type",
window.location.pathname.includes("stanzatranslated")
? "stanzatranslated"
: "stanza",
);
formData.append("stanza_id", stanzaId);
formData.append("selected_text", selectedText);
formData.append("annotation", annotationText);
Expand Down
4 changes: 3 additions & 1 deletion templates/stanzas.html
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ <h2 id="book-{{book_number}}" class="text-xl font-bold mb-4">Book {{ book_number
</span>
</a>
</span>
<span class="flex-1 pl-4">{{ stanza.unescaped_stanza_text|safe }}</span>
<span class="flex-1 pl-4">
{{ stanza.unescaped_stanza_text|annotate_text:stanza.annotations.all|safe }}
</span>
</div>
</div>
{% endfor %}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 5.0.2 on 2024-11-21 16:06

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("textannotation", "0002_alter_textannotation_options_and_more"),
]

operations = [
migrations.AlterField(
model_name="textannotation",
name="annotation_type",
field=models.CharField(
choices=[
("note", "Editorial Note"),
("variant", "Textual Variant"),
("reference", "Cross Reference"),
],
default="note",
max_length=20,
),
),
]
1 change: 0 additions & 1 deletion textannotation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ class TextAnnotation(models.Model):

ANNOTATION_TYPES = (
("note", "Editorial Note"),
("translation", "Translation"),
("variant", "Textual Variant"),
("reference", "Cross Reference"),
)
Expand Down

0 comments on commit 7c68816

Please sign in to comment.