diff --git a/app/models/inspection.py b/app/models/inspection.py index cb53a44..5485376 100644 --- a/app/models/inspection.py +++ b/app/models/inspection.py @@ -65,7 +65,7 @@ class Inspection(db.Model): completed_at = db.Column(db.DateTime) results = db.relationship('InspectionResult', backref='inspection', lazy='dynamic', cascade='all, delete-orphan') - issues = db.relationship('Issue', backref='inspection', lazy='dynamic') + issues = db.relationship('Issue', backref='inspection', lazy='dynamic', cascade='all, delete-orphan') def __repr__(self): return f'' @@ -83,4 +83,4 @@ class InspectionResult(db.Model): photo_path = db.Column(db.String(255)) def __repr__(self): - return f'' + return f'' \ No newline at end of file diff --git a/app/routes/inspections.py b/app/routes/inspections.py index e5c303e..d953470 100644 --- a/app/routes/inspections.py +++ b/app/routes/inspections.py @@ -404,7 +404,55 @@ def flag_issue(inspection_id): @supervisor_required def delete(inspection_id): inspection = Inspection.query.get_or_404(inspection_id) + + # Capture identifiers for the log before deletion + insp_id = inspection.id + insp_date = inspection.inspection_date.strftime('%Y-%m-%d %H:%M') + facility_name = inspection.facility.name + template_name = inspection.template.name + inspector_name = inspection.inspector.username + + # ── Clean up uploaded photos from disk ──────────────────────────────── + # Collect photo paths from form data (inspection_photos) and issues + photo_paths = [] + if inspection.notes: + try: + notes_data = json.loads(inspection.notes) + form_data = notes_data.get('_form_data', {}) if isinstance(notes_data, dict) else {} + for val in form_data.values(): + if isinstance(val, str) and val.startswith('uploads/'): + photo_paths.append(val) + except (json.JSONDecodeError, TypeError): + pass + + for issue in inspection.issues.all(): + if issue.photo_path: + photo_paths.append(issue.photo_path) + + # Delete the inspection record (cascade removes results and issues) db.session.delete(inspection) db.session.commit() - flash('Inspection deleted.', 'success') - return redirect(url_for('inspections.index')) + + # Remove photo files after successful DB commit + for rel_path in photo_paths: + abs_path = os.path.join(current_app.config['UPLOAD_FOLDER'], '..', 'static', rel_path) + abs_path = os.path.normpath(abs_path) + try: + if os.path.isfile(abs_path): + os.remove(abs_path) + except OSError: + pass # Non-fatal: log but don't block the response + + current_app.logger.info( + 'INSPECTION DELETED | id=%s | facility="%s" | template="%s" | ' + 'date=%s | inspector=%s | deleted_by=%s', + insp_id, facility_name, template_name, + insp_date, inspector_name, current_user.username + ) + + flash( + f'Inspection #{insp_id} ({template_name} — {facility_name}, {insp_date}) ' + f'has been permanently deleted.', + 'success' + ) + return redirect(url_for('inspections.index')) \ No newline at end of file diff --git a/app/templates/inspections/list.html b/app/templates/inspections/list.html index 81a0194..59e24e3 100644 --- a/app/templates/inspections/list.html +++ b/app/templates/inspections/list.html @@ -70,12 +70,23 @@ {{ ins.status|replace('_',' ')|title }} - + {% if ins.status == 'in_progress' or ins.status == 'flagged' %} Continue {% else %} View {% endif %} + {% if current_user.role in ['admin', 'supervisor'] %} + + {% endif %} {% endfor %} @@ -103,4 +114,52 @@ {% endif %} +{% if current_user.role in ['admin', 'supervisor'] %} + + +{% endif %} {% endblock %} + +{% block extra_js %} +{% if current_user.role in ['admin', 'supervisor'] %} + +{% endif %} +{% endblock %} \ No newline at end of file