Commit b9b1decc authored by Sorrel Harriet's avatar Sorrel Harriet
Browse files

adding code resource for lab 6

parent bb0163de
#!/usr/bin/env python3
from utils import db_connect
# make bson ObjectId class available for referencing
# bson objects inside a mongo query string
from bson.objectid import ObjectId
# connect to database
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()
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>
<a href="/cgi-bin/serve_cat.py" title="serve cat">Serve new cat</a>
""".format( img_src, img_alt, str(num_flucks) ))
else:
print("<p>Oops. Something went wrong!</p>")
# output some footer html
print("</body></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:
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
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