-
Notifications
You must be signed in to change notification settings - Fork 0
/
hdh.py
144 lines (127 loc) · 4.54 KB
/
hdh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import os
import psycopg2
import urlparse
#module for building web apps
from flask import Flask, url_for, render_template, request
#module for processing html and xml
from lxml import html
import requests
#imports scrape
import scrape
#imports ratings dictionary
import ratings
app = Flask(__name__)
urlparse.uses_netloc.append("postgres")
url = urlparse.urlparse(os.environ["DATABASE_URL"])
conn = psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
cursor = conn.cursor()
#input = item and item's rating
#outputs thumbs up/down
def rating_for (item, ratings):
#checks if items is in dictionary
if item in ratings:
#if rating returns true: thumbs up
if ratings[item]:
return thumbsUp()
#else thumbs return thumbs down
else:
return thumbsDown()
#return no rating if item isn't in dictionary
else:
return ''
#gets the ratings from the database
def getRatingsFromHall(diningHall):
ratings = {}
cursor.execute("SELECT item,rating FROM ratings WHERE dining_hall='" + diningHall + "';")
while True:
data = cursor.fetchone()
if data == None:
break
ratings[str(data[0])] = data[1]
return ratings
def renderPage(diningHall):
if diningHall == 'Canyon Vista':
return renderCV()
elif diningHall == '64 Degrees':
return render64d()
elif diningHall == "Pines":
return renderPines()
else:
return renderMain()
#defines image file for thumbs up from glyphicon
def thumbsUp():
return ' <span class="glyphicon glyphicon-thumbs-up"></span>'
#defines image file for thumbs down from glyphicon
def thumbsDown():
return ' <span class="glyphicon glyphicon-thumbs-down"></span>'
#helps import ratings dictionary
app.jinja_env.globals.update(rating_for=rating_for)
#home page rendering main.html
@app.route('/')
def renderMain():
return render_template('main.html')
#modifies the database based on user input
@app.route('/addRating/<diningHall>/<item>', methods=('GET', 'POST'))
def addRating(diningHall, item):
rating = request.form['rating']
command = "SELECT rating FROM ratings WHERE dining_hall='" + diningHall + "' and item='" + item + "';"
before = ['']
cursor.execute(command)
data = cursor.fetchone()
if data != None:
before[0] = data[0]
if before[0] == '':
if rating != '':
command = "INSERT INTO ratings VALUES ('" + diningHall + "', '" + item + "', " + rating + ");"
cursor.execute(command)
conn.commit()
return renderPage(diningHall)
else:
return renderPage(diningHall)
elif rating == '':
command = "DELETE FROM ratings WHERE dining_hall='" + diningHall + "' and item='" + item + "';"
cursor.execute(command)
conn.commit()
return renderPage(diningHall)
elif before[0] != rating:
command = "UPDATE ratings SET rating='" + rating + "' WHERE dining_hall='" + diningHall + "' and item='" + item + "';"
cursor.execute(command)
conn.commit()
return renderPage(diningHall)
else:
return renderPage(diningHall)
#defines CV page
@app.route('/canyonvista')
def renderCV():
#items variable pulls to scrape.py
#ratings variable pulls from ratings.py and ratings dictionary
print "This is renderCV()"
return render_template('CV.html', breakfast = scrape.allMealItems('Canyon Vista', 'Breakfast'),
lunch = scrape.allMealItems('Canyon Vista', 'Lunch'),
dinner = scrape.allMealItems('Canyon Vista', 'Dinner'),
ratings = getRatingsFromHall('Canyon Vista'),
diningHall = 'Canyon Vista')
#defines 64d's page
@app.route('/64degrees')
def render64d():
return render_template('64degrees.html', breakfast = scrape.allMealItems('64 Degrees', 'Breakfast'),
lunch = scrape.allMealItems('64 Degrees', 'Lunch'),
dinner = scrape.allMealItems('64 Degrees', 'Dinner'),
ratings = getRatingsFromHall('64 Degrees'),
diningHall = '64 Degrees')
#defines the pines page
@app.route('/pines')
def renderPines():
return render_template('pines.html', breakfast = scrape.allMealItems('Pines', 'Breakfast'),
lunch = scrape.allMealItems('Pines', 'Lunch'),
dinner = scrape.allMealItems('Pines', 'Dinner'),
ratings = getRatingsFromHall('Pines'),
diningHall = 'Pines')
if __name__=="__main__":
app.run(debug=False, host="0.0.0.0", port=10101)