Skip to content

Commit

Permalink
added comments to the code
Browse files Browse the repository at this point in the history
  • Loading branch information
lucobaco committed Feb 7, 2023
1 parent 9d2dd68 commit 0080219
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 118 deletions.
16 changes: 13 additions & 3 deletions movie_library/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
# import necessary modules
import os
from flask import Flask
from dotenv import load_dotenv
from pymongo import MongoClient

# import blueprint for routes
from movie_library.routes import pages

# load environment variables from .env file
load_dotenv()


# function to create Flask application
def create_app():
# create Flask app
app = Flask(__name__)

# set environment variables for the database URI and secret key
app.config["MONGODB_URI"] = os.environ.get("MONGODB_URI")
app.config["SECRET_KEY"] = os.environ.get(
"SECRET_KEY", "pf9Wkove4IKEAXvy-cQkeDPhv9Cb3Ag-wyJILbq_dFw"
)
app.config["SECRET_KEY"] = os.environ.get("SECRET_KEY", "pf9Wkove4IKEAXvy-cQkeDPhv9Cb3Ag-wyJILbq_dFw")

# connect to MongoDB database using the URI and get the default database
app.db = MongoClient(app.config["MONGODB_URI"]).get_default_database()

# register blueprint for routes
app.register_blueprint(pages)

# return the Flask app
return app
45 changes: 38 additions & 7 deletions movie_library/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Import FlaskForm class from the flask_wtf library
from flask_wtf import FlaskForm

# Import various field types and validators from the wtforms library
from wtforms import (
IntegerField,
PasswordField,
Expand All @@ -7,7 +10,6 @@
TextAreaField,
URLField,
)

from wtforms.validators import (
InputRequired,
Email,
Expand All @@ -17,15 +19,21 @@
)


# Define LoginForm class as a subclass of FlaskForm
class LoginForm(FlaskForm):
# Define email field with a label "Email" and validators InputRequired and Email
email = StringField("Email", validators=[InputRequired(), Email()])
# Define password field with a label "Password" and validator InputRequired
password = PasswordField("Password", validators=[InputRequired()])
# Define submit button with a label "Login"
submit = SubmitField("Login")


# Define RegisterForm class as a subclass of FlaskForm
class RegisterForm(FlaskForm):
# Define email field with a label "Email" and validators InputRequired and Email
email = StringField("Email", validators=[InputRequired(), Email()])

# Define password field with a label "Password" and validators InputRequired and Length
password = PasswordField(
"Password",
validators=[
Expand All @@ -37,7 +45,7 @@ class RegisterForm(FlaskForm):
),
],
)

# Define confirm_password field with a label "Confirm Password" and validators InputRequired and EqualTo
confirm_password = PasswordField(
"Confirm Password",
validators=[
Expand All @@ -48,45 +56,68 @@ class RegisterForm(FlaskForm):
),
],
)

# Define submit button with a label "Register"
submit = SubmitField("Register")


# Define MovieForm class as a subclass of FlaskForm
class MovieForm(FlaskForm):
# Define title field with a label "Title" and validator InputRequired
title = StringField("Title", validators=[InputRequired()])
# Define director field with a label "Director" and validator InputRequired
director = StringField("Director", validators=[InputRequired()])

# Define year field with a label "Year" and validators InputRequired and NumberRange
year = IntegerField(
"Year",
validators=[
InputRequired(),
NumberRange(min=1878, message="Please enter a year in the format YYYY."),
],
)

# Define submit button with a label "Add Movie"
submit = SubmitField("Add Movie")


# Class definition for `StringListField` which is a subclass of `TextAreaField`.
class StringListField(TextAreaField):
# Method to convert the data stored in `self.data` into a string representation.
# Returns a string that is a concatenation of the elements in `self.data` separated by line breaks.
def _value(self):
# If there is data stored in `self.data`.
if self.data:
# Return a string that is a concatenation of the elements in `self.data` separated by line breaks.
return "\n".join(self.data)
# If there is no data stored in `self.data`.
else:
# Return an empty string.
return ""

# Method to process the data passed in `valuelist` and store it in `self.data`.
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` is a non-empty list and the first element of `valuelist`
# is not falsy (i.e. not an empty string).
if valuelist and valuelist[0]:
# Split the first element of `valuelist` by line breaks and strip whitespace from each line.
# Store the resulting list of strings in `self.data`.
self.data = [line.strip() for line in valuelist[0].split("\n")]
# If `valuelist` is an empty list or the first element of `valuelist` is falsy.
else:
# Store an empty list in `self.data`.
self.data = []


# Class definition for `ExtendedMovieForm` which is a subclass of `MovieForm`.
class ExtendedMovieForm(MovieForm):
# Class-level attribute representing a `StringListField` for the "Cast" data.
cast = StringListField("Cast")
# Class-level attribute representing a `StringListField` for the "Series" data.
series = StringListField("Series")
# Class-level attribute representing a `StringListField` for the "Tags" data.
tags = StringListField("Tags")
# Class-level attribute representing a `TextAreaField` for the "Description" data.
description = TextAreaField("Description")
# Class-level attribute representing a `URLField` for the "Video link" data.
video_link = URLField("Video link")

# Class-level attribute representing a `SubmitField` for the form submission button.
submit = SubmitField("Submit")
17 changes: 17 additions & 0 deletions movie_library/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,41 @@
from datetime import datetime


# Define the Movie dataclass
@dataclass
class Movie:
# Unique identifier for the movie
_id: str
# Title of the movie
title: str
# Director of the movie
director: str
# Year the movie was released
year: int
# List of actors in the movie
cast: list[str] = field(default_factory=list)
# List of series the movie belongs to
series: list[str] = field(default_factory=list)
# The date the movie was last watched
last_watched: datetime = None
# User-assigned rating for the movie
rating: int = 0
# List of tags to categorize the movie
tags: list[str] = field(default_factory=list)
# A brief description of the movie
description: str = None
# Link to the video for the movie
video_link: str = None


# Define the User dataclass
@dataclass
class User:
# Unique identifier for the user
_id: str
# Email address of the user
email: str
# Hashed password for the user
password: str
# List of movies in the user's watchlist
movies: list[str] = field(default_factory=list)
Loading

0 comments on commit 0080219

Please sign in to comment.