diff --git a/manuscript/admin.py b/manuscript/admin.py index 5fb1ac8..478b821 100644 --- a/manuscript/admin.py +++ b/manuscript/admin.py @@ -139,7 +139,6 @@ class SingleManuscriptAdmin(ImportExportModelAdmin): ) search_fields = ("siglum",) resource_class = SingleManuscriptResource - readonly_fields = ("item_id",) class FolioAdmin(admin.ModelAdmin): @@ -204,6 +203,14 @@ def save_related(self, request, form, formsets, change): class StanzaAdmin(admin.ModelAdmin): inlines = [StanzaVariantInline] + list_display = ( + "stanza_line_code_starts", + "stanza_text", + ) + search_fields = ( + "stanza_text", + "stanza_line_code_starts", + ) class StanzaVariantAdmin(admin.ModelAdmin): diff --git a/manuscript/management/commands/load_stanzas.py b/manuscript/management/commands/load_stanzas.py index 4de7177..5cdc637 100644 --- a/manuscript/management/commands/load_stanzas.py +++ b/manuscript/management/commands/load_stanzas.py @@ -1,28 +1,66 @@ +import logging +import re + from django.core.management.base import BaseCommand -from manuscript.models import Stanza +from manuscript.models import SingleManuscript, Stanza + +logger = logging.getLogger(__name__) class Command(BaseCommand): - help = "Insert stanzas from a text file" + help = "Import poem data from a plain text file" def add_arguments(self, parser): parser.add_argument( - "file_path", type=str, help="The path to the file that contains the stanzas" + "--filepath", type=str, help="filepath of the plain text file to load" + ) + + def handle(self, *args, **options): + file_path = options.get("filepath") + + with open(file_path, "r", encoding="utf-8-sig") as file: + content = file.read() + + self.import_poem(content) + + def import_poem(self, content): + manuscript = SingleManuscript.objects.get(siglum="TEST") + book_pattern = re.compile(r"^\s*LIBRO (\d+)", re.MULTILINE) + stanza_pattern = re.compile( + r"^(\d+)\.\s*(.*?)(?=\n\d+\.|\Z)", re.DOTALL | re.MULTILINE ) - def handle(self, *args, **kwargs): - file_path = kwargs["file_path"] - with open(file_path, "r", encoding="utf-8") as file: - line_counter = 0 - for line in file: - line = line.strip() - if line.startswith("[RUBRIC]") or line.isdigit(): - continue - if line != "": - line_counter += 1 - line_number = f"01.02.{line_counter:02d}" - stanza = Stanza(line=line, line_number=line_number) - stanza.save() - else: - line_counter = 0 + book_matches = book_pattern.split(content) + if book_matches[0] == "": + book_matches = book_matches[1:] + + for i in range(0, len(book_matches), 2): + book_number = int( + book_matches[i] + ) # Extract book number from "LIBRO X" string + book_text = book_matches[ + i + 1 + ].strip() # Get the corresponding book content + + stanza_matches = stanza_pattern.findall(book_text) + + for stanza_number, stanza_text in stanza_matches: + stanza_number = int(stanza_number) + stanza_text = stanza_text.strip() + + stanza_lines = stanza_text.split("\n") + + for line_number, line_text in enumerate(stanza_lines, start=1): + line_code = ( + f"{book_number:02d}.{stanza_number:02d}.{line_number:02d}" + ) + # print(f"Line code: {line_code}, Line text: {line_text}") + + Stanza.objects.create( + stanza_line_code_starts=line_code, + stanza_text=line_text, + related_manuscript=manuscript, + ) + + self.stdout.write(self.style.SUCCESS("Successfully imported the stanzas")) diff --git a/manuscript/migrations/0071_alter_folio_options_alter_stanza_options.py b/manuscript/migrations/0071_alter_folio_options_alter_stanza_options.py new file mode 100644 index 0000000..c07d899 --- /dev/null +++ b/manuscript/migrations/0071_alter_folio_options_alter_stanza_options.py @@ -0,0 +1,20 @@ +# Generated by Django 5.0.2 on 2024-05-30 16:43 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ("manuscript", "0070_alter_location_line_code_alter_location_placename_id"), + ] + + operations = [ + migrations.AlterModelOptions( + name="folio", + options={"ordering": ["folio_number"]}, + ), + migrations.AlterModelOptions( + name="stanza", + options={"ordering": ["id"]}, + ), + ] diff --git a/manuscript/models.py b/manuscript/models.py index 6c14921..717f08a 100644 --- a/manuscript/models.py +++ b/manuscript/models.py @@ -5,6 +5,8 @@ from django.conf import settings from django.core.exceptions import ValidationError from django.db import models +from django.db.models import IntegerField +from django.db.models.functions import Cast from django_prose_editor.fields import ProseEditorField from prose.fields import RichTextField @@ -347,6 +349,9 @@ def derive_folio_location(self): manuscript=self.related_folio.manuscript, folio_number=book ).first() + class Meta: + ordering = ["id"] + class Folio(models.Model): """This provides a way to collect several stanzas onto a single page, and associate them with a single manuscript.""" diff --git a/manuscript/views.py b/manuscript/views.py index d43199d..27516a9 100644 --- a/manuscript/views.py +++ b/manuscript/views.py @@ -3,6 +3,9 @@ from django.urls import reverse from django.views import generic +from manuscript.models import Stanza + def index(request: HttpRequest): - return render(request, "index.html", {}) + stanzas = Stanza.objects.all().order_by("stanza_line_code_starts") + return render(request, "index.html", {"stanzas": stanzas}) diff --git a/templates/index.html b/templates/index.html index 65d47a9..634fd25 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,4 +4,9 @@
{{ stanza.stanza_text }}
+ {% endfor %} + {% endblock content %} \ No newline at end of file