-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
205 lines (173 loc) · 5.55 KB
/
app.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from flask import Flask, request, session, redirect, url_for, g
from dataclasses import dataclass
import random
import string
from typing import Dict, List
import interface
from requests import get
app = Flask(__name__, static_folder='client/build')
# Storing this secret key in code is unsafe, but fine for now.
app.secret_key = "d9be77765b2f16a443d918134adff8e54b8f9f9d0c82cf25f2c3817ba5ae7251"
storage = interface.InMemoryStorage()
storage.create_user("alex", "password")
storage.create_user("felix", "password")
storage.create_room("default room", "alex")
storage.join_room("default room", "felix")
# { "felix": [
# {
# "swipe": "yes",
# "food": "Indian"
# }
# ],
# "alex": [
# {
# "swipe": "no",
# "food": "Indian"
# }
# ]
# }
#
# actions = {
# "action1": SwipeAction(
# user='user1',
# room='1234',
# swipe=True,
# food="Mexican"
# )
# }
# @app.route('/api/')
# https://stackoverflow.com/questions/2257441/random-string-generation-with-upper-case-letters-and-digits
def generate_room_code():
return ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(5))
def valid_username_and_password(username, password, passwordConfirmation):
if len(username) == 0:
return "Username must be non-empty"
elif len(password) == 0:
return "Password must be non-empty"
elif password != passwordConfirmation:
return "Passwords do not match"
else:
return None
# def valid_
@app.get('/api/')
def hello_world():
username = session.get('username', None)
print(username)
if username is not None:
return 'Hello, ' + username
else:
return 'Hello, stranger! You should log in'
# create a user session
@app.post('/api/login/')
def login():
# Form data: say=Hi&to=Mom
username = request.json['username']
if storage.valid_login(username, request.json['password']):
session['username'] = username
else:
return {"error": "Invalid username and password."}, 401
print(request.data)
return {"data": "You are logged in."}
# end a user session
@app.delete('/api/logout/')
def logout():
# remove the username from the session if it's there
session.pop('username', None)
return {}, 200
# create a user
@app.post('/api/user')
def create_user():
username = request.json['username']
password = request.json['password']
passwordConfirmation = request.json['password_confirmation']
error_message = valid_username_and_password(username, password, passwordConfirmation)
# storage.user_exists(username) checks if the user already exists in storage
if storage.user_exists(username):
return {"error": "Username unavailable"}, 400
if None == error_message:
storage.create_user(username, password)
session['username'] = username
print(f" *****SESSION**** #{session}")
return {}, 200
else:
return {"error": error_message}, 400
# get user session
@app.get('/api/me')
def get_session():
# print(f" *****SESSION**** #{session}")
if 'username' not in session:
return {"error": "Unauthorized"}, 401
username=session['username']
return {"username": username}, 200
# not authorized
# create a room
@app.post('/api/room')
def create_room():
username = session["username"]
code = generate_room_code()
storage.create_room(code, username)
return {"code": code}
# join a room
@app.post('/api/room/join')
def join_room():
username = session["username"]
code = request.json['code']
if storage.room_exists(code):
storage.join_room(code, username)
return {"response": "Welcome to your room!"}, 200
return {"error": "Room not found!"}, 404
# start a room
@app.post('/api/room/start')
def start_room():
code = request.json['code']
print(code)
return storage.start_room(code)
# exit a room
# @app.post('/api/room/exit')
# record an action
@app.post('/api/room/swipe')
def record_action():
username = session["username"]
code = request.json['code']
action = request.json['action']
food = request.json['food']
if action not in { "yes", "no" }:
return { "error": "invalid action" }, 400
error = storage.record_action(username, action, food, code)
if error != None:
return { "error": error }, 400
return { "message": "action recorded" }, 200
#find a match between users
@app.post('/api/room/match')
def find_match():
code = request.json['code']
return storage.find_match(code)
@app.get('/api/cuisine/next')
def get_next_cuisine():
code = request.form['code']
username = session["username"]
selectedCuisine = storage.get_next_cuisine(code, username)
return { 'cuisine': selectedCuisine.cuisine, 'imageUrl': selectedCuisine.imageUrl }, 200
def proxy(host, path):
response = get(f"{host}{path}")
excluded_headers = [
"content-encoding",
"content-length",
"transfer-encoding",
"connection",
]
headers = {
name: value
for name, value in response.raw.headers.items()
if name.lower() not in excluded_headers
}
return (response.content, response.status_code, headers)
# Based on:
# - https://stackoverflow.com/questions/14023864/flask-url-route-route-all-other-urls-to-some-function
@app.route("/")
@app.route("/<path:rest>")
def fallback(rest = None):
if rest is not None and rest.startswith("/api/"):
return {}, 404
WEBPACK_DEV_SERVER_HOST = "http://localhost:5050"
return proxy(WEBPACK_DEV_SERVER_HOST, request.path)