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:
2025-10-20 18:11:16 -04:00
parent a775270790
commit f97808c7b3
41 changed files with 1147 additions and 1715 deletions

View File

@@ -0,0 +1,84 @@
from typing import Dict, List, Any
from .base import get_db
class ARFieldModel:
"""Model for custom AR field operations"""
@staticmethod
def get_all(tenant_id: str) -> List[Dict[str, Any]]:
"""Get all custom AR fields for a tenant"""
tenant_id = tenant_id.lower()
with get_db() as conn:
cursor = conn.cursor()
cursor.execute('''
SELECT id, field_name, label, field_type, editable, display_order
FROM custom_ar_fields
WHERE tenant_id = ?
ORDER BY display_order, id
''', (tenant_id,))
fields = []
for row in cursor.fetchall():
fields.append({
'id': row['id'],
'fieldName': row['field_name'],
'label': row['label'],
'fieldType': row['field_type'],
'editable': row['editable'],
'displayOrder': row['display_order']
})
return fields
@staticmethod
def save(tenant_id: str, field_data: Dict[str, Any]):
"""Save or update a custom AR field"""
tenant_id = tenant_id.lower()
with get_db() as conn:
cursor = conn.cursor()
if 'id' in field_data and field_data['id']:
# Update existing field
cursor.execute('''
UPDATE custom_ar_fields
SET field_name = ?, label = ?, field_type = ?, editable = ?,
display_order = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ? AND tenant_id = ?
''', (
field_data['fieldName'],
field_data['label'],
field_data['fieldType'],
field_data['editable'],
field_data['displayOrder'],
field_data['id'],
tenant_id
))
else:
# Insert new field
cursor.execute('''
INSERT INTO custom_ar_fields
(tenant_id, field_name, label, field_type, editable, display_order)
VALUES (?, ?, ?, ?, ?, ?)
''', (
tenant_id,
field_data['fieldName'],
field_data['label'],
field_data['fieldType'],
field_data['editable'],
field_data['displayOrder']
))
conn.commit()
@staticmethod
def delete(tenant_id: str, field_id: int):
"""Delete a custom AR field"""
tenant_id = tenant_id.lower()
with get_db() as conn:
cursor = conn.cursor()
cursor.execute('DELETE FROM custom_ar_fields WHERE id = ? AND tenant_id = ?',
(field_id, tenant_id))
conn.commit()