From ea14d895842cb0b56405d3e427282da4f139f838 Mon Sep 17 00:00:00 2001 From: Josh-Dev-Quest Date: Sun, 29 Mar 2026 11:00:12 +0200 Subject: [PATCH] Cleaned unused functions and files --- backend/app.py | 163 +++++++++-------------------- backend/data/journeys.json | 69 ++++++++++++ backend/data/users.json | 6 ++ backend/data/users/1/journeys.json | 42 -------- backend/data/users/1/posts.json | 38 ------- backend/data/users/2/journeys.json | 1 - backend/data/users/2/posts.json | 1 - 7 files changed, 125 insertions(+), 195 deletions(-) delete mode 100644 backend/data/users/1/journeys.json delete mode 100644 backend/data/users/1/posts.json delete mode 100644 backend/data/users/2/journeys.json delete mode 100644 backend/data/users/2/posts.json diff --git a/backend/app.py b/backend/app.py index 9aeb9cd..a0af988 100644 --- a/backend/app.py +++ b/backend/app.py @@ -13,10 +13,20 @@ CORS(app, supports_credentials=True) # allow cookies # Directories DATA_DIR = "data" USERS_FILE = os.path.join(DATA_DIR, "users.json") +JOURNEYS_FILE = os.path.join(DATA_DIR, 'journeys.json') os.makedirs(DATA_DIR, exist_ok=True) + # ==================== User helpers ==================== +def require_login(): + if "user_id" not in session: + return False + return True + +def get_current_user_id(): + return session.get("user_id") + def load_users(): try: if os.path.exists(USERS_FILE): @@ -49,99 +59,7 @@ def get_user_by_username(username): def get_user_by_id(user_id): users = load_users() return next((u for u in users if u["id"] == user_id), None) - -# Central journeys storage -JOURNEYS_FILE = os.path.join(DATA_DIR, 'journeys.json') - -def load_all_journeys(): - try: - if os.path.exists(JOURNEYS_FILE): - with open(JOURNEYS_FILE, 'r') as f: - return json.load(f) - except Exception as e: - print(f"Error loading journeys: {e}") - return [] - -def save_all_journeys(journeys): - try: - with open(JOURNEYS_FILE, 'w') as f: - json.dump(journeys, f, indent=2) - except Exception as e: - print(f"Error saving journeys: {e}") - -def get_next_journey_id(journeys): - if not journeys: - return 1 - return max(j['id'] for j in journeys) + 1 - -def get_journey_by_id(journey_id): - journeys = load_all_journeys() - return next((j for j in journeys if j['id'] == journey_id), None) - -def user_can_view_journey(journey, user_id): - if journey['owner_id'] == user_id: - return True - if journey.get('visibility') == 'public': - return True - if journey.get('visibility') == 'shared' and user_id in journey.get('shared_read', []): - return True - return False - -def user_can_edit_journey(journey, user_id): - if journey['owner_id'] == user_id: - return True - if journey.get('visibility') == 'shared' and user_id in journey.get('shared_edit', []): - return True - return False - - -# ==================== Per‑user data helpers ==================== -def get_user_data_dir(user_id): - user_dir = os.path.join(DATA_DIR, "users", str(user_id)) - os.makedirs(user_dir, exist_ok=True) - return user_dir - - -def load_user_journeys(user_id): - file_path = os.path.join(get_user_data_dir(user_id), "journeys.json") - try: - if os.path.exists(file_path): - with open(file_path, "r") as f: - return json.load(f) - except Exception as e: - print(f"Error loading journeys for user {user_id}: {e}") - return [] - - -def save_user_journeys(user_id, journeys): - file_path = os.path.join(get_user_data_dir(user_id), "journeys.json") - try: - with open(file_path, "w") as f: - json.dump(journeys, f, indent=2) - except Exception as e: - print(f"Error saving journeys for user {user_id}: {e}") - - -def load_user_posts(user_id): - file_path = os.path.join(get_user_data_dir(user_id), "posts.json") - try: - if os.path.exists(file_path): - with open(file_path, "r") as f: - return json.load(f) - except Exception as e: - print(f"Error loading posts for user {user_id}: {e}") - return [] - - -def save_user_posts(user_id, posts): - file_path = os.path.join(get_user_data_dir(user_id), "posts.json") - try: - with open(file_path, "w") as f: - json.dump(posts, f, indent=2) - except Exception as e: - print(f"Error saving posts for user {user_id}: {e}") - - + # ==================== Authentication endpoints ==================== @app.route("/api/register", methods=["POST"]) def register(): @@ -168,11 +86,7 @@ def register(): } users.append(new_user) save_users(users) - - # Create empty data files for the new user - save_user_journeys(new_id, []) - save_user_posts(new_id, []) - + # Log the user in automatically session["user_id"] = new_id @@ -215,23 +129,49 @@ def me(): return jsonify({"error": "User not found"}), 401 return jsonify({"id": user["id"], "username": user["username"]}) - # ==================== Journey helper functions ==================== -def require_login(): - if "user_id" not in session: - return False - return True +def load_all_journeys(): + try: + if os.path.exists(JOURNEYS_FILE): + with open(JOURNEYS_FILE, 'r') as f: + return json.load(f) + except Exception as e: + print(f"Error loading journeys: {e}") + return [] -def get_current_user_id(): - return session.get("user_id") +def save_all_journeys(journeys): + try: + with open(JOURNEYS_FILE, 'w') as f: + json.dump(journeys, f, indent=2) + except Exception as e: + print(f"Error saving journeys: {e}") +def get_next_journey_id(journeys): + if not journeys: + return 1 + return max(j['id'] for j in journeys) + 1 + +def get_journey_by_id(journey_id): + journeys = load_all_journeys() + return next((j for j in journeys if j['id'] == journey_id), None) + +def user_can_view_journey(journey, user_id): + if journey['owner_id'] == user_id: + return True + if journey.get('visibility') == 'public': + return True + if journey.get('visibility') == 'shared' and user_id in journey.get('shared_read', []): + return True + return False + +def user_can_edit_journey(journey, user_id): + if journey['owner_id'] == user_id: + return True + if journey.get('visibility') == 'shared' and user_id in journey.get('shared_edit', []): + return True + return False -def get_journeys_for_current_user(): - user_id = get_current_user_id() - if user_id is None: - return None - return load_user_journeys(user_id) # ==================== Journey endpoints ==================== @app.route('/api/journeys', methods=['GET']) @@ -341,9 +281,6 @@ def delete_journey(journey_id): save_all_journeys(journeys) return jsonify({'message': 'Journey deleted successfully', 'journey': journey}) -# ==================== Journey endpoints (already present) ==================== -# (Keep the existing journey endpoints: GET /api/journeys, POST, etc.) - # ==================== Comments (stored inside journeys) ==================== def save_journey(journey): journeys = load_all_journeys() diff --git a/backend/data/journeys.json b/backend/data/journeys.json index 66042b9..9498054 100644 --- a/backend/data/journeys.json +++ b/backend/data/journeys.json @@ -46,5 +46,74 @@ "created_at": "2026-03-28T17:47:56.571469" } ] + }, + { + "id": 2, + "owner_id": 3, + "title": "New Journey", + "description": "test", + "markers": [ + { + "lat": 46.89023157359399, + "lng": 6.789550781250001, + "title": "New Marker", + "date": "", + "description": "", + "videoUrl": "" + }, + { + "lat": 47.05141149430736, + "lng": 10.4205322265625, + "title": "New Marker", + "date": "", + "description": "", + "videoUrl": "" + }, + { + "lat": 46.58906908309185, + "lng": 10.310668945312502, + "title": "New Marker", + "date": "", + "description": "", + "videoUrl": "" + }, + { + "lat": 46.05417324177818, + "lng": 5.009765625000001, + "title": "New Marker", + "date": "", + "description": "", + "videoUrl": "" + }, + { + "lat": 45.537136680398596, + "lng": 7.448730468750001, + "title": "New Marker", + "date": "", + "description": "", + "videoUrl": "" + }, + { + "lat": 45.54867850352087, + "lng": 10.678710937500002, + "title": "New Marker", + "date": "", + "description": "", + "videoUrl": "" + } + ], + "created_at": "2026-03-29T10:54:18.332527", + "visibility": "public", + "shared_read": [], + "shared_edit": [], + "comments": [ + { + "id": 1774774657963, + "author_id": 1, + "author_name": "josh", + "text": "test", + "created_at": "2026-03-29T10:57:37.963970" + } + ] } ] \ No newline at end of file diff --git a/backend/data/users.json b/backend/data/users.json index 8932acf..6a6fea8 100644 --- a/backend/data/users.json +++ b/backend/data/users.json @@ -10,5 +10,11 @@ "username": "test1", "password_hash": "scrypt:32768:8:1$hPfITQadZq8438bv$38262bf82d93c596a82a1b052a4ba72f8d6729b796ca5273faa7dd47b409112959c4501e77922605a1f3a7ef08e68fa545ce03818eb82e6fb2503cc817c43e2a", "created_at": "2026-03-28T14:13:32.860143" + }, + { + "id": 3, + "username": "test2", + "password_hash": "scrypt:32768:8:1$iZJWgiHFhaN845sv$553a61855a32752aaca7f5d9ac200f99f89d215250e561fb3bb52c46b8d8bd96d09a915969c00bd8cfbded78b70740852671dfb84c659203c92982e2708a10f2", + "created_at": "2026-03-29T10:53:39.433798" } ] \ No newline at end of file diff --git a/backend/data/users/1/journeys.json b/backend/data/users/1/journeys.json deleted file mode 100644 index e67038d..0000000 --- a/backend/data/users/1/journeys.json +++ /dev/null @@ -1,42 +0,0 @@ -[ - { - "id": 1, - "title": "test", - "description": "11", - "markers": [ - { - "lat": 48.356249029540734, - "lng": 4.866943359375, - "title": "New Marker", - "date": "", - "description": "", - "videoUrl": "" - }, - { - "lat": 46.961510504873104, - "lng": 9.371337890625002, - "title": "New Marker", - "date": "", - "description": "", - "videoUrl": "" - }, - { - "lat": 45.51404592560427, - "lng": 11.656494140625002, - "title": "New Marker", - "date": "", - "description": "", - "videoUrl": "" - }, - { - "lat": 43.52465500687188, - "lng": 11.162109375, - "title": "New Marker", - "date": "", - "description": "", - "videoUrl": "" - } - ], - "created_at": "2026-03-27T21:49:26.885353" - } -] \ No newline at end of file diff --git a/backend/data/users/1/posts.json b/backend/data/users/1/posts.json deleted file mode 100644 index 98e13bd..0000000 --- a/backend/data/users/1/posts.json +++ /dev/null @@ -1,38 +0,0 @@ -[ - { - "id": 1, - "title": "test", - "content": "qwef", - "journeyId": null, - "image": null, - "author_id": 1, - "created_at": "2026-03-28T15:47:04.343616", - "visibility": "private", - "shared_read": [], - "shared_edit": [] - }, - { - "id": 2, - "title": "another test post", - "content": "sgadsfg", - "journeyId": null, - "image": null, - "author_id": 1, - "created_at": "2026-03-28T16:08:36.820019", - "visibility": "private", - "shared_read": [], - "shared_edit": [] - }, - { - "id": 3, - "title": "another post", - "content": "sfadfas", - "journeyId": null, - "image": null, - "author_id": 1, - "created_at": "2026-03-28T16:08:52.889611", - "visibility": "private", - "shared_read": [], - "shared_edit": [] - } -] \ No newline at end of file diff --git a/backend/data/users/2/journeys.json b/backend/data/users/2/journeys.json deleted file mode 100644 index 0637a08..0000000 --- a/backend/data/users/2/journeys.json +++ /dev/null @@ -1 +0,0 @@ -[] \ No newline at end of file diff --git a/backend/data/users/2/posts.json b/backend/data/users/2/posts.json deleted file mode 100644 index 0637a08..0000000 --- a/backend/data/users/2/posts.json +++ /dev/null @@ -1 +0,0 @@ -[] \ No newline at end of file