diff --git a/concordia/tests/test_views.py b/concordia/tests/test_views.py index dbbddf045..e92ba5f98 100644 --- a/concordia/tests/test_views.py +++ b/concordia/tests/test_views.py @@ -663,12 +663,25 @@ def test_generate_ocr_transcription(self, mock): mock.reset_mock() with patch("concordia.views.get_transcription_superseded") as superseded_mock: + # Test case if the trancription being replaced has already been superseded superseded_mock.return_value = HttpResponse(status=409) url = reverse("generate-ocr-transcription", kwargs={"asset_pk": asset2.pk}) response = self.client.post(url) self.assertEqual(409, response.status_code) + self.assertTrue(superseded_mock.called) self.assertFalse(mock.called) + # Test case if the transcription being replaced hasn't been superseded + superseded_mock.reset_mock() + superseded_mock.return_value = create_transcription( + asset=asset2, user=get_anonymous_user(), submitted=now() + ) + url = reverse("generate-ocr-transcription", kwargs={"asset_pk": asset2.pk}) + response = self.client.post(url) + self.assertEqual(201, response.status_code) + self.assertTrue(superseded_mock.called) + self.assertTrue(mock.called) + def test_project_detail_view(self): """ Test GET on route /campaigns/ (campaign) diff --git a/concordia/views.py b/concordia/views.py index 6a3ca393f..24556d576 100644 --- a/concordia/views.py +++ b/concordia/views.py @@ -1631,10 +1631,16 @@ def generate_ocr_transcription(request, *, asset_pk): language = request.POST.get("language", None) superseded = get_transcription_superseded(asset, supersedes_pk) if superseded: - return superseded + # If superseded is an HttpResponse, that means + # this transcription has already been superseded, so + # we won't run OCR and instead send back an error + # Otherwise, we just have thr transcription the OCR + # is gong to supersede, so we can continue + if isinstance(superseded, HttpResponse): + return superseded else: - # This means this is the first transcription on this asset - # to enable undoing of the OCR transcription, we create + # This means this is the first transcription on this asset. + # To enable undoing of the OCR transcription, we create # an empty transcription for the OCR transcription to supersede superseded = Transcription( asset=asset,