Cleaned unused functions and files
This commit is contained in:
7 files changed
+125
-195
No files matched your search
+50
-113
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user