From ad6f7d06734a97333db6f10a8bbea40963d5769c Mon Sep 17 00:00:00 2001 From: dx-tan Date: Thu, 7 Dec 2023 17:05:58 +0700 Subject: [PATCH] Add: sync api --- cope2n-api/fwd_api/api/ctel_view.py | 111 ++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) diff --git a/cope2n-api/fwd_api/api/ctel_view.py b/cope2n-api/fwd_api/api/ctel_view.py index 3da94a1..e487b01 100755 --- a/cope2n-api/fwd_api/api/ctel_view.py +++ b/cope2n-api/fwd_api/api/ctel_view.py @@ -176,6 +176,117 @@ class CtelViewSet(viewsets.ViewSet): j_time = time.time() return JsonResponse(status=status.HTTP_200_OK, data={"request_id": rq_id}) + @extend_schema(request={ + 'multipart/form-data': { + 'type': 'object', + 'properties': { + 'imei_files': { + 'type': 'array', + 'items': { + 'type': 'string', + 'format': 'binary' + } + }, + 'invoice_file': { + 'type': 'string', + 'format': 'binary' + }, + 'redemption_ID': { + 'type': 'string' + }, + }, + 'required': {'imei_files'} + } + }, responses=None, tags=['ocr']) + @action(detail=False, url_path="images/process_sync", methods=["POST"]) + # @transaction.atomic + def processes_sync(self, request): + s_time = time.time() + # print(30*"=") + # print(f"[DEBUG]: request: {request}") + # print(30*"=") + user_info = ProcessUtil.get_user(request) + user = user_info.user + sub = user_info.current_sub + + validated_data = ProcessUtil.sbt_validate_ocr_request_and_get(request, sub) + + provider_code = 'SAP' + rq_id = provider_code + uuid.uuid4().hex + + imei_file_objs: List[TemporaryUploadedFile] = validated_data['imei_file'] + invoice_file_objs: List[TemporaryUploadedFile] = validated_data['invoice_file'] + + files = { + "imei": imei_file_objs, + "invoice": invoice_file_objs + } + total_page = len(files.keys()) + # file_paths = [] + list_urls = [] + p_type = validated_data['type'] + new_request: SubscriptionRequest = SubscriptionRequest(pages=total_page, + process_type=p_type, status=1, request_id=rq_id, + provider_code=provider_code, + subscription=sub) + new_request.save() + count = 0 + compact_files = [] + for doc_type, doc_files in files.items(): + for i, doc_file in enumerate(doc_files): + _ext = doc_file.name.split(".")[-1] + if _ext not in allowed_file_extensions: + return JsonResponse(status=status.HTTP_406_NOT_ACCEPTABLE, data={"request_id": rq_id, "message": f"File {_ext} is now allowed"}) + _name = f"temp_{doc_type}_{rq_id}_{i}.{_ext}" + doc_file.seek(0) + file_path = FileUtils.resize_and_save_file(_name, new_request, doc_file, 100) + S3_path = FileUtils.save_to_S3(_name, new_request, file_path) + count += 1 + this_file = { + "file_name": _name, + "file_path": file_path, + "file_type": doc_type + } + compact_files.append(this_file) + c_connector.do_pdf((rq_id, sub.id, p_type, user.id, compact_files)) + + j_time = time.time() + + time_out = 120 + start = time.time() + while time.time() - start < time_out: + time.sleep(0.1) + report_filter = SubscriptionRequest.objects.filter(request_id=rq_id) + if len(report_filter) != 1: + raise InvalidException(excArgs='requestId') + + if user_info.current_sub.id != report_filter[0].subscription.id: + raise InvalidException(excArgs="user") + if int(report_filter[0].process_type) == ProcessType.FI_INVOICE.value: + data = report_filter[0].predict_result + xml_as_string = "" + + if data and 'content' in data and 'combine_results' in data['content'] and 'xml' in data['content']['combine_results']: + xml_as_string = data['content']['combine_results']['xml'] + xml_as_string = xml_as_string.replace("\n", "").replace("\\", "") + return HttpResponse(xml_as_string,content_type="text/xml") + + serializer: ReportSerializer = ReportSerializer(data=report_filter, many=True) + serializer.is_valid() + if report_filter[0].status == 400: + raise FileContentInvalidException() + + if len(serializer.data) == 0: + continue + if serializer.data[0].get("data", None) is None: + continue + if serializer.data[0]["data"].get("status", 200) != 200: + continue + + return Response(status=status.HTTP_200_OK, data=serializer.data[0]) + return JsonResponse(status=status.HTTP_504_GATEWAY_TIMEOUT, data={"status": "timeout",f"request_id": "{rq_id}"}) + + @extend_schema(request=None, responses=None, tags=['data']) @extend_schema(request=None, responses=None, tags=['templates'], methods=['GET']) @action(detail=False, url_path=r"media/(?P\w+)/(?P\w+)", methods=["GET"])