-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
190 lines (139 loc) · 5.69 KB
/
server.py
File metadata and controls
190 lines (139 loc) · 5.69 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"""One minute getaway."""
from jinja2 import StrictUndefined
from flask import Flask, render_template, request, redirect, session, url_for, jsonify, flash
from api import obtain_song_URL, get_song_info, get_track_ids, get_song_id_title
from goo_info import obtain_google_api_key
from loginapi import grab_env_variables, requires_auth, callback_handling
from model import connect_to_db
from image_finder import get_option_images
from queries import check_new_user, checkin_user, save_current_getaway, prior_getaways, get_getaway
import json
# imported Flash class above, then create an instance of it below
app = Flask(__name__)
app.secret_key = "JMD"
app.jinja_env.undefined = StrictUndefined
#this will throw an error if a variable is undefined in jinja
@app.route('/')
def index():
"""Homepage"""
return render_template("home.html")
@app.route('/songprocess', methods=["POST"])
def song_process():
"""Looks for song and returns song information"""
user_song = request.form.get("sname")
root = get_song_info(user_song)
track_ids = get_track_ids(root)
if len(track_ids) == 0:
flash("No song found with that name.")
return redirect('/')
elif len(track_ids) > 1:
songid_title = get_song_id_title(root, track_ids)
return render_template("song_prob.html", songs=songid_title)
elif len(track_ids) == 1:
track_id = track_ids[0].get('id')
return redirect(url_for("select_location", track_id=track_id))
@app.route('/select_loc', methods=["GET"])
def select_location():
"""Select location and validate location."""
track_id = request.args.get("track_id")
session['track_id'] = track_id
song_name = request.args.get("song_name")
session['song_name'] = song_name
google_api = obtain_google_api_key()
return render_template("select_loc.html", track_id=track_id,
google_api=google_api, song_name=song_name)
@app.route('/play', methods=["GET"])
def getaway():
"""Plays song and shows picture."""
# track_id = request.args.get("track_id")
track_id = session['track_id']
track_id = int(track_id)
surl = obtain_song_URL(track_id)
lurl = request.args.get("URLphoto")
locname = request.args.get("locname")
song_name = session['song_name']
session['lurl'] = lurl
session['locname'] = locname
session['song_name'] = song_name
return render_template("play.html", surl=surl, lurl=lurl, locname=locname,
track_id=track_id, song_name=song_name)
@app.route('/login', methods=["GET"])
def social_user_login():
"""Provides login using social media."""
env_variables = grab_env_variables()
AUTH0_CLIENT_SECRET = env_variables[0]
AUTH0_CLIENT_ID = env_variables[1]
AUTH0_DOMAIN = env_variables[2]
AUTH0_CALLBACK_URL = env_variables[3]
return render_template("login.html", AUTH0_CLIENT_SECRET=AUTH0_CLIENT_SECRET,
AUTH0_CLIENT_ID=AUTH0_CLIENT_ID,
AUTH0_DOMAIN=AUTH0_DOMAIN,
AUTH0_CALLBACK_URL=AUTH0_CALLBACK_URL)
@app.route('/callback')
def handle_callback():
"""Callback function for social login."""
callback_handling()
return redirect('/dashboard')
@app.route('/dashboard', methods=["GET"])
@requires_auth
def dashboard():
"""User information page once logged in."""
track_id = session.get("track_id")
lurl = session.get("lurl")
locname = session.get("locname")
song_name = session.get("song_name")
user = session["profile"]
user_find = user['user_id'].split("|")
user_id = user_find[1]
user_name = user['name']
check_new_user(user_id, user_name)
return render_template("dashboard.html", user=user, lurl=lurl, locname=locname,
track_id=track_id, song_name=song_name)
@app.route('/save_getaway', methods=["GET"])
@requires_auth
def save_getaway():
user = session.get("profile")
user_find = user['user_id'].split("|")
user_id = user_find[1]
track_id = session.get("track_id")
location_url = session.get("lurl")
location_name = session.get("locname")
song_name = session.get("song_name")
getaway = save_current_getaway(user_id, track_id, location_url, location_name,
song_name)
return render_template("getaway_saved.html")
@app.route('/getaways', methods=["GET"])
@requires_auth
def getaways():
user = session.get("profile")
user_find = user['user_id'].split("|")
user_id = user_find[1]
user_getaways = prior_getaways(user_id)
return render_template("user_getaways.html", user_getaways=user_getaways)
@app.route('/replay', methods=["GET"])
def replay_getaway():
getaway_id = request.args.get("getaway_id")
getaway = get_getaway(getaway_id)
seven_digital_track_id = int(getaway.song.seven_digital_track_id)
surl = obtain_song_URL(seven_digital_track_id)
return render_template("replay_getaway.html", surl=surl, getaway=getaway)
@app.route('/logout')
@requires_auth
def logout():
"""Provides logout and redirect to Homepage."""
session.clear()
return redirect('https://michdcode.auth0.com/v2/logout?returnTo=http://127.0.0.1:5000/')
@app.route('/options', methods=["GET"])
def provide_options():
"""Provides options once getaway has finished."""
images = get_option_images()
lurl = session.get("lurl")
locname = session.get("locname")
song_name = session.get("song_name")
return render_template("options.html", images=images, lurl=lurl, locname=locname,
song_name=song_name)
if __name__ == "__main__":
connect_to_db(app)
app.debug = True
app.run()
#server only runs if executed from terminal, cannot be imported module