Skip to content
This repository was archived by the owner on Dec 1, 2022. It is now read-only.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
155 changes: 142 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,156 @@
import webapp2
import cgi

class Index(webapp2.RequestHandler):
# html boilerplate for the top of every page
page_header = """
<!DOCTYPE html>
<html>
<head>
<title>FlickList</title>
<style type="text/css">
.error {
color: red;
}
</style>
</head>
<body>
<h1>
<a href="/">FlickList</a>
</h1>
"""

# html boilerplate for the bottom of every page
page_footer = """
</body>
</html>
"""


# a list of movies that nobody should be allowed to watch
terrible_movies = [
"Gigli",
"Star Wars Episode 1: Attack of the Clones",
"Paul Blart: Mall Cop 2",
"Nine Lives"
]

def getRandomMovie(self):

# TODO: make a list with at least 5 movie titles
def getCurrentWatchlist():
""" Returns the user's current watchlist """

# TODO: randomly choose one of the movies, and return it
# for now, we are just pretending
return [ "Star Wars", "Minions", "Freaky Friday", "My Favorite Martian" ]

return "The Big Lebowski"

class Index(webapp2.RequestHandler):
""" Handles requests coming in to '/' (the root of our site)
e.g. www.flicklist.com/
"""

def get(self):
movie = self.getRandomMovie()

# build the response string
response = "<h1>Movie of the Day</h1>"
response += "<p>" + movie + "</p>"
edit_header = "<h3>Edit My Watchlist</h3>"

# a form for adding new movies
add_form = """
<form action="/add" method="post">
<label>
I want to add
<input type="text" name="new-movie"/>
to my watchlist.
</label>
<input type="submit" value="Add It"/>
</form>
"""

# a form for crossing off movies
# (first we build a dropdown from the current watchlist items)
crossoff_options = ""
for movie in getCurrentWatchlist():
crossoff_options += '<option value="{0}">{0}</option>'.format(movie)

crossoff_form = """
<form action="/cross-off" method="post">
<label>
I want to cross off
<select name="crossed-off-movie"/>
{0}
</select>
from my watchlist.
</label>
<input type="submit" value="Cross It Off"/>
</form>
""".format(crossoff_options)

# if we have an error, make a <p> to display it
error = self.request.get("error")
if error:
error_esc = cgi.escape(error, quote=True)
error_element = '<p class="error">' + error_esc + '</p>'
else:
error_element = ''

# combine all the pieces to build the content of our response
main_content = edit_header + add_form + crossoff_form + error_element
content = page_header + main_content + page_footer
self.response.write(content)


class AddMovie(webapp2.RequestHandler):
""" Handles requests coming in to '/add'
e.g. www.flicklist.com/add
"""

def post(self):
# look inside the request to figure out what the user typed
new_movie = self.request.get("new-movie")

# TODO 2
# if the user typed nothing at all, redirect and yell at them


# TODO 3
# if the user wants to add a terrible movie, redirect and yell at them


# TODO 1
# 'escape' the user's input so that if they typed HTML, it doesn't mess up our site

# build response content
new_movie_element = "<strong>" + new_movie + "</strong>"
sentence = new_movie_element + " has been added to your Watchlist!"
content = page_header + "<p>" + sentence + "</p>" + page_footer
self.response.write(content)


class CrossOffMovie(webapp2.RequestHandler):
""" Handles requests coming in to '/cross-off'
e.g. www.flicklist.com/cross-off
"""

def post(self):
# look inside the request to figure out what the user typed
crossed_off_movie = self.request.get("crossed-off-movie")

if (crossed_off_movie in getCurrentWatchlist()) == False:
# the user tried to cross off a movie that isn't in their list,
# so we redirect back to the front page and yell at them

# make a helpful error message
error = "'{0}' is not in your Watchlist, so you can't cross it off!".format(crossed_off_movie)

# redirect to homepage, and include error as a query parameter in the URL
self.redirect("/?error=" + error)

# TODO: pick a different random movie, and display it under
# the heading "<h1>Tommorrow's Movie</h1>"
# if we didn't redirect by now, then all is well
crossed_off_movie_element = "<strike>" + crossed_off_movie + "</strike>"
confirmation = crossed_off_movie_element + " has been crossed off your Watchlist."
content = page_header + "<p>" + confirmation + "</p>" + page_footer
self.response.write(content)

self.response.write(response)

app = webapp2.WSGIApplication([
('/', Index)
('/', Index),
('/add', AddMovie),
('/cross-off', CrossOffMovie)
], debug=True)