UPDATE: add image url to response of request

This commit is contained in:
daovietanh99 2024-02-21 16:20:15 +07:00
parent caa6bcda84
commit 25117a3697

View File

@ -15,7 +15,7 @@ import json
from ..exception.exceptions import InvalidException, RequiredFieldException, NotFoundException
from ..models import SubscriptionRequest, Report, ReportFile, SubscriptionRequestFile
from ..utils.accuracy import shadow_report, MonthReportAccumulate, first_of_list, extract_report_detail_list, IterAvg
from ..utils.file import download_from_S3, convert_date_string
from ..utils.file import download_from_S3, convert_date_string, build_media_url_v2, build_url
from ..utils.redis import RedisUtils
from ..utils.process import string_to_boolean
from ..request.ReportCreationSerializer import ReportCreationSerializer
@ -580,10 +580,15 @@ class RequestViewSet(viewsets.ViewSet):
subscription_request_files = SubscriptionRequestFile.objects.filter(request=subscription_request.id)
for subscription_request_file in subscription_request_files:
sub = subscription_request.subscription
user_id = sub.user.id
sync_id = sub.user.sync_id
sub_id = sub.id
files.append({
'File Name': subscription_request_file.file_name,
'File Path': subscription_request_file.file_path,
'File Category': subscription_request_file.file_category,
'File URL': build_media_url_v2(subscription_request_file.file_name.split['.'][0], user_id, sub_id, sync_id),
'Origin_Name': subscription_request_file.origin_name,
'Is Bad Image Quality': subscription_request_file.is_bad_image_quality,
'Doc Type': subscription_request_file.doc_type,
@ -661,34 +666,3 @@ 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<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()
subscription_request_files = SubscriptionRequestFile.objects.filter(request=subscription_request.id)
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)