Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	fixes
Browse files- .gitignore +3 -0
- app.py +96 -51
    	
        .gitignore
    ADDED
    
    | @@ -0,0 +1,3 @@ | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            .venv
         | 
| 2 | 
            +
            users_data.json
         | 
| 3 | 
            +
            .vscode
         | 
    	
        app.py
    CHANGED
    
    | @@ -18,8 +18,8 @@ load_dotenv() | |
| 18 | 
             
            app = Flask(__name__)
         | 
| 19 | 
             
            app.secret_key = 'spiritual-journey-finder-2024'
         | 
| 20 |  | 
| 21 | 
            -
            # File to store user data
         | 
| 22 | 
            -
            USERS_FILE = os.getenv("USERS_FILE", " | 
| 23 |  | 
| 24 | 
             
            # Together API for chatbot
         | 
| 25 | 
             
            TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY")
         | 
| @@ -137,15 +137,37 @@ RELIGIONS = { | |
| 137 |  | 
| 138 | 
             
            def load_users():
         | 
| 139 | 
             
                """Load users from JSON file"""
         | 
| 140 | 
            -
                 | 
| 141 | 
            -
                     | 
| 142 | 
            -
                         | 
|  | |
|  | |
|  | |
| 143 | 
             
                return {}
         | 
| 144 |  | 
| 145 | 
             
            def save_users(users):
         | 
| 146 | 
             
                """Save users to JSON file"""
         | 
| 147 | 
            -
                 | 
| 148 | 
            -
                     | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 149 |  | 
| 150 | 
             
            def calculate_results(answers):
         | 
| 151 | 
             
                """Calculate which spiritual paths align with user's answers"""
         | 
| @@ -195,58 +217,78 @@ def home(): | |
| 195 | 
             
            @app.route("/login", methods=["GET", "POST"])
         | 
| 196 | 
             
            def login():
         | 
| 197 | 
             
                if request.method == "POST":
         | 
| 198 | 
            -
                     | 
| 199 | 
            -
             | 
| 200 | 
            -
             | 
| 201 | 
            -
             | 
| 202 | 
            -
             | 
| 203 | 
            -
             | 
| 204 | 
            -
             | 
| 205 | 
            -
                         | 
| 206 | 
            -
             | 
| 207 | 
            -
             | 
| 208 | 
            -
                         | 
| 209 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 210 | 
             
                                session['username'] = username
         | 
| 211 | 
             
                                return jsonify({"success": True})
         | 
| 212 | 
            -
                         | 
| 213 | 
            -
             | 
| 214 | 
            -
                    
         | 
| 215 | 
            -
                         | 
| 216 | 
            -
                         | 
| 217 | 
            -
                            users[username]['password'] = generate_password_hash(password)
         | 
| 218 | 
            -
                            save_users(users)
         | 
| 219 | 
            -
                            session['username'] = username
         | 
| 220 | 
            -
                            return jsonify({"success": True})
         | 
| 221 | 
            -
                    
         | 
| 222 | 
            -
                    return jsonify({"success": False, "message": "Invalid credentials!"})
         | 
| 223 |  | 
| 224 | 
             
                return render_template("index.html", logged_in=False, is_signup=False)
         | 
| 225 |  | 
| 226 | 
             
            @app.route("/signup", methods=["GET", "POST"])
         | 
| 227 | 
             
            def signup():
         | 
| 228 | 
             
                if request.method == "POST":
         | 
| 229 | 
            -
                     | 
| 230 | 
            -
             | 
| 231 | 
            -
             | 
| 232 | 
            -
             | 
| 233 | 
            -
             | 
| 234 | 
            -
             | 
| 235 | 
            -
             | 
| 236 | 
            -
                         | 
| 237 | 
            -
             | 
| 238 | 
            -
             | 
| 239 | 
            -
                         | 
| 240 | 
            -
             | 
| 241 | 
            -
             | 
| 242 | 
            -
             | 
| 243 | 
            -
             | 
| 244 | 
            -
                         | 
| 245 | 
            -
                         | 
| 246 | 
            -
             | 
| 247 | 
            -
             | 
| 248 | 
            -
             | 
| 249 | 
            -
             | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 250 |  | 
| 251 | 
             
                return render_template("index.html", logged_in=False, is_signup=True)
         | 
| 252 |  | 
| @@ -357,5 +399,8 @@ Rules: Keep 30-50 words, be respectful, use * for bullet points (format: "Text: | |
| 357 | 
             
                        "message": f"Chat error: {str(e)}"
         | 
| 358 | 
             
                    })
         | 
| 359 |  | 
|  | |
|  | |
|  | |
| 360 | 
             
            if __name__ == "__main__":
         | 
| 361 | 
             
                app.run(debug=True, port=5001)
         | 
|  | |
| 18 | 
             
            app = Flask(__name__)
         | 
| 19 | 
             
            app.secret_key = 'spiritual-journey-finder-2024'
         | 
| 20 |  | 
| 21 | 
            +
            # File to store user data - defaults to current directory (writable in Docker)
         | 
| 22 | 
            +
            USERS_FILE = os.getenv("USERS_FILE", "users_data.json")
         | 
| 23 |  | 
| 24 | 
             
            # Together API for chatbot
         | 
| 25 | 
             
            TOGETHER_API_KEY = os.getenv("TOGETHER_API_KEY")
         | 
|  | |
| 137 |  | 
| 138 | 
             
            def load_users():
         | 
| 139 | 
             
                """Load users from JSON file"""
         | 
| 140 | 
            +
                try:
         | 
| 141 | 
            +
                    if os.path.exists(USERS_FILE):
         | 
| 142 | 
            +
                        with open(USERS_FILE, 'r') as f:
         | 
| 143 | 
            +
                            return json.load(f)
         | 
| 144 | 
            +
                except Exception as e:
         | 
| 145 | 
            +
                    print(f"Error loading users: {e}")
         | 
| 146 | 
             
                return {}
         | 
| 147 |  | 
| 148 | 
             
            def save_users(users):
         | 
| 149 | 
             
                """Save users to JSON file"""
         | 
| 150 | 
            +
                try:
         | 
| 151 | 
            +
                    # Ensure parent directory exists
         | 
| 152 | 
            +
                    os.makedirs(os.path.dirname(USERS_FILE) if os.path.dirname(USERS_FILE) else '.', exist_ok=True)
         | 
| 153 | 
            +
                    with open(USERS_FILE, 'w') as f:
         | 
| 154 | 
            +
                        json.dump(users, f, indent=2)
         | 
| 155 | 
            +
                    return True
         | 
| 156 | 
            +
                except Exception as e:
         | 
| 157 | 
            +
                    print(f"Error saving users: {e}")
         | 
| 158 | 
            +
                    return False
         | 
| 159 | 
            +
             | 
| 160 | 
            +
            def initialize_default_user():
         | 
| 161 | 
            +
                """Create default test user if no users exist"""
         | 
| 162 | 
            +
                users = load_users()
         | 
| 163 | 
            +
                if not users:  # Only create if no users exist
         | 
| 164 | 
            +
                    users['test'] = {
         | 
| 165 | 
            +
                        'password': generate_password_hash('test'),
         | 
| 166 | 
            +
                        'answers': [],
         | 
| 167 | 
            +
                        'results': []
         | 
| 168 | 
            +
                    }
         | 
| 169 | 
            +
                    save_users(users)
         | 
| 170 | 
            +
                    print("✅ Default test user created (username: test, password: test)")
         | 
| 171 |  | 
| 172 | 
             
            def calculate_results(answers):
         | 
| 173 | 
             
                """Calculate which spiritual paths align with user's answers"""
         | 
|  | |
| 217 | 
             
            @app.route("/login", methods=["GET", "POST"])
         | 
| 218 | 
             
            def login():
         | 
| 219 | 
             
                if request.method == "POST":
         | 
| 220 | 
            +
                    try:
         | 
| 221 | 
            +
                        data = request.get_json()
         | 
| 222 | 
            +
                        if not data:
         | 
| 223 | 
            +
                            return jsonify({"success": False, "message": "Invalid request"}), 400
         | 
| 224 | 
            +
                            
         | 
| 225 | 
            +
                        username = data.get('username', '').strip()
         | 
| 226 | 
            +
                        password = data.get('password', '')
         | 
| 227 | 
            +
                        
         | 
| 228 | 
            +
                        if not username or not password:
         | 
| 229 | 
            +
                            return jsonify({"success": False, "message": "Username and password required"}), 400
         | 
| 230 | 
            +
                        
         | 
| 231 | 
            +
                        users = load_users()
         | 
| 232 | 
            +
                        if username in users:
         | 
| 233 | 
            +
                            stored = users[username]['password']
         | 
| 234 | 
            +
                        
         | 
| 235 | 
            +
                            # 1) Try hash-based verification (works for any Werkzeug scheme)
         | 
| 236 | 
            +
                            try:
         | 
| 237 | 
            +
                                if check_password_hash(stored, password):
         | 
| 238 | 
            +
                                    session['username'] = username
         | 
| 239 | 
            +
                                    return jsonify({"success": True})
         | 
| 240 | 
            +
                            except Exception:
         | 
| 241 | 
            +
                                pass  # if stored isn't a hash string, we'll try plaintext next
         | 
| 242 | 
            +
                        
         | 
| 243 | 
            +
                            # 2) Legacy plaintext fallback → upgrade to a hash
         | 
| 244 | 
            +
                            if stored == password:
         | 
| 245 | 
            +
                                users[username]['password'] = generate_password_hash(password)
         | 
| 246 | 
            +
                                if not save_users(users):
         | 
| 247 | 
            +
                                    return jsonify({"success": False, "message": "Error saving data"}), 500
         | 
| 248 | 
             
                                session['username'] = username
         | 
| 249 | 
             
                                return jsonify({"success": True})
         | 
| 250 | 
            +
                        
         | 
| 251 | 
            +
                        return jsonify({"success": False, "message": "Invalid credentials"})
         | 
| 252 | 
            +
                    except Exception as e:
         | 
| 253 | 
            +
                        print(f"Login error: {e}")
         | 
| 254 | 
            +
                        return jsonify({"success": False, "message": "Server error"}), 500
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 255 |  | 
| 256 | 
             
                return render_template("index.html", logged_in=False, is_signup=False)
         | 
| 257 |  | 
| 258 | 
             
            @app.route("/signup", methods=["GET", "POST"])
         | 
| 259 | 
             
            def signup():
         | 
| 260 | 
             
                if request.method == "POST":
         | 
| 261 | 
            +
                    try:
         | 
| 262 | 
            +
                        data = request.get_json()
         | 
| 263 | 
            +
                        if not data:
         | 
| 264 | 
            +
                            return jsonify({"success": False, "message": "Invalid request"}), 400
         | 
| 265 | 
            +
                            
         | 
| 266 | 
            +
                        username = data.get('username', '').strip()
         | 
| 267 | 
            +
                        password = data.get('password', '')
         | 
| 268 | 
            +
                        
         | 
| 269 | 
            +
                        if not username or not password:
         | 
| 270 | 
            +
                            return jsonify({"success": False, "message": "Username and password required"}), 400
         | 
| 271 | 
            +
                        
         | 
| 272 | 
            +
                        users = load_users()
         | 
| 273 | 
            +
                        
         | 
| 274 | 
            +
                        if username in users:
         | 
| 275 | 
            +
                            return jsonify({"success": False, "message": "Username already exists"})
         | 
| 276 | 
            +
                        
         | 
| 277 | 
            +
                        # Create new user with hashed password
         | 
| 278 | 
            +
                        users[username] = {
         | 
| 279 | 
            +
                            'password': generate_password_hash(password),
         | 
| 280 | 
            +
                            'answers': [],
         | 
| 281 | 
            +
                            'results': []
         | 
| 282 | 
            +
                        }
         | 
| 283 | 
            +
                        
         | 
| 284 | 
            +
                        if not save_users(users):
         | 
| 285 | 
            +
                            return jsonify({"success": False, "message": "Error saving user data"}), 500
         | 
| 286 | 
            +
                            
         | 
| 287 | 
            +
                        session['username'] = username
         | 
| 288 | 
            +
                        return jsonify({"success": True})
         | 
| 289 | 
            +
                    except Exception as e:
         | 
| 290 | 
            +
                        print(f"Signup error: {e}")
         | 
| 291 | 
            +
                        return jsonify({"success": False, "message": "Server error"}), 500
         | 
| 292 |  | 
| 293 | 
             
                return render_template("index.html", logged_in=False, is_signup=True)
         | 
| 294 |  | 
|  | |
| 399 | 
             
                        "message": f"Chat error: {str(e)}"
         | 
| 400 | 
             
                    })
         | 
| 401 |  | 
| 402 | 
            +
            # Initialize default test user on startup
         | 
| 403 | 
            +
            initialize_default_user()
         | 
| 404 | 
            +
             | 
| 405 | 
             
            if __name__ == "__main__":
         | 
| 406 | 
             
                app.run(debug=True, port=5001)
         | 
