Project 4: Movie Watchlist: Receive and validate data using WTForms (+34, -2)

models.py (+17, -0)

From: curriculum/section14/lectures/08_receive_validate_wtform/end/movie_library/models.py

To: curriculum/section14/lectures/08_receive_validate_wtform/end/movie_library/models.py

            
            new file mode 100644
index 0000000..2c19798
--- /dev/null
+++ b/curriculum/section14/lectures/08_receive_validate_wtform/end/movie_library/models.py
@@ -0,0 +1,17 @@
+from dataclasses import dataclass, field
+from datetime import datetime
+
+
+@dataclass
+class Movie:
+    _id: str
+    title: str
+    director: str
+    year: int
+    cast: list[str] = field(default_factory=list)
+    series: list[str] = field(default_factory=list)
+    last_watched: datetime = None
+    rating: int = 0
+    tags: list[str] = field(default_factory=list)
+    description: str = None
+    video_link: str = None
        

routes.py (+17, -2)

From: curriculum/section14/lectures/08_receive_validate_wtform/start/movie_library/routes.py

To: curriculum/section14/lectures/08_receive_validate_wtform/end/movie_library/routes.py

            
            index b3bb925..6d3bf0c 100644
--- a/curriculum/section14/lectures/08_receive_validate_wtform/start/movie_library/routes.py
+++ b/curriculum/section14/lectures/08_receive_validate_wtform/end/movie_library/routes.py
@@ -1,11 +1,17 @@
+import uuid
+from dataclasses import asdict
+
 from flask import (
     Blueprint,
+    current_app,
     redirect,
     render_template,
     session,
+    url_for,
     request,
 )
 from movie_library.forms import MovieForm
+from movie_library.models import Movie
 
 
 pages = Blueprint(
@@ -25,8 +31,17 @@ def index():
 def add_movie():
     form = MovieForm()
 
-    if request.method == "POST":
-        pass
+    if form.validate_on_submit():
+        movie = Movie(
+            _id=uuid.uuid4().hex,
+            title=form.title.data,
+            director=form.director.data,
+            year=form.year.data,
+        )
+
+        current_app.db.movie.insert_one(asdict(movie))
+
+        return redirect(url_for(".index"))
 
     return render_template(
         "new_movie.html", title="Movies Watchlist - Add Movie", form=form