Commit 87078298 authored by Elaheh Homayounvala's avatar Elaheh Homayounvala
Browse files


parent c51e1841
#!/usr/bin/env python3
from utils import db_connect
from bson.objectid import ObjectId
from datetime import datetime
import cgi
import cgitb
# connect to database
db = db_connect()
# output some header html
print("Content-Type: text/html\n")
print("""<!DOCTYPE html>
<html lang="en">
<meta charset="utf-8">
<title>Hello Caflucks</title>
<h1>Welcome to Catflucks</h1>""")
# get the form data
form = cgi.FieldStorage()
# 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>
""".format( img_src, img_alt, str(num_flucks) ))
print("<p>Oops. Something went wrong!</p>")
# output a form with skip/fluck buttons
print("""<form method="POST" action="/cgi-bin/">
<input type="hidden" value="{}" name="img_id">
<input name="btn_skip" type="submit" value="Skip">
<input name="btn_fluck" type="submit" value="Fluck">
</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( {
} )
elif 'btn_skip' in form:
result = db.flucks.insert( {
} )
# output some footer html
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