Skip to content

Commit

Permalink
feat: Derive FK to related stanzas from the stanza variant line code
Browse files Browse the repository at this point in the history
This commit modifies the `StanzaVariant` model's `save` method to trim the letter code from the `stanza_variation_line_code_starts` field. This allows for easier lookup of the foreign key to the `Stanza` model. Additionally, the commit updates the `__str__` method of the `StanzaVariant` model to display the association between the stanza variation and the stanza it belongs to.
  • Loading branch information
hepplerj committed Jun 5, 2024
1 parent 343e402 commit 683191a
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 19 deletions.
26 changes: 26 additions & 0 deletions manuscript/migrations/0073_alter_stanzavariant_stanza.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 5.0.2 on 2024-06-05 16:23

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
(
"manuscript",
"0072_alter_detail_distance_lines_alter_detail_is_sea_red_and_more",
),
]

operations = [
migrations.AlterField(
model_name="stanzavariant",
name="stanza",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="manuscript.stanza",
),
),
]
24 changes: 24 additions & 0 deletions manuscript/migrations/0074_alter_stanzavariant_stanza.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.0.2 on 2024-06-05 16:32

import django.db.models.deletion
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("manuscript", "0073_alter_stanzavariant_stanza"),
]

operations = [
migrations.AlterField(
model_name="stanzavariant",
name="stanza",
field=models.ForeignKey(
blank=True,
editable=False,
null=True,
on_delete=django.db.models.deletion.CASCADE,
to="manuscript.stanza",
),
),
]
52 changes: 33 additions & 19 deletions manuscript/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@

from bs4 import BeautifulSoup
from django.conf import settings
from django.core.exceptions import ValidationError
from django.core.exceptions import ObjectDoesNotExist, 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

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -228,15 +225,6 @@ def __str__(self) -> str:
class StanzaVariant(models.Model):
"""Notes about variants in a stanza"""

LINE_VARIANTS = (
("lf", "Lines Flipped"),
("dw", "Different Words"),
("do", "Different Order"),
("dp", "Different Punctuation"),
("ds", "Different Spelling"),
("dc", "Different Capitalization"),
)

# TODO: Ability to have variation in lines
id = models.AutoField(primary_key=True)
stanza_variation = models.TextField(
Expand All @@ -256,17 +244,43 @@ class StanzaVariant(models.Model):
)

stanza = models.ForeignKey(
"Stanza", on_delete=models.PROTECT, blank=True, null=True
"Stanza",
on_delete=models.CASCADE,
blank=True,
null=True,
editable=False,
)

def provide_snippet_of_stanza(self):
return self.stanza.stanza_text[:100]
return self.stanza.stanza_variation[:100]

def save(self, *args, **kwargs):
# we trim the letter code off the stanza_variation_line_code_starts
# so we can look for the FK to the Stanza
try:
stanza_line_code_starts = self.stanza_variation_line_code_starts[:-1]
except TypeError:
stanza_line_code_starts = None

try:
self.stanza = Stanza.objects.get(
stanza_line_code_starts=stanza_line_code_starts
)
except ObjectDoesNotExist:
pass

def __str__(self) -> str:
soup = BeautifulSoup(self.stanza_variation, "html.parser")
text = soup.get_text()
super().save(*args, **kwargs)

return text[:100] + "..."
def __str__(self) -> str:
return (
"Stanza "
+ self.stanza_variation_line_code_starts
+ " associated with "
+ self.stanza.stanza_line_code_starts
+ " ("
+ self.stanza.related_manuscript.siglum
+ ")"
)


class Stanza(models.Model):
Expand Down

0 comments on commit 683191a

Please sign in to comment.