From cbed5fe5fba48687f3e085c16f17761f5b0ee878 Mon Sep 17 00:00:00 2001 From: Dylan Hillerbrand Date: Mon, 29 Jul 2024 14:48:40 -0400 Subject: [PATCH 1/3] feat(source model): add temporary segment field to source Adds the segment_m2m many-to-many field to the source model. This additional field ensures that the data currently in the segment field is not overwritten. Once data in the segment field is transferred, this field can be renamed. Changes to the source create and source edit forms make this new field editable. Refs: #1549 --- django/cantusdb_project/main_app/forms.py | 5 ++++ .../migrations/0026_source_segment_m2m.py | 23 +++++++++++++++++++ .../main_app/models/source.py | 3 +++ .../main_app/templates/source_create.html | 6 +++++ .../main_app/templates/source_edit.html | 6 +++++ 5 files changed, 43 insertions(+) create mode 100644 django/cantusdb_project/main_app/migrations/0026_source_segment_m2m.py diff --git a/django/cantusdb_project/main_app/forms.py b/django/cantusdb_project/main_app/forms.py index dceae1750..c2469b672 100644 --- a/django/cantusdb_project/main_app/forms.py +++ b/django/cantusdb_project/main_app/forms.py @@ -26,6 +26,7 @@ from django.contrib.admin.widgets import ( FilteredSelectMultiple, ) +from django.forms.widgets import CheckboxSelectMultiple from dal import autocomplete # ModelForm allows to build a form directly from a model @@ -176,6 +177,7 @@ class Meta: # "siglum", "holding_institution", "shelfmark", + "segment_m2m", "provenance", "provenance_notes", "full_source", @@ -204,6 +206,7 @@ class Meta: url="holding-autocomplete" ), "shelfmark": TextInputWidget(), + "segment_m2m": CheckboxSelectMultiple(), "provenance": autocomplete.ModelSelect2(url="provenance-autocomplete"), "provenance_notes": TextInputWidget(), "date": TextInputWidget(), @@ -365,6 +368,7 @@ class Meta: # "siglum", "holding_institution", "shelfmark", + "segment_m2m", "provenance", "provenance_notes", "full_source", @@ -392,6 +396,7 @@ class Meta: url="holding-autocomplete" ), "shelfmark": TextInputWidget(), + "segment_m2m": CheckboxSelectMultiple(), "provenance": autocomplete.ModelSelect2(url="provenance-autocomplete"), "provenance_notes": TextInputWidget(), "date": TextInputWidget(), diff --git a/django/cantusdb_project/main_app/migrations/0026_source_segment_m2m.py b/django/cantusdb_project/main_app/migrations/0026_source_segment_m2m.py new file mode 100644 index 000000000..c3911e6e3 --- /dev/null +++ b/django/cantusdb_project/main_app/migrations/0026_source_segment_m2m.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.11 on 2024-07-26 17:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("main_app", "0025_alter_source_date"), + ] + + operations = [ + migrations.AddField( + model_name="source", + name="segment_m2m", + field=models.ManyToManyField( + blank=True, + related_name="sources", + to="main_app.segment", + verbose_name="Segments", + ), + ), + ] diff --git a/django/cantusdb_project/main_app/models/source.py b/django/cantusdb_project/main_app/models/source.py index e6a1aad7c..988942128 100644 --- a/django/cantusdb_project/main_app/models/source.py +++ b/django/cantusdb_project/main_app/models/source.py @@ -105,6 +105,9 @@ class Source(BaseModel): segment = models.ForeignKey( "Segment", on_delete=models.PROTECT, blank=True, null=True ) + segment_m2m = models.ManyToManyField( + "Segment", blank=True, related_name="sources", verbose_name="Segments" + ) source_status = models.CharField( blank=True, null=True, choices=source_status_choices, max_length=255 ) diff --git a/django/cantusdb_project/main_app/templates/source_create.html b/django/cantusdb_project/main_app/templates/source_create.html index b196677bc..541ef7eab 100644 --- a/django/cantusdb_project/main_app/templates/source_create.html +++ b/django/cantusdb_project/main_app/templates/source_create.html @@ -64,6 +64,12 @@

Create Source

{{ form.shelfmark.help_text }}

+
+ +
{{ form.segment_m2m }} +
diff --git a/django/cantusdb_project/main_app/templates/source_edit.html b/django/cantusdb_project/main_app/templates/source_edit.html index 58f240ee7..51bb130be 100644 --- a/django/cantusdb_project/main_app/templates/source_edit.html +++ b/django/cantusdb_project/main_app/templates/source_edit.html @@ -66,6 +66,12 @@

{{ form.shelfmark.help_text }}

+
+ +
{{ form.segment_m2m }} +
From 12f5202d992a819725c0b9fc27df614e67836835 Mon Sep 17 00:00:00 2001 From: Dylan Hillerbrand Date: Tue, 30 Jul 2024 12:32:19 -0400 Subject: [PATCH 2/3] fix(source forms): Show name without ID for segment field on source forms --- django/cantusdb_project/main_app/forms.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/django/cantusdb_project/main_app/forms.py b/django/cantusdb_project/main_app/forms.py index c2469b672..9e4807f43 100644 --- a/django/cantusdb_project/main_app/forms.py +++ b/django/cantusdb_project/main_app/forms.py @@ -57,6 +57,19 @@ class SelectWidgetNameModelChoiceField(NameModelChoiceField): widget = SelectWidget() +class CheckboxNameModelMultipleChoiceField(forms.ModelMultipleChoiceField): + """ + A custom ModelMultipleChoiceField that overrides the label_from_instance method + to display the object's name attribute instead of str(object) and uses + the CheckboxMulitpleSelect widget. + """ + + def label_from_instance(self, obj): + return obj.name + + widget = CheckboxSelectMultiple() + + class ChantCreateForm(forms.ModelForm): class Meta: model = Chant @@ -206,7 +219,6 @@ class Meta: url="holding-autocomplete" ), "shelfmark": TextInputWidget(), - "segment_m2m": CheckboxSelectMultiple(), "provenance": autocomplete.ModelSelect2(url="provenance-autocomplete"), "provenance_notes": TextInputWidget(), "date": TextInputWidget(), @@ -238,6 +250,9 @@ class Meta: url="all-users-autocomplete" ), } + field_classes = { + "segment_m2m": CheckboxNameModelMultipleChoiceField, + } TRUE_FALSE_CHOICES_SOURCE = ( (True, "Full source"), @@ -428,6 +443,9 @@ class Meta: url="all-users-autocomplete" ), } + field_classes = { + "segment_m2m": CheckboxNameModelMultipleChoiceField, + } CHOICES_FULL_SOURCE = ( (None, "None"), From 449dd9735d1e7cd204f9015ed40d69f717278d0a Mon Sep 17 00:00:00 2001 From: Dylan Hillerbrand Date: Tue, 30 Jul 2024 12:41:27 -0400 Subject: [PATCH 3/3] refactor(source edit view): remove duplicate object query --- django/cantusdb_project/main_app/views/source.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django/cantusdb_project/main_app/views/source.py b/django/cantusdb_project/main_app/views/source.py index f838735e3..208eabec8 100644 --- a/django/cantusdb_project/main_app/views/source.py +++ b/django/cantusdb_project/main_app/views/source.py @@ -405,7 +405,7 @@ class SourceEditView(LoginRequiredMixin, UserPassesTestMixin, UpdateView): pk_url_kwarg = "source_id" def get_context_data(self, **kwargs): - source = self.get_object() + source = self.object context = super().get_context_data(**kwargs) if source.segment and source.segment.id == BOWER_SEGMENT_ID: