.txt 7.96 KB •240 lines • Formatting may be inconsistent from source # app/routes.py - Add this new file for frontend routes from flask import Blueprint, render_template, request, redirect, url_for, flash, session, jsonify from flask_jwt_extended import get_jwt_identity, jwt_required, verify_jwt_in_request import requests import os # Create blueprint for frontend routes frontend_bp = Blueprint('frontend', __name__) def get_api_base_url(): """Get the base URL for API calls""" return request.url_root + 'api' @frontend_bp.route('/') def index(): """Landing page""" return render_template('index.html') @frontend_bp.route('/login') def login(): """Login page""" return render_template('auth/login.html') @frontend_bp.route('/register') def register(): """Registration page""" return render_template('auth/register.html') @frontend_bp.route('/logout') def logout(): """Logout - clear session and redirect""" session.clear() flash('You have been logged out successfully.', 'info') return redirect(url_for('frontend.index')) @frontend_bp.route('/dashboard') def dashboard(): """User dashboard - route to appropriate dashboard based on role""" # In a real app, you'd check the user's role from the JWT token # For now, we'll default to worker dashboard # You can add logic here to determine user role and route accordingly return render_template('dashboard/worker.html') @frontend_bp.route('/dashboard/contractor') def contractor_dashboard(): """Contractor dashboard""" return render_template('dashboard/contractor.html') @frontend_bp.route('/dashboard/worker') def worker_dashboard(): """Worker dashboard""" return render_template('dashboard/worker.html') @frontend_bp.route('/jobs') def browse_jobs(): """Browse all jobs page""" return render_template('jobs/browse.html') @frontend_bp.route('/jobs/') def job_details(job_id): """Job details page""" return render_template('jobs/details.html') @frontend_bp.route('/post-job') def post_job(): """Job creation page (contractors only)""" return render_template('jobs/create.html') @frontend_bp.route('/my-jobs') def my_jobs(): """Contractor's posted jobs""" return render_template('jobs/my_jobs.html') @frontend_bp.route('/my-applications') def my_applications(): """Worker's job applications""" return render_template('jobs/my_applications.html') @frontend_bp.route('/profile') def profile(): """User profile management""" return render_template('profile/manage.html') @frontend_bp.route('/categories') def categories(): """Job categories page""" return render_template('jobs/categories.html') # Error handlers @frontend_bp.errorhandler(404) def not_found(error): """404 error page""" return render_template('errors/404.html'), 404 @frontend_bp.errorhandler(500) def server_error(error): """500 error page""" return render_template('errors/500.html'), 500 # Helper route for AJAX calls (optional) @frontend_bp.route('/api-proxy/') def api_proxy(endpoint): """Proxy API calls to avoid CORS issues in development""" api_url = f"{get_api_base_url()}/{endpoint}" if request.method == 'GET': response = requests.get(api_url, params=request.args) elif request.method == 'POST': response = requests.post(api_url, json=request.json) else: return jsonify({'error': 'Method not allowed'}), 405 return response.json(), response.status_code # Update your app/__init__.py to register the frontend blueprint: # Add this to your app/__init__.py after registering other blueprints: def create_app(config_name='default'): app = Flask(__name__) app.config.from_object(config[config_name]) # Initialize extensions db.init_app(app) jwt.init_app(app) migrate.init_app(app, db) cors.init_app(app) # Register API blueprints (existing) from .blueprints.auth import auth_bp from .blueprints.marketplace import marketplace_bp from .blueprints.legal import legal_bp from .blueprints.safety import safety_bp from .blueprints.gamification import gamification_bp app.register_blueprint(auth_bp, url_prefix='/api/auth') app.register_blueprint(marketplace_bp, url_prefix='/api/marketplace') app.register_blueprint(legal_bp, url_prefix='/api/legal') app.register_blueprint(safety_bp, url_prefix='/api/safety') app.register_blueprint(gamification_bp, url_prefix='/api/gamification') # Register frontend blueprint (NEW) from .routes import frontend_bp app.register_blueprint(frontend_bp) # Health check endpoint @app.route('/api/health') def health_check(): return jsonify({ 'service': 'RateRight - Australian Construction Marketplace', 'status': 'fully_operational', 'version': '1.0.0', 'compliance': 'Australian WHS Act 2011, Fair Work Act, Tax Law', 'api_endpoints': { 'authentication': '/api/auth/*', 'marketplace': '/api/marketplace/*', 'legal_compliance': '/api/legal/*', 'safety_system': '/api/safety/*', 'gamification': '/api/gamification/*', 'system_health': '/api/health' } }) return app # Create app/templates/errors/404.html and 500.html for error handling: # app/templates/errors/404.html """ {% extends "base.html" %} {% block title %}Page Not Found - RateRight{% endblock %} {% block content %}

404

Page Not Found

The page you're looking for doesn't exist or has been moved.

{% endblock %} """ # app/templates/errors/500.html """ {% extends "base.html" %} {% block title %}Server Error - RateRight{% endblock %} {% block content %}

500

Server Error

Something went wrong on our end. Our team has been notified.

{% endblock %} """ # Static files structure you'll need: # app/static/css/ - for custom CSS (optional, Bootstrap handles most styling) # app/static/js/ - for custom JavaScript (optional, most is inline) # app/static/images/ - for logos, construction images, etc. # Quick start command to test your frontend: # 1. Add the routes.py file to your app/ directory # 2. Update your app/__init__.py to register the frontend blueprint # 3. Create the error template files # 4. Restart your Flask server: python run.py # 5. Visit http://localhost:5000 to see your construction-themed marketplace!