This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
185 lines (121 loc) · 4.54 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
# https://towardsdatascience.com/build-deploy-a-react-flask-app-47a89a5d17d9
import os
from flask import Flask, redirect, send_from_directory, jsonify, request
from flask_restful import Api
from flask_cors import CORS
from api.spaces import SpacesApi
from api.reviews import ReviewsApi
from api.reports import ReportsApi
import database as db
import auth
# ----------------------------------------------------------------------
app = Flask(__name__, static_url_path="", static_folder="frontend/build")
app.secret_key = os.environ["APP_SECRET_KEY"]
if "FLASK_ENV" in os.environ and os.environ.get("FLASK_ENV") == "development":
CORS(app)
api = Api(app)
# ---------------------------------------------
# Routes for index and errors.
# ---------------------------------------------
@app.route("/", defaults={"path": ""})
def serve(path):
return send_from_directory(app.static_folder, "index.html")
@app.errorhandler(404)
def not_found(e):
return send_from_directory(app.static_folder, "index.html")
# ---------------------------------------------
# Routes for spaces.
# ---------------------------------------------
api.add_resource(SpacesApi, "/spaces", "/spaces/<space_id>")
@app.route("/locations")
def locations():
return jsonify({"locations": db.get_locations()})
@app.route("/types")
def types():
return jsonify({"types": db.get_types()})
# ---------------------------------------------
# Routes for reviews.
# ---------------------------------------------
api.add_resource(ReviewsApi, "/reviews", "/reviews/<review_id>")
# ---------------------------------------------
# Routes for reports.
# ---------------------------------------------
api.add_resource(ReportsApi, "/reports", "/reports/<report_id>")
@app.route("/reports")
def reports():
return jsonify(db.get_reports())
# ---------------------------------------------
# Routes for amenities.
# ---------------------------------------------
@app.route("/amenities")
def amenities():
return jsonify({"amenities": db.get_amenities()})
# ---------------------------------------------
# Routes for tags.
# ---------------------------------------------
@app.route("/tags")
def tags():
return jsonify({"tags": db.get_tags()})
# ---------------------------------------------
# Routes for favorites.
# ---------------------------------------------
@app.route("/getfavorite")
def get_is_favorite():
user_id = request.args.get("user_id")
space_id = request.args.get("space_id")
return jsonify({"is_favorite": db.get_favorite(user_id, space_id)})
@app.route("/postfavorite")
def post_is_favorite():
user_id = request.args.get("user_id")
space_id = request.args.get("space_id")
if not user_id or not space_id:
return jsonify(
{"status": 400, "response": "error: invalid parameters in request"}
)
try:
res = db.post_favorite(user_id, space_id)
return jsonify({"status": 200, "response": res})
except Exception as err:
return jsonify({"status": 500, "response": err})
# ---------------------------------------------
# Routes for Profile page.
# ---------------------------------------------
@app.route("/getfavorites")
def get_list_favorites():
user_id = request.args.get("user_id")
data = db.get_favorites(user_id)
return jsonify(data)
# ---------------------------------------------
# API: for moderation
# ---------------------------------------------
@app.route("/getawaitingapproval")
def get_awaiting_approval():
data = db.get_awaiting_approval()
return jsonify(data)
@app.route("/approve")
def handle_approve():
space_id = request.args.get("space_id")
approval = request.args.get("approval") == "true"
admin = db.check_user_admin()
if admin:
ret = db.handle_approval(space_id, approval)
else:
ret = "user isn't admin, operation disallowed"
return jsonify({"response": ret})
# ---------------------------------------------
# Routes for authentication.
# ---------------------------------------------
@app.route("/logout", methods=["GET"])
def logout():
return auth.logout()
@app.route("/login", methods=["GET"])
def login():
auth.authenticate() # sets session variable
return redirect("/loginredirect")
@app.route("/user_logged_in", methods=["GET"])
def user_logged_in():
username = auth.authenticate()
return jsonify({"netid": username, "admin": db.check_user_admin()})
# ----------------------------------------------------------------------
if __name__ == "__main__":
app.run(host="0.0.0.0", port=int(os.environ.get("PORT", 8000)))