fix: invalidate stale service records PDF cache on service record changes
Ensure admin/API deletes and updates of service records, photos, and attachments invalidate the cached service-records PDF. Also harden cached PDF lookup/download paths so a missing file is treated as stale cache instead of a 500. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -298,6 +298,52 @@ class WorkOrderImagesEndpointTests(TestCase):
|
||||
self.assertFalse(cached.is_active)
|
||||
self.assertEqual(cached.status, 'failed')
|
||||
|
||||
def test_service_record_delete_invalidates_service_records_pdf_cache(self):
|
||||
cached = GeneratedWorkOrderPdf.objects.create(
|
||||
work_order=self.work_order,
|
||||
requested_by=self.user,
|
||||
pdf_type='service_records',
|
||||
status='ready',
|
||||
filename='MT150726.work-order-service-records.pdf',
|
||||
expires_at=timezone.now() + timedelta(hours=1),
|
||||
)
|
||||
cached.file.save(
|
||||
'MT150726.work-order-service-records.pdf',
|
||||
ContentFile(b'%PDF-1.4 cached service records'),
|
||||
save=True,
|
||||
)
|
||||
|
||||
VehicleServiceRecord.objects.filter(pk=self.service_record.pk).delete()
|
||||
|
||||
cached.refresh_from_db()
|
||||
self.assertFalse(cached.is_active)
|
||||
self.assertEqual(cached.status, 'failed')
|
||||
self.assertFalse(cached.file.name)
|
||||
|
||||
def test_service_records_pdf_rebuilds_when_cached_file_is_missing(self):
|
||||
cached = GeneratedWorkOrderPdf.objects.create(
|
||||
work_order=self.work_order,
|
||||
requested_by=self.user,
|
||||
pdf_type='service_records',
|
||||
status='ready',
|
||||
filename='MT150726.work-order-service-records.pdf',
|
||||
expires_at=timezone.now() + timedelta(hours=1),
|
||||
)
|
||||
cached.file.save(
|
||||
'MT150726.work-order-service-records.pdf',
|
||||
ContentFile(b'%PDF-1.4 cached service records'),
|
||||
save=True,
|
||||
)
|
||||
cached.file.storage.delete(cached.file.name)
|
||||
|
||||
response = self.client.get(f"/api/fleet/work-orders/{self.work_order.pk}/service-records-pdf/")
|
||||
self.assertEqual(response.status_code, 200, response.content)
|
||||
self.assertEqual(response['Content-Type'], 'application/pdf')
|
||||
|
||||
cached.refresh_from_db()
|
||||
self.assertFalse(cached.is_active)
|
||||
self.assertEqual(cached.status, 'failed')
|
||||
|
||||
def test_service_records_docx_contains_embedded_service_photos(self):
|
||||
VehicleServicePhoto.objects.create(
|
||||
service_record=self.service_record,
|
||||
|
||||
Reference in New Issue
Block a user