Skip to content

Commit

Permalink
Allow no choice in license_choice
Browse files Browse the repository at this point in the history
manual validation validate_license_choice will take care of choice
validation. see comments for rationale.
  • Loading branch information
amCap1712 committed Sep 26, 2022
1 parent db773f1 commit 278ab14
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion critiquebrainz/frontend/forms/review.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class ReviewEditForm(FlaskForm):
choices=[
('CC BY-SA 3.0', lazy_gettext('Allow commercial use of this review(<a href="https://creativecommons.org/licenses/by-sa/3.0/" target="_blank">CC BY-SA 3.0 license</a>)')), # noqa: E501
('CC BY-NC-SA 3.0', lazy_gettext('Do not allow commercial use of this review, unless approved by MetaBrainz Foundation (<a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" target="_blank">CC BY-NC-SA 3.0 license</a>)')), # noqa: E501
])
],
validate_choice=False
)
remember_license = BooleanField(lazy_gettext("Remember this license choice for further preference"))
language = SelectField(lazy_gettext("You need to accept the license agreement!"), choices=languages)
rating = IntegerField(lazy_gettext("Rating"), widget=Input(input_type='number'), validators=[validators.Optional()])
Expand All @@ -63,6 +65,19 @@ def validate_license_choice(self, field):
if not field.raw_data or not field.raw_data[0]:
raise ValidationError(lazy_gettext("You need to choose a license"))

# choice validation for RadioField's is done in pre_validate in WTForms which executes prior to
# this validator. it does not take into consideration whether the review is draft or not so we
# have to disable it with validate_choice = False to allow no choice in license field. so we also
# have to manually validate the license choice here
match = False
for value, label in field.choices:
if value == field.data:
match = True
break

if not match:
raise ValidationError(lazy_gettext("Not a valid choice"))


class ReviewCreateForm(ReviewEditForm):
agreement = BooleanField(validators=[
Expand Down

0 comments on commit 278ab14

Please sign in to comment.