From 7f3e19891159e0ff6132ea4c3776a89d4c155046 Mon Sep 17 00:00:00 2001 From: daovietanh99 Date: Wed, 21 Feb 2024 14:00:25 +0700 Subject: [PATCH] ADD: get request image api, UPDATE: request list total field --- cope2n-api/fwd_api/api/accuracy_view.py | 36 +++++++++++++++++++++++++ cope2n-api/fwd_api/api_router.py | 3 ++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/cope2n-api/fwd_api/api/accuracy_view.py b/cope2n-api/fwd_api/api/accuracy_view.py index f5cfc11..3a770b3 100644 --- a/cope2n-api/fwd_api/api/accuracy_view.py +++ b/cope2n-api/fwd_api/api/accuracy_view.py @@ -148,6 +148,8 @@ class AccuracyViewSet(viewsets.ViewSet): subscription_requests = SubscriptionRequest.objects.filter(base_query).order_by('created_at') + request_count = subscription_requests.count() + paginator = Paginator(subscription_requests, page_size) page = paginator.get_page(page_number) @@ -192,6 +194,7 @@ class AccuracyViewSet(viewsets.ViewSet): 'number': page.number, 'total_pages': page.paginator.num_pages, 'count': page.paginator.count, + 'total_requests': request_count } } @@ -663,3 +666,36 @@ class RequestViewSet(viewsets.ViewSet): return JsonResponse({'message': 'success.'}, status=200) else: return JsonResponse({'error': 'Invalid request method.'}, status=405) + + +class RequestImageViewSet(viewsets.ViewSet): + lookup_field = "username" + @action(detail=False, url_path=r"request_image/(?P[\w\-]+)/(?P[\w\-]+)", methods=["GET"]) + def get_request_image(self, request, request_id=None, image_name=None): + if request.method == 'GET': + base_query = Q(request_id=request_id) + + subscription_request = SubscriptionRequest.objects.filter(base_query) + + if subscription_request.count() == 0: + raise NotFoundException(excArgs=request_id) + + subscription_request = subscription_request.first() + + file_query = Q(request=request_id) + + subscription_request_files = SubscriptionRequestFile.objects.filter(file_query) + + for subscription_request_file in subscription_request_files: + if image_name == subscription_request_file.file_name: + tmp_file = None + download_from_S3("ocr-sds/ocr_invoice/" + request_id + "/" + image_name, tmp_file) + if tmp_file is not None: + file = open(tmp_file, 'rb') + return FileResponse(file, status=200) + else: + raise NotFoundException(excArgs=image_name) + + raise NotFoundException(excArgs=image_name) + else: + return JsonResponse({'error': 'Invalid request method.'}, status=405) diff --git a/cope2n-api/fwd_api/api_router.py b/cope2n-api/fwd_api/api_router.py index 322cc20..dec909e 100755 --- a/cope2n-api/fwd_api/api_router.py +++ b/cope2n-api/fwd_api/api_router.py @@ -2,7 +2,7 @@ from django.conf import settings from rest_framework.routers import DefaultRouter, SimpleRouter from fwd_api.api.ctel_view import CtelViewSet -from fwd_api.api.accuracy_view import AccuracyViewSet, RequestViewSet +from fwd_api.api.accuracy_view import AccuracyViewSet, RequestViewSet, RequestImageViewSet from fwd_api.api.ctel_user_view import CtelUserViewSet @@ -17,6 +17,7 @@ router.register("ctel", CtelViewSet, basename="CtelAPI") router.register("ctel", CtelUserViewSet, basename="CtelUserAPI") router.register("ctel", AccuracyViewSet, basename="AccuracyAPI") router.register("ctel", RequestViewSet, basename="RequestAPI") +router.register("ctel", RequestImageViewSet, basename="RequestImageAPI") app_name = "api" urlpatterns = router.urls