Refactor application to MVC architecture with Flask blueprints
Major architectural refactoring to improve code organization, maintainability, and scalability: - Implement MVC pattern with clear separation of concerns - Organize code into blueprints for different functional areas (main, tenant, admin, api) - Create models layer to encapsulate all database operations - Create services layer for business logic (auth, product, barcode) - Implement application factory pattern for better testability - Add environment-based configuration management - Remove old monolithic app.py and consolidate routes New structure: - app/models/ - Database operations (Tenant, Product, ARField, Settings) - app/services/ - Business logic (Auth, Product, Barcode services) - app/blueprints/ - Controllers organized by function - app/config.py - Environment configurations - run.py - Application entry point All routes remain backward compatible. No changes to API or templates functionality.
This commit is contained in:
49
src/app/__init__.py
Normal file
49
src/app/__init__.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import os
|
||||
from flask import Flask, redirect
|
||||
from werkzeug.routing import BaseConverter
|
||||
|
||||
def create_app(config_name='development'):
|
||||
"""Application factory pattern"""
|
||||
app = Flask(__name__)
|
||||
|
||||
# Load configuration
|
||||
from app.config import config
|
||||
app.config.from_object(config[config_name])
|
||||
|
||||
# Ensure data folder exists
|
||||
os.makedirs(app.config['DATA_FOLDER'], exist_ok=True)
|
||||
|
||||
# Custom converter for tenant IDs
|
||||
class TenantConverter(BaseConverter):
|
||||
regex = '[a-zA-Z0-9_-]+'
|
||||
|
||||
def to_python(self, value):
|
||||
# Convert to lowercase when parsing from URL
|
||||
return value.lower()
|
||||
|
||||
def to_url(self, value):
|
||||
# Convert to lowercase when generating URLs
|
||||
return value.lower()
|
||||
|
||||
app.url_map.converters['tenant'] = TenantConverter
|
||||
|
||||
# Initialize database
|
||||
with app.app_context():
|
||||
from app.models.base import init_database
|
||||
init_database()
|
||||
|
||||
# Register blueprints
|
||||
from app.blueprints import main_bp, tenant_bp, admin_bp, api_bp
|
||||
|
||||
app.register_blueprint(main_bp)
|
||||
app.register_blueprint(tenant_bp)
|
||||
app.register_blueprint(admin_bp)
|
||||
app.register_blueprint(api_bp)
|
||||
|
||||
# Catch-all route for admin without tenant - redirect to home
|
||||
@app.route('/admin/')
|
||||
@app.route('/admin/<path:path>')
|
||||
def redirect_admin_to_home(path=None):
|
||||
return redirect('/')
|
||||
|
||||
return app
|
||||
Reference in New Issue
Block a user