diff --git a/cgi-bin/.myserve_cat.py.swp b/cgi-bin/.myserve_cat.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..85001640de113c39d8f680b7b15c483463167daf Binary files /dev/null and b/cgi-bin/.myserve_cat.py.swp differ diff --git a/cgi-bin/__pycache__/myutils.cpython-35.pyc b/cgi-bin/__pycache__/myutils.cpython-35.pyc new file mode 100644 index 0000000000000000000000000000000000000000..045c8e0d467591f624983cf3c1e4f4638910d7fc Binary files /dev/null and b/cgi-bin/__pycache__/myutils.cpython-35.pyc differ diff --git a/cgi-bin/myserve_cat.py b/cgi-bin/myserve_cat.py new file mode 100755 index 0000000000000000000000000000000000000000..ba914c6666951288902588ef164b05850fdf6210 --- /dev/null +++ b/cgi-bin/myserve_cat.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 + +from myutils import db_connect + +# make bson ObjectId class available for referencing +# bson objects inside a mongo query string +from bson.objectid import ObjectId +from datetime import datetime +# connect to database +import cgi +import cgitb +cgitb.enable() +form=cgi.FieldStorage() +db = db_connect() + +# output some header html +print("Content-Type: text/html\n") +print("""<!DOCTYPE html> +<html lang="en"> +<head> +<meta charset="utf-8"> +<title>Hello Caflucks</title> +</head> +<body> +<h1>Welcome to Catflucks</h1>""") + + +# get one random document from images collection +result = db.images.aggregate( + [{ '$sample': { 'size': 1 } }] +) + +# if a result came back +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'] + + # 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() + num_nice = db.flucks.find( {"image_id": ObjectId(img_id), "is_nice":1} ).count() + print("""<p>You are viewing a random image of a cat.</p> + <img src="{}" alt="{}" width=500> + <p>This poor cat has been flucked {} times already.</p> + <p>Nice Points {}</p> + <a href="/cgi-bin/myserve_cat.py" title="serve cat">Serve new cat</a> + """.format( img_src, img_alt, str(num_flucks), str(num_nice) )) +else: + print("<p>Oops. Something went wrong!</p>") + +# output some footer html output a form with skip/fluck buttons +print("""<form method="POST" action="/cgi-bin/myserve_cat.py"> + <input type="hidden" value="{}" name="img_id"> + <input name="btn_skip" type="submit" value="Skip"> + <input name="btn_fluck" type="submit" value="Fluck"> + <input name="btn_nice" type="submit" value="Nice"> + </form>""".format( img_id )) +# check if either button clicked and insert a fluck in the database +if 'btn_fluck' in form: + result = db.flucks.insert( { + "image_id":ObjectId(form['img_id'].value), + "is_flucked":1, + "timestamp":datetime.now().timestamp(), + "is_nice":0 + } ) +elif 'btn_skip' in form: + result = db.flucks.insert( { + "image_id":ObjectId(form['img_id'].value), + "is_flucked":0, + "timestamp":datetime.now().timestamp(), + "is_nice":0 + } ) +elif 'btn_nice' in form: + result = db.flucks.insert( { + "image_id":ObjectId(form['img_id'].value), + "is_flucked":0, + "timestamp":datetime.now().timestamp(), + "is_nice":1 + } ) +print("</body></html>") diff --git a/cgi-bin/myservecat.py b/cgi-bin/myservecat.py new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/cgi-bin/myutils.py b/cgi-bin/myutils.py new file mode 100755 index 0000000000000000000000000000000000000000..c217a9b27bd112656af494eabfdae64a72c39d6a --- /dev/null +++ b/cgi-bin/myutils.py @@ -0,0 +1,30 @@ +#!/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: + https://www.python.org/dev/peps/pep-0257/ +""" + +from pymongo import MongoClient + +def db_connect(): + """ Provides a connection to mongoDB database + + Returns: + Object: A handle to a mongoDB database + """ + # try to create instance of MongoClient object + try: + client = MongoClient('mongodb://localhost:27017/') + except: + print("Error: failed to create mongo client") + raise + # if we have a mongo client... + else: + # switch to the specified database + db = client.catflucks + # return a handle to the database + return db + diff --git a/dump/catflucks/accounts.bson b/dump/catflucks/accounts.bson new file mode 100644 index 0000000000000000000000000000000000000000..f7495f04a5c0779de913effc8aa4284f27d2a10c Binary files /dev/null and b/dump/catflucks/accounts.bson differ diff --git a/dump/catflucks/accounts.metadata.json b/dump/catflucks/accounts.metadata.json new file mode 100644 index 0000000000000000000000000000000000000000..0fbe2667003f97231276c64eb996ccab7db50ad8 --- /dev/null +++ b/dump/catflucks/accounts.metadata.json @@ -0,0 +1 @@ +{ "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "catflucks.accounts" } ] } \ No newline at end of file diff --git a/dump/catflucks/flucks.bson b/dump/catflucks/flucks.bson new file mode 100644 index 0000000000000000000000000000000000000000..d8f227e8d594e884fd32c51ae4b8faf867510a38 Binary files /dev/null and b/dump/catflucks/flucks.bson differ diff --git a/dump/catflucks/flucks.metadata.json b/dump/catflucks/flucks.metadata.json new file mode 100644 index 0000000000000000000000000000000000000000..a75d15a2bd527d850dc76457cc7ba5e1b05b1d7e --- /dev/null +++ b/dump/catflucks/flucks.metadata.json @@ -0,0 +1 @@ +{ "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "catflucks.flucks" } ] } \ No newline at end of file diff --git a/dump/catflucks/images.bson b/dump/catflucks/images.bson new file mode 100644 index 0000000000000000000000000000000000000000..448eb07b3d19f1c0bf05370acab47cae26a117c5 Binary files /dev/null and b/dump/catflucks/images.bson differ diff --git a/dump/catflucks/images.metadata.json b/dump/catflucks/images.metadata.json new file mode 100644 index 0000000000000000000000000000000000000000..b12bd6827138cb3f392f9547d3dd1ab3391b1606 --- /dev/null +++ b/dump/catflucks/images.metadata.json @@ -0,0 +1 @@ +{ "indexes" : [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "catflucks.images" } ] } \ No newline at end of file diff --git a/dump/catflucks/system.indexes.bson b/dump/catflucks/system.indexes.bson new file mode 100644 index 0000000000000000000000000000000000000000..3a045ae92526736da2b0678107defbc96f47294f Binary files /dev/null and b/dump/catflucks/system.indexes.bson differ diff --git a/mysimpleServer.py b/mysimpleServer.py new file mode 100755 index 0000000000000000000000000000000000000000..799937412843f145c900006414d2a7918a215253 --- /dev/null +++ b/mysimpleServer.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 +# above 'she-bang' line makes the script executable from command line + +""" A Simple Web Server + Run with ./simpleServer.py + Make sure all cgi scripts are executable + for single script: + chmod +x simpleServer.py + 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'