Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(proofs): run OCR in post_save signal instead of create #549

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions open_prices/api/proofs/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
from django.conf import settings
from django_filters.rest_framework import DjangoFilterBackend
from django_q.tasks import async_task
from drf_spectacular.utils import extend_schema
from rest_framework import filters, mixins, status, viewsets
from rest_framework.decorators import action
Expand Down Expand Up @@ -77,10 +75,6 @@ def upload(self, request: Request) -> Response:
status=status.HTTP_400_BAD_REQUEST,
)
file_path, mimetype, image_thumb_path = store_file(request.data.get("file"))
async_task(
"open_prices.proofs.utils.run_ocr_task",
f"{settings.IMAGES_DIR}/{file_path}",
)
proof_create_data = {
"file_path": file_path,
"mimetype": mimetype,
Expand Down
11 changes: 11 additions & 0 deletions open_prices/proofs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.db.models import Count, signals
from django.dispatch import receiver
from django.utils import timezone
from django_q.tasks import async_task

from open_prices.common import constants, utils
from open_prices.locations import constants as location_constants
Expand Down Expand Up @@ -299,6 +300,16 @@ def set_missing_fields_from_prices(self):
self.save()


@receiver(signals.post_save, sender=Proof)
def proof_post_save_run_ocr(sender, instance, created, **kwargs):
if not settings.TESTING:
if created:
async_task(
"open_prices.proofs.utils.fetch_and_save_ocr_data",
f"{settings.IMAGES_DIR}/{instance.file_path}",
)


@receiver(signals.post_save, sender=Proof)
def proof_post_save_update_prices(sender, instance, created, **kwargs):
if not created:
Expand Down
6 changes: 3 additions & 3 deletions open_prices/proofs/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from open_prices.proofs import constants as proof_constants
from open_prices.proofs.factories import ProofFactory
from open_prices.proofs.models import Proof
from open_prices.proofs.utils import run_ocr_task
from open_prices.proofs.utils import fetch_and_save_ocr_data

LOCATION_OSM_NODE_652825274 = {
"type": location_constants.TYPE_OSM,
Expand Down Expand Up @@ -311,7 +311,7 @@ def test_proof_update(self):


class RunOCRTaskTest(TestCase):
def test_run_ocr_task_success(self):
def test_fetch_and_save_ocr_data_success(self):
response_data = {"responses": [{"textAnnotations": [{"description": "test"}]}]}
with self.settings(GOOGLE_CLOUD_VISION_API_KEY="test_api_key"):
# mock call to run_ocr_on_image
Expand All @@ -323,7 +323,7 @@ def test_run_ocr_task_success(self):
image_path = Path(f"{tmpdirname}/test.jpg")
with image_path.open("w") as f:
f.write("test")
run_ocr_task(image_path)
fetch_and_save_ocr_data(image_path)
mock_run_ocr_on_image.assert_called_once_with(
image_path, "test_api_key"
)
Expand Down
2 changes: 1 addition & 1 deletion open_prices/proofs/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def run_ocr_on_image(image_path: Path | str, api_key: str) -> dict[str, Any] | N
return r.json()


def run_ocr_task(image_path: Path | str, override: bool = False) -> None:
def fetch_and_save_ocr_data(image_path: Path | str, override: bool = False) -> None:
"""Run OCR on the image stored at the given path and save the result to a
JSON file.

Expand Down