Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -49,6 +49,7 @@ c.execute('''CREATE TABLE IF NOT EXISTS user_details
|
|
| 49 |
certification TEXT,
|
| 50 |
activity TEXT,
|
| 51 |
country TEXT,
|
|
|
|
| 52 |
FOREIGN KEY(user_id) REFERENCES users(id))''')
|
| 53 |
|
| 54 |
|
|
@@ -120,7 +121,7 @@ def login():
|
|
| 120 |
user = c.execute("SELECT * FROM users WHERE google_id=?", (google_id,)).fetchone()
|
| 121 |
if user:
|
| 122 |
access_token = create_access_token(identity=user[1], expires_delta=False)
|
| 123 |
-
return jsonify({"access_token": access_token}), 200
|
| 124 |
else:
|
| 125 |
return jsonify({"message": "User not found"}), 404
|
| 126 |
|
|
@@ -131,10 +132,11 @@ def login():
|
|
| 131 |
user = c.execute("SELECT * FROM users WHERE username=?", (username,)).fetchone()
|
| 132 |
if user and check_password_hash(user[2], password):
|
| 133 |
access_token = create_access_token(identity=username, expires_delta=False)
|
| 134 |
-
return jsonify({"access_token": access_token}), 200
|
| 135 |
else:
|
| 136 |
return jsonify({"message": "Invalid username or password"}), 401
|
| 137 |
|
|
|
|
| 138 |
@app.route('/user_details', methods=['POST'])
|
| 139 |
@jwt_required()
|
| 140 |
def add_user_details():
|
|
@@ -159,9 +161,9 @@ def add_user_details():
|
|
| 159 |
user_id = user[0]
|
| 160 |
try:
|
| 161 |
c.execute("INSERT INTO user_details (user_id, first_name, last_name, school_name, bachelors_degree, "
|
| 162 |
-
"masters_degree, certification, activity, country) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
| 163 |
(user_id, first_name, last_name, school_name, bachelors_degree, masters_degree, certification,
|
| 164 |
-
activity, country))
|
| 165 |
conn.commit()
|
| 166 |
return jsonify({"message": "User details added successfully"}), 201
|
| 167 |
except sqlite3.IntegrityError:
|
|
@@ -364,5 +366,24 @@ def select_mentor():
|
|
| 364 |
return jsonify({"message": "Failed to select mentor"}), 500
|
| 365 |
|
| 366 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 367 |
if __name__ == '__main__':
|
| 368 |
app.run(debug=True)
|
|
|
|
| 49 |
certification TEXT,
|
| 50 |
activity TEXT,
|
| 51 |
country TEXT,
|
| 52 |
+
data_filled BOOLEAN DEFAULT FALSE,
|
| 53 |
FOREIGN KEY(user_id) REFERENCES users(id))''')
|
| 54 |
|
| 55 |
|
|
|
|
| 121 |
user = c.execute("SELECT * FROM users WHERE google_id=?", (google_id,)).fetchone()
|
| 122 |
if user:
|
| 123 |
access_token = create_access_token(identity=user[1], expires_delta=False)
|
| 124 |
+
return jsonify({"access_token": access_token, "data_filled": user[2]}), 200
|
| 125 |
else:
|
| 126 |
return jsonify({"message": "User not found"}), 404
|
| 127 |
|
|
|
|
| 132 |
user = c.execute("SELECT * FROM users WHERE username=?", (username,)).fetchone()
|
| 133 |
if user and check_password_hash(user[2], password):
|
| 134 |
access_token = create_access_token(identity=username, expires_delta=False)
|
| 135 |
+
return jsonify({"access_token": access_token, "data_filled": user[2]}), 200
|
| 136 |
else:
|
| 137 |
return jsonify({"message": "Invalid username or password"}), 401
|
| 138 |
|
| 139 |
+
|
| 140 |
@app.route('/user_details', methods=['POST'])
|
| 141 |
@jwt_required()
|
| 142 |
def add_user_details():
|
|
|
|
| 161 |
user_id = user[0]
|
| 162 |
try:
|
| 163 |
c.execute("INSERT INTO user_details (user_id, first_name, last_name, school_name, bachelors_degree, "
|
| 164 |
+
"masters_degree, certification, activity, country, data_filled) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
| 165 |
(user_id, first_name, last_name, school_name, bachelors_degree, masters_degree, certification,
|
| 166 |
+
activity, country, True))
|
| 167 |
conn.commit()
|
| 168 |
return jsonify({"message": "User details added successfully"}), 201
|
| 169 |
except sqlite3.IntegrityError:
|
|
|
|
| 366 |
return jsonify({"message": "Failed to select mentor"}), 500
|
| 367 |
|
| 368 |
|
| 369 |
+
@app.route('/users', methods=['GET'])
|
| 370 |
+
def get_all_users():
|
| 371 |
+
try:
|
| 372 |
+
users = c.execute("SELECT * FROM users").fetchall()
|
| 373 |
+
users_list = [{"id": user[0], "username": user[1]} for user in users]
|
| 374 |
+
return jsonify({"users": users_list}), 200
|
| 375 |
+
except Exception as e:
|
| 376 |
+
return jsonify({"message": f"Failed to retrieve users: {str(e)}"}), 500
|
| 377 |
+
|
| 378 |
+
@app.route('/users', methods=['DELETE'])
|
| 379 |
+
def delete_all_users():
|
| 380 |
+
try:
|
| 381 |
+
c.execute("DELETE FROM users")
|
| 382 |
+
conn.commit()
|
| 383 |
+
return jsonify({"message": "All users deleted successfully"}), 200
|
| 384 |
+
except Exception as e:
|
| 385 |
+
return jsonify({"message": f"Failed to delete users: {str(e)}"}), 500
|
| 386 |
+
|
| 387 |
+
|
| 388 |
if __name__ == '__main__':
|
| 389 |
app.run(debug=True)
|