Skip to content

Commit

Permalink
Merge pull request #30 from chnm/stanza-import
Browse files Browse the repository at this point in the history
Django management command for importing the stanzas
  • Loading branch information
hepplerj authored May 30, 2024
2 parents 3170545 + 636526b commit 7212d26
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 20 deletions.
9 changes: 8 additions & 1 deletion manuscript/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ class SingleManuscriptAdmin(ImportExportModelAdmin):
)
search_fields = ("siglum",)
resource_class = SingleManuscriptResource
readonly_fields = ("item_id",)


class FolioAdmin(admin.ModelAdmin):
Expand Down Expand Up @@ -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):
Expand Down
74 changes: 56 additions & 18 deletions manuscript/management/commands/load_stanzas.py
Original file line number Diff line number Diff line change
@@ -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"))
Original file line number Diff line number Diff line change
@@ -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"]},
),
]
5 changes: 5 additions & 0 deletions manuscript/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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."""
Expand Down
5 changes: 4 additions & 1 deletion manuscript/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})
5 changes: 5 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ <h2>Homepage</h2>

{% block content %}

<h3>Text of the manuscript</h3>
{% for stanza in stanzas %}
<p>{{ stanza.stanza_text }}</p>
{% endfor %}

{% endblock content %}

0 comments on commit 7212d26

Please sign in to comment.