Skip to content

Commit

Permalink
Merge branch 'dufferzafar/cb-151'
Browse files Browse the repository at this point in the history
  • Loading branch information
gentlecat committed Feb 17, 2015
2 parents 74a2523 + 4ac5bff commit 9da7f74
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions critiquebrainz/frontend/review/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask_wtf import Form
from flask_babel import gettext, Locale
from wtforms import TextAreaField, RadioField, SelectField, BooleanField, StringField, validators
from wtforms.validators import ValidationError
from wtforms.widgets import HiddenInput
from babel.core import UnknownLocaleError
from critiquebrainz.data.model.review import supported_languages
Expand All @@ -9,6 +10,16 @@
MIN_REVIEW_LENGTH = 25
MAX_REVIEW_LENGTH = 100000


class StateAndLength(validators.Length):
def __call__(self, form, field):
if form.state.data == "draft":
return
l = field.data and len(field.data) or 0
if l < self.min or self.max != -1 and l > self.max:
raise ValidationError(self.message)


# Loading supported languages
languages = []
for language_code in supported_languages:
Expand All @@ -22,9 +33,9 @@ class ReviewEditForm(Form):
state = StringField(widget=HiddenInput(), default='draft', validators=[validators.DataRequired()])
text = TextAreaField(gettext("Text"), [
validators.DataRequired(message=gettext("Review is empty!")),
validators.Length(min=MIN_REVIEW_LENGTH, max=MAX_REVIEW_LENGTH,
message=gettext("Review length needs to be between %(min)d and %(max)d characters.",
min=MIN_REVIEW_LENGTH, max=MAX_REVIEW_LENGTH))])
StateAndLength(min=MIN_REVIEW_LENGTH, max=MAX_REVIEW_LENGTH,
message=gettext("Review length needs to be between %(min)d and %(max)d characters.",
min=MIN_REVIEW_LENGTH, max=MAX_REVIEW_LENGTH))])
license_choice = RadioField(
choices=[
('CC BY-SA 3.0', 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>)')),
Expand Down

0 comments on commit 9da7f74

Please sign in to comment.