Project 4: Movie Watchlist: Create a custom WTForm widget (+36, -2)
forms.py (+36, -2)
From:
curriculum/section14/lectures/13_create_custom_wtform_widget_multiline_input/start/movie_library/forms.py
To:
curriculum/section14/lectures/13_create_custom_wtform_widget_multiline_input/end/movie_library/forms.py
index b22bb36..e8d61eb 100644
--- a/curriculum/section14/lectures/13_create_custom_wtform_widget_multiline_input/start/movie_library/forms.py
+++ b/curriculum/section14/lectures/13_create_custom_wtform_widget_multiline_input/end/movie_library/forms.py
@@ -1,7 +1,16 @@
from flask_wtf import FlaskForm
-from wtforms import IntegerField, StringField, SubmitField
+from wtforms import (
+ IntegerField,
+ StringField,
+ SubmitField,
+ TextAreaField,
+ URLField,
+)
-from wtforms.validators import InputRequired, NumberRange
+from wtforms.validators import (
+ InputRequired,
+ NumberRange,
+)
class MovieForm(FlaskForm):
@@ -17,3 +26,28 @@ class MovieForm(FlaskForm):
)
submit = SubmitField("Add Movie")
+
+
+class StringListField(TextAreaField):
+ def _value(self):
+ if self.data:
+ return "\n".join(self.data)
+ else:
+ return ""
+
+ def process_formdata(self, valuelist):
+ # checks valuelist contains at least 1 element, and the first element isn't falsy (i.e. empty string)
+ if valuelist and valuelist[0]:
+ self.data = [line.strip() for line in valuelist[0].split("\n")]
+ else:
+ self.data = []
+
+
+class ExtendedMovieForm(MovieForm):
+ cast = StringListField("Cast")
+ series = StringListField("Series")
+ tags = StringListField("Tags")
+ description = TextAreaField("Description")
+ video_link = URLField("Video link")
+
+ submit = SubmitField("Submit")