ADD: get request image api, UPDATE: request list total field

This commit is contained in:
daovietanh99 2024-02-21 14:00:25 +07:00
parent fda47870dc
commit 7f3e198911
2 changed files with 38 additions and 1 deletions

View File

@ -148,6 +148,8 @@ class AccuracyViewSet(viewsets.ViewSet):
subscription_requests = SubscriptionRequest.objects.filter(base_query).order_by('created_at') subscription_requests = SubscriptionRequest.objects.filter(base_query).order_by('created_at')
request_count = subscription_requests.count()
paginator = Paginator(subscription_requests, page_size) paginator = Paginator(subscription_requests, page_size)
page = paginator.get_page(page_number) page = paginator.get_page(page_number)
@ -192,6 +194,7 @@ class AccuracyViewSet(viewsets.ViewSet):
'number': page.number, 'number': page.number,
'total_pages': page.paginator.num_pages, 'total_pages': page.paginator.num_pages,
'count': page.paginator.count, 'count': page.paginator.count,
'total_requests': request_count
} }
} }
@ -663,3 +666,36 @@ class RequestViewSet(viewsets.ViewSet):
return JsonResponse({'message': 'success.'}, status=200) return JsonResponse({'message': 'success.'}, status=200)
else: else:
return JsonResponse({'error': 'Invalid request method.'}, status=405) return JsonResponse({'error': 'Invalid request method.'}, status=405)
class RequestImageViewSet(viewsets.ViewSet):
lookup_field = "username"
@action(detail=False, url_path=r"request_image/(?P<request_id>[\w\-]+)/(?P<image_name>[\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)

View File

@ -2,7 +2,7 @@ from django.conf import settings
from rest_framework.routers import DefaultRouter, SimpleRouter from rest_framework.routers import DefaultRouter, SimpleRouter
from fwd_api.api.ctel_view import CtelViewSet 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 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", CtelUserViewSet, basename="CtelUserAPI")
router.register("ctel", AccuracyViewSet, basename="AccuracyAPI") router.register("ctel", AccuracyViewSet, basename="AccuracyAPI")
router.register("ctel", RequestViewSet, basename="RequestAPI") router.register("ctel", RequestViewSet, basename="RequestAPI")
router.register("ctel", RequestImageViewSet, basename="RequestImageAPI")
app_name = "api" app_name = "api"
urlpatterns = router.urls urlpatterns = router.urls