05/04 Update code to replace Area related to Facility
This commit is contained in:
+15
-16
@@ -14,7 +14,7 @@ import logging
|
||||
from flask import Blueprint, request, g, current_app
|
||||
from app import db
|
||||
from app.models.issue import Issue
|
||||
from app.models.facility import Area
|
||||
from app.models.facility import Facility, Area
|
||||
from app.models.inspection import Inspection
|
||||
from app.api.errors import api_ok, api_error
|
||||
from app.api.decorators import jwt_required
|
||||
@@ -45,7 +45,7 @@ def create_issue():
|
||||
------------
|
||||
{
|
||||
"inspection_id": 42, // optional
|
||||
"area_id": 12, // required
|
||||
"facility_id": 5, // required
|
||||
"severity": "high", // "low"|"medium"|"high"|"critical"
|
||||
"description": "...", // required
|
||||
"photo_path": "uploads/...",// optional — already uploaded via /photos/upload
|
||||
@@ -73,20 +73,20 @@ def create_issue():
|
||||
return api_ok({'issue_id': existing.id, 'duplicate': True})
|
||||
|
||||
# ── Validate ──────────────────────────────────────────────────────────
|
||||
area_id = data.get('area_id')
|
||||
facility_id = data.get('facility_id')
|
||||
severity = data.get('severity', '').lower()
|
||||
description = (data.get('description') or '').strip()
|
||||
|
||||
if not area_id:
|
||||
return api_error('area_id is required', 400)
|
||||
if not facility_id:
|
||||
return api_error('facility_id is required', 400)
|
||||
if severity not in _VALID_SEVERITY:
|
||||
return api_error(f'severity must be one of: {", ".join(sorted(_VALID_SEVERITY))}', 400)
|
||||
if not description:
|
||||
return api_error('description is required', 400)
|
||||
|
||||
area = db.session.get(Area, area_id)
|
||||
if area is None:
|
||||
return api_error('Area not found', 404)
|
||||
facility = db.session.get(Facility, facility_id)
|
||||
if facility is None:
|
||||
return api_error('Facility not found', 404)
|
||||
|
||||
inspection_id = data.get('inspection_id')
|
||||
if inspection_id:
|
||||
@@ -99,7 +99,7 @@ def create_issue():
|
||||
# ── Create ────────────────────────────────────────────────────────────
|
||||
issue = Issue(
|
||||
inspection_id = inspection_id,
|
||||
area_id = area_id,
|
||||
facility_id = facility_id,
|
||||
severity = severity,
|
||||
description = description,
|
||||
photo_path = data.get('photo_path') or None,
|
||||
@@ -111,7 +111,6 @@ def create_issue():
|
||||
db.session.flush() # get issue.id
|
||||
|
||||
# ── Notifications ─────────────────────────────────────────────────────
|
||||
facility_id = area.facility_id
|
||||
try:
|
||||
from flask import url_for
|
||||
issue_link = url_for('issues.view', issue_id=issue.id, _external=False)
|
||||
@@ -123,8 +122,8 @@ def create_issue():
|
||||
event_type = 'issue_flagged',
|
||||
title = f'New Issue #{issue.id} (Mobile)',
|
||||
body = (
|
||||
f'A new {severity.title()}-severity issue was logged in '
|
||||
f'{area.name} during {inspection_ref}. '
|
||||
f'A new {severity.title()}-severity issue was logged at '
|
||||
f'{facility.name} during {inspection_ref}. '
|
||||
f'Description: {description[:120]}'
|
||||
f'{"…" if len(description) > 120 else ""}'
|
||||
),
|
||||
@@ -137,11 +136,11 @@ def create_issue():
|
||||
db.session.commit()
|
||||
|
||||
log_action(ACTION_CREATE, 'Issue', issue.id,
|
||||
f'{severity} issue in {area.name}',
|
||||
f'{severity} issue at {facility.name}',
|
||||
f'source=mobile; inspection_id={inspection_id}; '
|
||||
f'local_id={mobile_local_id}')
|
||||
|
||||
logger.info('API ISSUES | created | issue_id=%d | area=%s | severity=%s | user=%s',
|
||||
issue.id, area.name, severity, user.username)
|
||||
logger.info('API ISSUES | created | issue_id=%d | facility=%s | severity=%s | user=%s',
|
||||
issue.id, facility.name, severity, user.username)
|
||||
|
||||
return api_ok({'issue_id': issue.id, 'duplicate': False})
|
||||
return api_ok({'issue_id': issue.id, 'duplicate': False})
|
||||
|
||||
Reference in New Issue
Block a user