Refactor models to implement ValidatableMixin; add validation logic for state management in Area, Brand, Item, Room, and RoomFunction classes
This commit is contained in:
parent
8de21bae9c
commit
d7e38cb8da
9 changed files with 196 additions and 9 deletions
49
routes.py
49
routes.py
|
@ -4,7 +4,7 @@ from .models import Brand, Item, Inventory, RoomFunction, User, WorkLog, Room, A
|
|||
from sqlalchemy import or_, delete
|
||||
from sqlalchemy.orm import aliased
|
||||
from . import db
|
||||
from .utils import eager_load_user_relationships, eager_load_inventory_relationships, eager_load_room_relationships, eager_load_worklog_relationships, chunk_list, add_named_entities
|
||||
from .utils.load import eager_load_user_relationships, eager_load_inventory_relationships, eager_load_room_relationships, eager_load_worklog_relationships, chunk_list, add_named_entities
|
||||
import pandas as pd
|
||||
import traceback
|
||||
import json
|
||||
|
@ -522,3 +522,50 @@ def settings():
|
|||
endpoint='settings'
|
||||
)
|
||||
|
||||
@main.route('/api/settings', methods=['POST'])
|
||||
def api_settings():
|
||||
try:
|
||||
payload = request.get_json()
|
||||
if not payload:
|
||||
return {'error': 'No JSON payload'}, 400
|
||||
|
||||
for label, entries in [
|
||||
('brands', payload.get('brands', [])),
|
||||
('types', payload.get('types', [])),
|
||||
('sections', payload.get('sections', [])),
|
||||
('functions', payload.get('functions', [])),
|
||||
('rooms', payload.get('rooms', [])),
|
||||
]:
|
||||
if not isinstance(entries, list):
|
||||
return {'error': f"{label} must be a list"}, 400
|
||||
for entry in entries:
|
||||
if not isinstance(entry, dict):
|
||||
return {'error': f'Each {label} entry must be a dict'}, 400
|
||||
|
||||
errors = []
|
||||
errors += Brand.validate_state(payload.get("brands", []))
|
||||
errors += Item.validate_state(payload.get("types", []))
|
||||
errors += Area.validate_state(payload.get("sections", []))
|
||||
errors += RoomFunction.validate_state(payload.get("functions", []))
|
||||
errors += Room.validate_state(payload.get("rooms", []))
|
||||
|
||||
if errors:
|
||||
return {'status': 'error', 'errors': errors}, 400
|
||||
|
||||
with db.session.begin():
|
||||
brand_map = Brand.sync_from_state(payload.get('brands', []))
|
||||
type_map = Item.sync_from_state(payload.get('types', []))
|
||||
section_map = Area.sync_from_state(payload.get('sections', []))
|
||||
function_map = RoomFunction.sync_from_state(payload.get('functions', []))
|
||||
|
||||
Room.sync_from_state(
|
||||
submitted_rooms=payload.get('rooms', []),
|
||||
section_map=section_map,
|
||||
function_map=function_map
|
||||
)
|
||||
|
||||
return {'status': 'ok'}, 200
|
||||
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
return {'status': 'error', 'message': str(e)}, 500
|
Loading…
Add table
Add a link
Reference in a new issue