Skip to content

Commit

Permalink
Merge pull request #1577 from dchiller/i1549-segment-field
Browse files Browse the repository at this point in the history
Add many-to-many segment field to source model
  • Loading branch information
dchiller authored Jul 30, 2024
2 parents 0cf9be0 + 449dd97 commit 540cb3e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
23 changes: 23 additions & 0 deletions django/cantusdb_project/main_app/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,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
Expand Down Expand Up @@ -176,6 +190,7 @@ class Meta:
# "siglum",
"holding_institution",
"shelfmark",
"segment_m2m",
"provenance",
"provenance_notes",
"full_source",
Expand Down Expand Up @@ -235,6 +250,9 @@ class Meta:
url="all-users-autocomplete"
),
}
field_classes = {
"segment_m2m": CheckboxNameModelMultipleChoiceField,
}

TRUE_FALSE_CHOICES_SOURCE = (
(True, "Full source"),
Expand Down Expand Up @@ -365,6 +383,7 @@ class Meta:
# "siglum",
"holding_institution",
"shelfmark",
"segment_m2m",
"provenance",
"provenance_notes",
"full_source",
Expand Down Expand Up @@ -392,6 +411,7 @@ class Meta:
url="holding-autocomplete"
),
"shelfmark": TextInputWidget(),
"segment_m2m": CheckboxSelectMultiple(),
"provenance": autocomplete.ModelSelect2(url="provenance-autocomplete"),
"provenance_notes": TextInputWidget(),
"date": TextInputWidget(),
Expand Down Expand Up @@ -423,6 +443,9 @@ class Meta:
url="all-users-autocomplete"
),
}
field_classes = {
"segment_m2m": CheckboxNameModelMultipleChoiceField,
}

CHOICES_FULL_SOURCE = (
(None, "None"),
Expand Down
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
3 changes: 3 additions & 0 deletions django/cantusdb_project/main_app/models/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
6 changes: 6 additions & 0 deletions django/cantusdb_project/main_app/templates/source_create.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ <h3>Create Source</h3>
<small>{{ form.shelfmark.help_text }}</small>
</p>
</div>
<div class="form-group m-1 col-lg-3">
<label for="{{ form.segment_m2m.id_for_label }}">
<small>Segments:</small>
</label>
<br>{{ form.segment_m2m }}
</div>
</div>
<div class="form-row">
<div class="form-group m-1 col-lg-3">
Expand Down
6 changes: 6 additions & 0 deletions django/cantusdb_project/main_app/templates/source_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ <h3>
<small>{{ form.shelfmark.help_text }}</small>
</p>
</div>
<div class="form-group m-1 col-lg-3">
<label for="{{ form.segment_m2m.id_for_label }}">
<small>Segments:</small>
</label>
<br>{{ form.segment_m2m }}
</div>
</div>
<div class="form-row mb-3">
<div class="form-group m-1 col-lg-7">
Expand Down
2 changes: 1 addition & 1 deletion django/cantusdb_project/main_app/views/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 540cb3e

Please sign in to comment.