-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
172 lines (103 loc) · 4.07 KB
/
server.py
File metadata and controls
172 lines (103 loc) · 4.07 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
"""Road Trip app"""
from jinja2 import StrictUndefined
from flask import Flask, render_template, request, flash, redirect, session, jsonify
from flask_debugtoolbar import DebugToolbarExtension
from model import connect_to_db, User, Trip, db
import os
import yelping
app = Flask(__name__)
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.secret_key = "jaofep98urOSAKFDa89"
#Included to make Jinja variables easier to debug:
app.jinja_env.undefined = StrictUndefined
#----------------------------------------------------------------------------
@app.route('/', methods=['GET'])
def index():
"""Homepage allows users to log in"""
return render_template("homepage.html")
@app.route('/login', methods=['POST'])
def user_login():
"""Logs user into system"""
username = request.form.get("name")
password = request.form.get("password")
# print "username: ", username # this works
# print "password: ", password # this works
user = User.query.filter_by(username=username).first()
# print "User query result: ", user # this works, <User user_id: 1 username: admin> // User query result: None
if not user:
flash("User name not found. Please try again")
return redirect("/")
if user.password != password:
flash("Your password was incorrect. Please try again")
return redirect("/")
else:
session["user_id"] = user.user_id
flash("You are logged in")
return render_template("search_form.html")
@app.route('/new-user')
def register_form():
return render_template("new_user.html")
@app.route('/register', methods=['POST'])
def new_user():
"""Allows new user to register and adds them to database"""
username = request.form.get("name")
password = request.form.get("password")
new_user = User(username=username, password=password)
db.session.add(new_user)
db.session.commit()
flash("Thank you for registering as a user")
#code 307 preserves the original form method through the redirect
return redirect("/login", code=307)
@app.route('/map', methods=['POST'])
def new_search():
"""Takes user's start and end location and returns road trip results"""
start = request.form.get("start")
end = request.form.get("end")
# print "start!!", start
# print "end!!", end
return render_template("map.html", start=start,
end=end,
key=os.environ['PLACES_SECRET_KEY'])
@app.route('/saved-trips', methods=['POST'])
def saved_trips():
"""Allows user to save trips and adds them to database"""
trip_name = request.form.get("trip_name")
start = request.form.get("start")
end = request.form.get("end")
user_id = session.get("user_id")
new_trip = Trip(user_id=user_id, trip_name=trip_name, start=start, end=end)
db.session.add(new_trip)
db.session.commit()
return "succeess"
@app.route('/my-trips')
def userTrips():
"""Displays saved trip for registered users"""
trips = Trip.query.order_by('trip_name').all()
return render_template("my_trips.html", trips=trips)
@app.route("/yelp-search")
def yelpRestaurantSearch():
"""Search Yelp for restaurants along path using bounding boxes"""
term = request.args.get("term")
search_type = request.args.get("type")
city = request.args.get("city")
# print "city: ", city
params = {"term": term, "type": search_type}
# print "Params", params
resp = yelping.client.search(city, **params)
# import pdb
# pdb.set_trace()
yelp_link = resp.businesses[0].url
rating_graphic = resp.businesses[0].rating_img_url
yelp_name = resp.businesses[0].name
return jsonify([yelp_link, rating_graphic, yelp_name])
@app.route('/d3map')
def displayD3Map():
"""Displays interactive D3 map"""
return render_template("d3map.html")
if __name__ == "__main__":
# Code below only runs if server file is opened directly
#Turn this off after debugging
# app.debug = True
connect_to_db(app)
DebugToolbarExtension(app)
app.run(host="0.0.0.0")