Skip to content

Commit

Permalink
added anthem translation field as of enhancement issue #21 (#54)
Browse files Browse the repository at this point in the history
* enhancement issue #21

* Translations field converted to dropdown list

* fixed error in one of the unit tests

* fixed second error

* refactor: anthem fields grouped as field type FormField

---------

Co-authored-by: MonEstCha <mec@gmx.net>
  • Loading branch information
MonEstCha and MonEstCha authored Sep 7, 2024
1 parent 074fdf8 commit e495561
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 26 deletions.
15 changes: 11 additions & 4 deletions forms.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from flask import request
from flask_wtf import FlaskForm
from flask_wtf import FlaskForm, Form
from wtforms import DateField, HiddenField, SelectField, SelectMultipleField, \
StringField
from wtforms import (
TimeField,
FormField
)
from wtforms.validators import DataRequired
from wtforms.widgets import TextArea
Expand All @@ -13,6 +14,14 @@
hymns = [('', 'None')] + [(h.ref, f'{h.ref} - {h.title}') for h in
Music.neh_hymns()]

translations = [('', 'None')] + [(h.translation, f'{h.translation}') for h in
Music.neh_hymns()]

class AnthemForm(Form):
title = StringField('Anthem')
composer = StringField('Anthem composer')
lyrics = StringField('Anthem lyrics', widget=TextArea())
translation = SelectField('Anthem Translation', choices=translations)

class PewSheetForm(FlaskForm):
title = HiddenField('Title')
Expand All @@ -35,9 +44,7 @@ class PewSheetForm(FlaskForm):
offertory_hymn = SelectField('Offertory Hymn', choices=hymns)
recessional_hymn = SelectField('Recessional Hymn', choices=hymns)

anthem_title = StringField('Anthem')
anthem_composer = StringField('Anthem composer')
anthem_lyrics = StringField('Anthem lyrics', widget=TextArea())
anthem_group = FormField(AnthemForm)

class Meta:
csrf = False
Expand Down
20 changes: 12 additions & 8 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from models_base import get

if typing.TYPE_CHECKING:
from forms import PewSheetForm
from forms import PewSheetForm, AnthemForm

from utils import get_neh_df, advent, closest_sunday_to, NoPandasError, logger

Expand Down Expand Up @@ -182,6 +182,7 @@ class Music:
composer: Optional[str] = field()
lyrics: Optional[str] = field()
ref: Optional[str] = field()
translation: Optional[str] = field()

@classmethod
def neh_hymns(cls) -> List['Music']:
Expand All @@ -203,7 +204,8 @@ def nehref2num(nehref: str) -> typing.Tuple[int, str]:
category='Hymn',
composer=None,
lyrics=None,
ref=f'NEH: {record.number}'
ref=f'NEH: {record.number}',
translation=f'Words/translation available at NEH: {record.number}, {record.firstLine}'
) for record in records
]
hymns.sort(key=lambda m: nehref2num(m.ref or ""))
Expand Down Expand Up @@ -419,7 +421,7 @@ def items(self) -> List[PewSheetItem]:

if self.anthem:
items.append(
ServiceItem('Anthem', [self.anthem.lyrics],
ServiceItem('Anthem', [self.anthem.lyrics, self.anthem.translation],
f'{self.anthem.title}. {self.anthem.composer}'))

if self.recessional_hymn:
Expand All @@ -438,13 +440,15 @@ def from_form(cls, form: 'PewSheetForm') -> 'Service':
else:
secondary_feasts = []

if form.anthem_title.data or form.anthem_composer.data or form.anthem_lyrics.data:
if form.anthem_group.data:
ag = form.anthem_group
anthem = Music(
title=form.anthem_title.data,
composer=form.anthem_composer.data,
lyrics=form.anthem_lyrics.data,
title=ag.title.data,
composer=ag.composer.data,
lyrics=ag.lyrics.data,
category='Anthem',
ref=None
ref=None,
translation=ag.translation.data
)
else:
anthem = None
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ python-dotenv~=0.21.0
python-slugify~=7.0.0
PyYAML~=6.0
WTForms~=3.0.1
setuptools~=50.3.2
setuptools~=71.1.0
openpyxl==3.0.9
Werkzeug~=2.2.2
38 changes: 26 additions & 12 deletions templates/serviceForm.html
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ <h3 id="titleH3"></h3>
</div>

<div class="row mb-3">
{{ form.anthem_title.label(class='col-sm-2 col-form-label') }}
{{ form.anthem_group.title.label(class='col-sm-2 col-form-label') }}
<div class="col-sm-5">
{{ form.anthem_title(class='form-control') }}
{% if form.anthem_title.errors %}
{% for error in form.anthem_title.errors %}
{{ form.anthem_group.title(class='form-control') }}
{% if form.anthem_group.title.errors %}
{% for error in form.anthem_group.title.errors %}
<small class="font-small text-danger">
{{ error }}
</small>
Expand All @@ -136,11 +136,11 @@ <h3 id="titleH3"></h3>
</div>

<div class="row mb-3">
{{ form.anthem_composer.label(class='col-sm-2 col-form-label') }}
{{ form.anthem_group.translation.label(class='col-sm-2 col-form-label') }}
<div class="col-sm-5">
{{ form.anthem_composer(class='form-control') }}
{% if form.anthem_composer.errors %}
{% for error in form.anthem_composer.errors %}
{{ form.anthem_group.translation(class='form-select') }}
{% if form.anthem_group.translation.errors %}
{% for error in form.anthem_group.translation.errors %}
<small class="font-small text-danger">
{{ error }}
</small>
Expand All @@ -150,11 +150,25 @@ <h3 id="titleH3"></h3>
</div>

<div class="row mb-3">
{{ form.anthem_lyrics.label(class='col-sm-2 col-form-label') }}
{{ form.anthem_group.composer.label(class='col-sm-2 col-form-label') }}
<div class="col-sm-5">
{{ form.anthem_lyrics(class='form-control') }}
{% if form.anthem_lyrics.errors %}
{% for error in form.anthem_lyrics.errors %}
{{ form.anthem_group.composer(class='form-control') }}
{% if form.anthem_group.composer.errors %}
{% for error in form.anthem_group.composer.errors %}
<small class="font-small text-danger">
{{ error }}
</small>
{% endfor %}
{% endif %}
</div>
</div>

<div class="row mb-3">
{{ form.anthem_group.lyrics.label(class='col-sm-2 col-form-label') }}
<div class="col-sm-5">
{{ form.anthem_group.lyrics(class='form-control') }}
{% if form.anthem_group.lyrics.errors %}
{% for error in form.anthem_group.lyrics.errors %}
<small class="font-small text-danger">
{{ error }}
</small>
Expand Down
4 changes: 3 additions & 1 deletion tests/test_pypew.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ def test_neh_lookup(self):
category='Hymn',
composer=None,
lyrics=None,
ref='NEH: 1a')
ref='NEH: 1a',
translation = 'Words/translation available at NEH: 1a, Creator of the stars of night')
)

def test_neh_lookup_unsuccessful(self):
Expand Down Expand Up @@ -223,6 +224,7 @@ def test_pew_sheet_docx_view(self, m_create_docx):
'introit_hymn': '',
'offertory_hymn': '',
'recessional_hymn': '',
'anthem_group-translation': ''
}
)
)
Expand Down

0 comments on commit e495561

Please sign in to comment.