Commit 9b5cef07 authored by Sorrel Harriet's avatar Sorrel Harriet
Browse files

updating app for Lab 8

parent 86563d0a
......@@ -3,8 +3,13 @@
This script handles the processing
of the login form in the catflucks application.
Note: there is lots of code repetition here...
What could we do about that?
there is code repetition here...
What could we do about that?
Also note:
this functionality is not yet integrated
with the rest of the application.
How could it be?
# import modules from Python standard library
......@@ -20,16 +25,15 @@ import utils
# connect to database
db = utils.db_connect( config )
# tell the browser we are outputting HTML
print("Content-Type: text/html\n")
# get the form data
form = cgi.FieldStorage()
# tell the browser we are sending HTML
print("Content-Type: text/html\n")
# check that login form was submitted
if 'btn_login' in form:
# encrypt the password
pw = form['password'].value
# get the username from the form
username = form['username'].value
# look for an account with the username
account = db.accounts.find_one({
......@@ -37,15 +41,21 @@ if 'btn_login' in form:
# check if an account came back
if account is not None:
# if so, compare the unhashed password from the form
# get the unhashed password sent in the form
pw = form['password'].value
# compare the unhashed password from the form
# with the hashed version in the database
if bcrypt.checkpw(pw.encode('utf-8'),account['password'].encode('utf-8')):
response = "Hello {}!".format(account['name']['first'])
if bcrypt.checkpw( pw.encode('utf-8'), account['password'].encode('utf-8') ):
# if they match, output a personal greeting
response = "Hello {}!".format( account['name']['first'] )
# if not, we know it was a wrong password
response = "Wrong password."
response = "Sorry, I don't think I know you!"+username
# for testing only, output response
# their username didn't match, so tell them that
response = "Sorry, I don't think I know you!"
# for testing purposes, output the response here...
# they didn't come via the form, so tell them to go away!
print("<p>How did you get here? Maybe you want to <a href='/cgi-bin/'>go back home</a>?</p>")
#!/usr/bin/env python3
This script is the main entry point to
the catflucks application.
# import modules from Python standard library
from bson.objectid import ObjectId
import cgi
import cgitb
from datetime import datetime
# import custom modules
from config import config
import utils
# connect to database
db = utils.db_connect( config )
# render header HTML
print("Content-Type: text/html\n")
print( utils.render_template( config['TEMPLATE_DIR'] + 'header.html') )
"""# decide what to do based on page parameter in query string
# FieldStorage also contains data sent in GET in dict format
params = cgi.FieldStorage()
# check if page parameter is set
# NOTE: this is a pretty horrible way to
# implement view switching...consider it
# is for demonstrative purposes only!
if ['page'] in params:
if params['page'] == 'login':
# render login form
print( utils.render_template( config['TEMPLATE_DIR'] + 'login.html') )
# render normal view"""
# render login form. Could use JS to collapse this by default.
print( utils.render_template( config['TEMPLATE_DIR'] + 'login.html') )
# get one random document from images collection
result = db.images.aggregate(
[{ '$sample': { 'size': 1 } }]
# if a result came back, do stuff with it...
if result:
# iterate through objects in the cursor (should only be 1)
for img in result:
# pull out the img url and alt text
img_src = img['url']
img_alt = img['alt']
img_id = img['_id']
# render serve_cat template, passing it dynamic data
print( utils.render_template( config['TEMPLATE_DIR']+'serve_cat.html', data=[img_src, img_alt] ) )
# find and count flucks with matching img_id and where is_flucked is 1
num_flucks = db.flucks.find( {"image_id": ObjectId(img_id), "is_flucked":1} ).count()
# render cat_stats template, passing it dynamic data
print( utils.render_template( config['TEMPLATE_DIR']+'cat_stats.html', data=[num_flucks] ) )
# render form_fluck template, passing it dynamic data
print( utils.render_template( config['TEMPLATE_DIR']+'form_fluck.html', data=["/cgi-bin/",img_id] ) )
print("<p>Oops. Something went wrong!</p>")
# get form data
form = cgi.FieldStorage()
# check if either button clicked and insert a fluck in the database
if 'btn_fluck' in form:
result = db.flucks.insert( {
} )
elif 'btn_skip' in form:
result = db.flucks.insert( {
} )
# render cat_stats template, passing it the dynamic data
print( utils.render_template( config['TEMPLATE_DIR']+'footer.html' ) )
#!/usr/bin/env python3
This module provides a set of reuseable utility functions
This is a Google style docstring by the way.
Read more about them here:
from pymongo import MongoClient
def db_connect( config ):
""" Provides a connection to mongoDB database
Object: A handle to a mongoDB database
# try to create instance of MongoClient object
client = MongoClient( config['SERVER_ADDRESS'], config['PORT'] )
# raise a custom exception
raise Exception("Problem connecting to the database!")
# if we have a mongo client...
# switch to the specified database
db = client[ config['DATABASE_NAME'] ]
# check a handle was returned
if db is not None:
# return a handle to the database
return db
def render_template( temp_path, data=[] ):
""" Reads in an HTML string from file
replacing any placeholders with values
supplied in data list.
Input params:
temp_path: the path to the template file
data: optional list of data values
String: A formatted string of HTML
# open and read the template file
with open(temp_path, 'r') as f:
html =
raise Exception("Could not open template")
if data is not None:
# replace placeholders with data in list
html = html.format(*data)
raise Exception("Problem parsing data to template")
return html
#!/usr/bin/env python3
# above 'she-bang' line makes the script executable from command line
""" A Simple Web Server
Run with ./
Make sure all cgi scripts are executable
for single script:
chmod +x
or for a whole directory:
chmod -r +x cgi-bin/
import http.server # import http.server module
import cgitb; cgitb.enable() # import and enable cgitb module for exception handling
PORT = 8000 # specifies the port number to accept connections on
server = http.server.HTTPServer # provides simple web server
handler = http.server.CGIHTTPRequestHandler # provides request handler
server_address = ("", PORT) # specify server directory and port number
handler.cgi_directories = ["/","/cgi-bin","/htbin"] # where CGI scripts will reside in relation to the `server' directory
print("Starting server...") # outputs a message
httpd = server(server_address, handler) # creates the server, passing it the server address and port number, as well as the CGI handler (httpd stands for HTTP Daemon)
print("serving at port", PORT) # outputs a message
httpd.serve_forever() # puts program in infinite loop so that the server can `serve_forever'
<p>This poor cat has been flucked {} times already.</p>
<form method="POST" action="{}">
<input type="hidden" value="{}" name="img_id">
<input name="btn_skip" type="submit" value="Skip">
<input name="btn_fluck" type="submit" value="Fluck">
<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>Hello Caflucks</title>
<h1>Welcome to Catflucks</h1>
<h3>Log in</h3>
<form method="POST" action="/cgi-bin/">
<label for="username">Username:</label>
<input type="text" name="username">
<label for="password">Password:</label>
<input type="password" name="password">
<input type="submit" name="btn_login">
<p>You are viewing a random image of a cat.</p>
<img src="{}" alt="{}" width=500>
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment