Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -217,71 +217,79 @@ def calculate_results(answers):
|
|
| 217 |
# Routes
|
| 218 |
@app.route("/")
|
| 219 |
def index():
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
@app.route("/get_questions", methods=["GET"])
|
| 223 |
-
def get_questions():
|
| 224 |
-
return jsonify({"questions": QUESTIONS})
|
| 225 |
-
|
| 226 |
-
@app.route("/get_user_data", methods=["GET"])
|
| 227 |
-
def get_user_data():
|
| 228 |
-
if 'username' not in session:
|
| 229 |
-
return jsonify({"success": False, "message": "Not logged in"})
|
| 230 |
-
|
| 231 |
-
users = load_users()
|
| 232 |
-
user_data = users.get(session['username'], {})
|
| 233 |
-
return jsonify({
|
| 234 |
-
"success": True,
|
| 235 |
-
"username": session['username'],
|
| 236 |
-
"recommendations": user_data.get('recommendations', []),
|
| 237 |
-
"answers": user_data.get('answers', [])
|
| 238 |
-
})
|
| 239 |
-
|
| 240 |
-
@app.route("/check_session", methods=["GET"])
|
| 241 |
-
def check_session():
|
| 242 |
if 'username' in session:
|
| 243 |
-
|
| 244 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 245 |
|
| 246 |
-
@app.route("/login", methods=["POST"])
|
| 247 |
def login():
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
-
@app.route("/
|
| 263 |
-
def
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 275 |
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
}
|
| 282 |
-
save_users(users)
|
| 283 |
-
session['username'] = username
|
| 284 |
-
return jsonify({"success": True, "message": "Account created successfully"})
|
| 285 |
|
| 286 |
@app.route("/submit_assessment", methods=["POST"])
|
| 287 |
def submit_assessment():
|
|
@@ -306,6 +314,22 @@ def submit_assessment():
|
|
| 306 |
|
| 307 |
return jsonify({"success": False, "message": "User not found"})
|
| 308 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
@app.route("/logout")
|
| 310 |
def logout():
|
| 311 |
session.pop('username', None)
|
|
|
|
| 217 |
# Routes
|
| 218 |
@app.route("/")
|
| 219 |
def index():
|
| 220 |
+
# Check if user is logged in
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
if 'username' in session:
|
| 222 |
+
users = load_users()
|
| 223 |
+
user_data = users.get(session['username'], {})
|
| 224 |
+
has_results = len(user_data.get('recommendations', [])) > 0
|
| 225 |
+
|
| 226 |
+
return render_template("index.html",
|
| 227 |
+
logged_in=True,
|
| 228 |
+
username=session['username'],
|
| 229 |
+
title="π Your Spiritual Journey" if has_results else "π Spiritual Path Assessment",
|
| 230 |
+
message="Explore your personalized results" if has_results else "Discover your path",
|
| 231 |
+
has_results=has_results,
|
| 232 |
+
questions=QUESTIONS,
|
| 233 |
+
results=user_data.get('recommendations', [])
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
# Not logged in - show login page
|
| 237 |
+
return render_template("index.html",
|
| 238 |
+
logged_in=False,
|
| 239 |
+
is_signup=False
|
| 240 |
+
)
|
| 241 |
|
| 242 |
+
@app.route("/login", methods=["GET", "POST"])
|
| 243 |
def login():
|
| 244 |
+
if request.method == "POST":
|
| 245 |
+
data = request.json
|
| 246 |
+
username = data.get('username', '').strip()
|
| 247 |
+
password = data.get('password', '').strip()
|
| 248 |
+
|
| 249 |
+
if not username or not password:
|
| 250 |
+
return jsonify({"success": False, "message": "Username and password required"})
|
| 251 |
+
|
| 252 |
+
users = load_users()
|
| 253 |
+
if username in users and users[username].get('password') == password:
|
| 254 |
+
session['username'] = username
|
| 255 |
+
return jsonify({"success": True, "message": "Login successful"})
|
| 256 |
+
|
| 257 |
+
return jsonify({"success": False, "message": "Invalid credentials"})
|
| 258 |
|
| 259 |
+
# GET request - show login page
|
| 260 |
+
return render_template("index.html",
|
| 261 |
+
logged_in=False,
|
| 262 |
+
is_signup=False
|
| 263 |
+
)
|
| 264 |
|
| 265 |
+
@app.route("/signup", methods=["GET", "POST"])
|
| 266 |
+
def signup():
|
| 267 |
+
if request.method == "POST":
|
| 268 |
+
data = request.json
|
| 269 |
+
username = data.get('username', '').strip()
|
| 270 |
+
password = data.get('password', '').strip()
|
| 271 |
+
|
| 272 |
+
if not username or not password:
|
| 273 |
+
return jsonify({"success": False, "message": "All fields required"})
|
| 274 |
+
|
| 275 |
+
users = load_users()
|
| 276 |
+
if username in users:
|
| 277 |
+
return jsonify({"success": False, "message": "Username already exists"})
|
| 278 |
+
|
| 279 |
+
users[username] = {
|
| 280 |
+
"password": password,
|
| 281 |
+
"answers": [],
|
| 282 |
+
"recommendations": []
|
| 283 |
+
}
|
| 284 |
+
save_users(users)
|
| 285 |
+
session['username'] = username
|
| 286 |
+
return jsonify({"success": True, "message": "Account created successfully"})
|
| 287 |
|
| 288 |
+
# GET request - show signup page
|
| 289 |
+
return render_template("index.html",
|
| 290 |
+
logged_in=False,
|
| 291 |
+
is_signup=True
|
| 292 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
|
| 294 |
@app.route("/submit_assessment", methods=["POST"])
|
| 295 |
def submit_assessment():
|
|
|
|
| 314 |
|
| 315 |
return jsonify({"success": False, "message": "User not found"})
|
| 316 |
|
| 317 |
+
@app.route("/reset_assessment", methods=["POST"])
|
| 318 |
+
def reset_assessment():
|
| 319 |
+
if 'username' not in session:
|
| 320 |
+
return jsonify({"success": False, "message": "Not logged in"})
|
| 321 |
+
|
| 322 |
+
users = load_users()
|
| 323 |
+
username = session['username']
|
| 324 |
+
|
| 325 |
+
if username in users:
|
| 326 |
+
users[username]['answers'] = []
|
| 327 |
+
users[username]['recommendations'] = []
|
| 328 |
+
save_users(users)
|
| 329 |
+
return jsonify({"success": True})
|
| 330 |
+
|
| 331 |
+
return jsonify({"success": False, "message": "User not found"})
|
| 332 |
+
|
| 333 |
@app.route("/logout")
|
| 334 |
def logout():
|
| 335 |
session.pop('username', None)
|