def topnav(environ): # Function that defines the top navigation bar, and what appears in it. session = str(interface.user_from_cookie(db, environ)) if session == 'None': topnav = """ <DIV ID='topnav'> <DIV CLASS='topleft'> <UL> <H3><LI><A HREF='/' TITLE='Philip's Webpage'> Philip </A></LI></H3> <DIV CLASS='SELECTED'> <LI><A HREF='/' TITLE ='Homepage'> Home </a></LI> </DIV> </UL> </DIV> <DIV CLASS = "topright"> <FORM NAME = "login" METHOD="post"> <INPUT NAME='username' PLACEHOLDER = 'Email' TITLE='Username or email'> <INPUT NAME='password' TYPE = 'password' PLACEHOLDER='Password' TITLE='Password'> <INPUT TYPE='submit' VALUE='Sign In' TITLE="Click here to login" CLASS='signin'> </FORM> </DIV> </DIV> <DIV ID = 'container'> <BR><BR><BR><BR> """ else: nick, avatar = interface.get_user_details(db, session) topnav = """ <DIV ID ='topnav'> <DIV CLASS = 'topleft'> <UL> <H3> <LI><A HREF='/' TITLE="Philip's Webpage"> Philip </A></LI> </H3> """ if environ['PATH_INFO'] == '/': topnav += "<DIV CLASS ='selected'> <LI><A HREF='/' TITLE='Homepage'> Home </A></LI> </DIV>" else: topnav += "<LI><A HREF='/' TITLE='Homepage'> Home </A></LI>" if environ['PATH_INFO'] == '/my': topnav += "<DIV CLASS='selected'> <LI><A HREF='/my' TITLE='Go to my comments'> My Comments </A></LI> </DIV>" else: topnav += "<LI><A HREF='/my' TITLE='Go to my comments'> My Comments </A></LI>" if environ['PATH_INFO'] == '/comment': topnav += "<DIV CLASS='selected'> <LI><A HREF='/comment' TITLE='Go to add a comment'> Add Comment </A></LI> </DIV>" else: topnav += "<LI><A HREF='/comment' TITLE='Go to add a comment'> Add Comment </A></LI>" topnav += """ </UL> </DIV> <DIV CLASS = 'topright'> <UL> <LI><A HREF='/my' TITLE='Go to my profile'> %s </A></LI> <LI><A HREF='/logout' TITLE='Logout'> Sign Out </A></LI> </UL> </DIV> </DIV> <DIV ID = 'container'> <BR><BR><BR><BR> """ % (nick) return topnav
def my_comments(environ, start_response): # Page to look at your own comments. username = interface.user_from_cookie(db, environ) user_details = interface.get_user_details(db, username) my_page_text = "<H1> Welcome back %s! </H1><BR><H2> Your recent comments </H2>" % (user_details[0]) my_page_comments = comments(interface.list_comments_user(db, username)) headers = [('content-type', 'text/html')] start_response('200 OK', headers) page = ["<HTML>", css, topnav(environ), my_page_text, my_page_comments, "</DIV></HTML>" ] return page
def test_user_from_cookie(self): """The user_from_cookie procedure finds the name of the logged in user from the session cookie if present Test relies on working generate_cookie """ import Cookie # first test with no cookie environ = dict() email_from_cookie = interface.user_from_cookie(self.db, environ) self.assertEquals( email_from_cookie, None, "Expected None in case with no cookie, got %s" % str(email_from_cookie)) cookie = Cookie.SimpleCookie() cookie[interface.COOKIE_NAME] = 'fake sessionid' environ = {'HTTP_COOKIE': cookie[interface.COOKIE_NAME].OutputString()} email_from_cookie = interface.user_from_cookie(self.db, environ) self.assertEquals( email_from_cookie, None, "Expected None in case with invalid session id, got %s" % str(email_from_cookie)) # run tests for all test users for email, password, first, last in self.users: cookie = interface.generate_session(self.db, email) self.assertNotEqual( cookie, None, "generate_session failing, can't run user_from_cookie tests") environ = { 'HTTP_COOKIE': cookie[interface.COOKIE_NAME].OutputString() } email_from_cookie = interface.user_from_cookie(self.db, environ) self.assertEqual(email_from_cookie, email)
def my_comments(environ, start_response): # Page to look at your own comments. username = interface.user_from_cookie(db, environ) user_details = interface.get_user_details(db, username) my_page_text = "<H1> Welcome back %s! </H1><BR><H2> Your recent comments </H2>" % ( user_details[0]) my_page_comments = comments(interface.list_comments_user(db, username)) headers = [('content-type', 'text/html')] start_response('200 OK', headers) page = [ "<HTML>", css, topnav(environ), my_page_text, my_page_comments, "</DIV></HTML>" ] return page
def test_user_from_cookie(self): """The user_from_cookie procedure finds the name of the logged in user from the session cookie if present Test relies on working generate_cookie """ import Cookie # first test with no cookie environ = dict() email_from_cookie = interface.user_from_cookie(self.db, environ) self.assertEquals( email_from_cookie, None, "Expected None in case with no cookie, got %s" % str(email_from_cookie) ) cookie = Cookie.SimpleCookie() cookie[interface.COOKIE_NAME] = "fake sessionid" environ = {"HTTP_COOKIE": cookie[interface.COOKIE_NAME].OutputString()} email_from_cookie = interface.user_from_cookie(self.db, environ) self.assertEquals( email_from_cookie, None, "Expected None in case with invalid session id, got %s" % str(email_from_cookie) ) # run tests for all test users for email, password, first, last in self.users: cookie = interface.generate_session(self.db, email) self.assertNotEqual(cookie, None, "generate_session failing, can't run user_from_cookie tests") environ = {"HTTP_COOKIE": cookie[interface.COOKIE_NAME].OutputString()} email_from_cookie = interface.user_from_cookie(self.db, environ) self.assertEqual(email_from_cookie, email)
def application(environ, start_response): """Demo WSGI application""" formdata = cgi.FieldStorage(environ=environ, fp=environ['wsgi.input']) if formdata.has_key('username') and formdata.has_key('password'): username = formdata.getvalue('username') password = formdata.getvalue('password') if interface.check_login(db, username, password) == True: cookie = interface.generate_session(db, username) # Correct username or password, return a page saying so. return login_success(cookie, environ, start_response) else: # Invalid username or password, return a page with an error box saying so. return invalid(environ, start_response) elif formdata.has_key('username') or formdata.has_key('password'): # Invalid username or password, return a page with an error box saying so. return invalid(environ, start_response) session = str(interface.user_from_cookie(db, environ)) if formdata.has_key('website') and formdata.has_key('addcomment'): if formdata.getvalue('website') == 'http://': # Shouldn't post a comment about http://! return comment_unsuccessful(environ, start_response) elif session != 'None': topic = formdata.getvalue('website') comment = formdata.getvalue('addcomment') interface.add_comment(db, session, topic, comment) #Comment successfully added. Return a page saying so, with a redirect link to main page. return comment_successful(environ, start_response) else: # Unlogged users should receive a 404 Not Found error return no_permission(environ, start_response) elif formdata.has_key('website') or formdata.has_key('addcomment'): if session != 'None': #Invalid form returned. Return a page showing an error box and how to fix it. return comment_unsuccessful(environ, start_response) else: # Unlogged users should receive a 404 Not Found error. return no_permission(environ, start_response) if environ['PATH_INFO'] == '/': return main_page(environ, start_response) elif environ['PATH_INFO'] == '/login': return login_page(environ, start_response) elif environ['PATH_INFO'] == '/my': if session != 'None': return my_comments(environ, start_response) else: return show_404_app(environ, start_response) elif environ['PATH_INFO'] == '/conversation': return conversation_page(environ, start_response) elif environ['PATH_INFO'] == '/comment': if session != 'None': return add_comment(environ, start_response) else: #Unlogged users should be told they do not have permission to access this page. return no_permission(environ, start_response) elif environ['PATH_INFO'] == '/logout': if session != 'None': interface.delete_session(db, session) return logout(environ, start_response) else: # Unlogged users should receive a 404 Not Found error. return show_404_app(environ, start_response) else: # Path invalid, return 404 Not Found return show_404_app(environ, start_response)