Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
data-networks-web
lab-exercises
Commits
87078298
Commit
87078298
authored
Nov 17, 2018
by
Elaheh Homayounvala
Browse files
Delete serve_fluckable_cat.py
parent
c51e1841
Changes
1
Hide whitespace changes
Inline
Side-by-side
lab-6/cgi-bin/serve_fluckable_cat.py
deleted
100755 → 0
View file @
c51e1841
#!/usr/bin/env python3
from
utils
import
db_connect
from
bson.objectid
import
ObjectId
from
datetime
import
datetime
import
cgi
import
cgitb
cgitb
.
enable
()
# 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 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
)
))
else
:
print
(
"<p>Oops. Something went wrong!</p>"
)
# output a form with skip/fluck buttons
print
(
"""<form method="POST" action="/cgi-bin/serve_cat_beta.py">
<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
(
{
"image_id"
:
ObjectId
(
form
[
'img_id'
].
value
),
"is_flucked"
:
1
,
"timestamp"
:
datetime
.
now
().
timestamp
()
}
)
elif
'btn_skip'
in
form
:
result
=
db
.
flucks
.
insert
(
{
"image_id"
:
ObjectId
(
form
[
'img_id'
].
value
),
"is_flucked"
:
0
,
"timestamp"
:
datetime
.
now
().
timestamp
()
}
)
# output some footer html
print
(
"</body></html>"
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment