diff --git a/main.py b/main.py index 99fa49b..ebfe90c 100644 --- a/main.py +++ b/main.py @@ -1,27 +1,156 @@ import webapp2 +import cgi -class Index(webapp2.RequestHandler): +# html boilerplate for the top of every page +page_header = """ + + + + FlickList + + + +

+ FlickList +

+""" + +# html boilerplate for the bottom of every page +page_footer = """ + + +""" + + +# 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 = "

Movie of the Day

" - response += "

" + movie + "

" + edit_header = "

Edit My Watchlist

" + + # a form for adding new movies + add_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 += ''.format(movie) + + crossoff_form = """ +
+ + +
+ """.format(crossoff_options) + + # if we have an error, make a

to display it + error = self.request.get("error") + if error: + error_esc = cgi.escape(error, quote=True) + error_element = '

' + error_esc + '

' + 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 = "" + new_movie + "" + sentence = new_movie_element + " has been added to your Watchlist!" + content = page_header + "

" + sentence + "

" + 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 "

Tommorrow's Movie

" + # if we didn't redirect by now, then all is well + crossed_off_movie_element = "" + crossed_off_movie + "" + confirmation = crossed_off_movie_element + " has been crossed off your Watchlist." + content = page_header + "

" + confirmation + "

" + page_footer + self.response.write(content) - self.response.write(response) app = webapp2.WSGIApplication([ - ('/', Index) + ('/', Index), + ('/add', AddMovie), + ('/cross-off', CrossOffMovie) ], debug=True)