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 = """ + + +
+" + movie + "
" + edit_header = "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 "" + confirmation + "
" + page_footer + self.response.write(content) - self.response.write(response) app = webapp2.WSGIApplication([ - ('/', Index) + ('/', Index), + ('/add', AddMovie), + ('/cross-off', CrossOffMovie) ], debug=True)