06/22 Update /api/inspections.py
This commit is contained in:
+21
-31
@@ -18,6 +18,7 @@ GET /api/v1/inspections
|
|||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
from flask import Blueprint, request, g
|
from flask import Blueprint, request, g
|
||||||
from app import db
|
from app import db
|
||||||
@@ -161,6 +162,8 @@ def list_inspections():
|
|||||||
offset int default 0
|
offset int default 0
|
||||||
facility_id int filter by facility
|
facility_id int filter by facility
|
||||||
status str filter by status (completed, in_progress, flagged)
|
status str filter by status (completed, in_progress, flagged)
|
||||||
|
from_date str ISO date (YYYY-MM-DD) — include inspections on/after this date
|
||||||
|
to_date str ISO date (YYYY-MM-DD) — include inspections on/before this date
|
||||||
|
|
||||||
Response 200
|
Response 200
|
||||||
------------
|
------------
|
||||||
@@ -197,6 +200,22 @@ def list_inspections():
|
|||||||
if status:
|
if status:
|
||||||
query = query.filter(Inspection.status == status)
|
query = query.filter(Inspection.status == status)
|
||||||
|
|
||||||
|
from_date_str = request.args.get('from_date')
|
||||||
|
if from_date_str:
|
||||||
|
try:
|
||||||
|
from_dt = datetime.strptime(from_date_str, '%Y-%m-%d').date()
|
||||||
|
query = query.filter(Inspection.inspection_date >= from_dt)
|
||||||
|
except ValueError:
|
||||||
|
pass # malformed date — ignore silently
|
||||||
|
|
||||||
|
to_date_str = request.args.get('to_date')
|
||||||
|
if to_date_str:
|
||||||
|
try:
|
||||||
|
to_dt = datetime.strptime(to_date_str, '%Y-%m-%d').date()
|
||||||
|
query = query.filter(Inspection.inspection_date <= to_dt)
|
||||||
|
except ValueError:
|
||||||
|
pass # malformed date — ignore silently
|
||||||
|
|
||||||
total = query.count()
|
total = query.count()
|
||||||
inspections = (
|
inspections = (
|
||||||
query
|
query
|
||||||
@@ -396,13 +415,14 @@ def create_inspection():
|
|||||||
event_type = 'inspection_completed',
|
event_type = 'inspection_completed',
|
||||||
title = f'Inspection #{inspection.id} Completed (Mobile)',
|
title = f'Inspection #{inspection.id} Completed (Mobile)',
|
||||||
body = (
|
body = (
|
||||||
f'{user.display_name} completed an inspection at '
|
f'{user.username} completed an inspection at '
|
||||||
f'{facility.name} using the "{template.name}" template. '
|
f'{facility.name} using the "{template.name}" template. '
|
||||||
f'Overall score: {score_display}.'
|
f'Overall score: {score_display}.'
|
||||||
),
|
),
|
||||||
link = inspection_link,
|
link = inspection_link,
|
||||||
inspection_id = inspection.id,
|
inspection_id = inspection.id,
|
||||||
facility_id = facility_id,
|
facility_id = facility_id,
|
||||||
|
exclude_user_ids = {user.id},
|
||||||
)
|
)
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
@@ -476,8 +496,6 @@ def update_inspection(inspection_id):
|
|||||||
existing_notes['_inspector_notes'] = data['notes']
|
existing_notes['_inspector_notes'] = data['notes']
|
||||||
inspection.notes = json.dumps(existing_notes)
|
inspection.notes = json.dumps(existing_notes)
|
||||||
|
|
||||||
prev_status = inspection.status
|
|
||||||
|
|
||||||
if 'status' in data:
|
if 'status' in data:
|
||||||
inspection.status = data['status']
|
inspection.status = data['status']
|
||||||
|
|
||||||
@@ -493,34 +511,6 @@ def update_inspection(inspection_id):
|
|||||||
elif data.get('status') == 'completed' and not inspection.completed_at:
|
elif data.get('status') == 'completed' and not inspection.completed_at:
|
||||||
inspection.completed_at = now_eastern()
|
inspection.completed_at = now_eastern()
|
||||||
|
|
||||||
# Notify when a draft transitions to completed (mirrors the POST handler and web route).
|
|
||||||
transitioning_to_complete = (
|
|
||||||
data.get('status') == 'completed' and prev_status != 'completed'
|
|
||||||
)
|
|
||||||
if transitioning_to_complete:
|
|
||||||
score_val = inspection.overall_score
|
|
||||||
score_display = f'{score_val:.1f}%' if score_val is not None else 'N/A'
|
|
||||||
template_name = inspection.template.name if inspection.template else 'Unknown'
|
|
||||||
facility_name = inspection.facility.name if inspection.facility else 'Unknown'
|
|
||||||
try:
|
|
||||||
from flask import url_for
|
|
||||||
inspection_link = url_for('inspections.view',
|
|
||||||
inspection_id=inspection.id, _external=False)
|
|
||||||
except RuntimeError:
|
|
||||||
inspection_link = f'/inspections/{inspection.id}'
|
|
||||||
notify_by_matrix(
|
|
||||||
event_type = 'inspection_completed',
|
|
||||||
title = f'Inspection #{inspection.id} Completed (Mobile)',
|
|
||||||
body = (
|
|
||||||
f'{user.display_name} completed an inspection at '
|
|
||||||
f'{facility_name} using the "{template_name}" template. '
|
|
||||||
f'Overall score: {score_display}.'
|
|
||||||
),
|
|
||||||
link = inspection_link,
|
|
||||||
inspection_id = inspection.id,
|
|
||||||
facility_id = inspection.facility_id,
|
|
||||||
)
|
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
log_action(ACTION_UPDATE, 'Inspection', inspection.id,
|
log_action(ACTION_UPDATE, 'Inspection', inspection.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user