diff --git a/cope2n-api/fwd_api/api/accuracy_view.py b/cope2n-api/fwd_api/api/accuracy_view.py index db6f0cd..8f7c798 100644 --- a/cope2n-api/fwd_api/api/accuracy_view.py +++ b/cope2n-api/fwd_api/api/accuracy_view.py @@ -15,11 +15,13 @@ 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, build_media_url_v2, build_url +from ..utils.file import download_from_S3, convert_date_string, build_media_url_v2, build_url, build_S3_url from ..utils.redis import RedisUtils from ..utils.process import string_to_boolean from ..request.ReportCreationSerializer import ReportCreationSerializer from ..utils.subsidiary import map_subsidiary_long_to_short, map_subsidiary_short_to_long +from fwd_api.utils.accuracy import predict_result_to_ready +import copy redis_client = RedisUtils() @@ -573,6 +575,14 @@ class RequestViewSet(viewsets.ViewSet): raise NotFoundException(excArgs=request_id) subscription_request = subscription_request.first() + + sample_result = { + "request_id": subscription_request.request_id, + "retailername": None, + "sold_to_party": None, + "purchase_date": None, + "imei_number": [] + } data = [] files = [] @@ -584,26 +594,48 @@ class RequestViewSet(viewsets.ViewSet): user_id = sub.user.id sync_id = sub.user.sync_id sub_id = sub.id + reviewed_result = subscription_request_file.reviewed_result + feedback_result = subscription_request_file.feedback_result + predicted_result = subscription_request_file.predict_result + + if not reviewed_result: + reviewed_result = copy.deepcopy(sample_result) + if not feedback_result: + feedback_result = copy.deepcopy(sample_result) + if not predicted_result: + predicted_result = copy.deepcopy(sample_result) + 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, + 'File URL': build_S3_url("sbt_invoice/" + subscription_request.request_id + "/" + subscription_request_file.file_name, 600), + 'Original Name': subscription_request_file.origin_name, 'Is Bad Image Quality': subscription_request_file.is_bad_image_quality, 'Doc Type': subscription_request_file.doc_type, 'Processing Time (ms)': subscription_request_file.processing_time, 'Reason': subscription_request_file.reason, 'Counter Measures': subscription_request_file.counter_measures, - 'Predicted Result': subscription_request_file.predict_result, - 'Feedback Result': subscription_request_file.feedback_result, - 'Reviewed Result': subscription_request_file.reviewed_result, + 'Predicted Result': predicted_result, + 'Feedback Result': feedback_result, + 'Reviewed Result': reviewed_result, 'Feedback Accuracy': subscription_request_file.feedback_accuracy, 'Reviewed Accuracy': subscription_request_file.reviewed_accuracy, 'Created At': subscription_request_file.created_at.isoformat(), 'Updated At': subscription_request_file.updated_at.isoformat() }) + reviewed_result = subscription_request.reviewed_result + feedback_result = subscription_request.feedback_result + predicted_result = predict_result_to_ready(subscription_request.predict_result) + + if not reviewed_result: + reviewed_result = copy.deepcopy(sample_result) + if not feedback_result: + feedback_result = copy.deepcopy(sample_result) + if not predicted_result: + predicted_result = copy.deepcopy(sample_result) + data.append({ 'Document Type': subscription_request.doc_type, 'RequestID': subscription_request.request_id, @@ -612,9 +644,9 @@ class RequestViewSet(viewsets.ViewSet): 'Provider Code': subscription_request.provider_code, 'Status': subscription_request.status, 'Files': files, - 'Reviewed Result': subscription_request.reviewed_result, - 'Feedback Result': subscription_request.feedback_result, - 'Predicted Result': subscription_request.predict_result, + 'Reviewed Result': reviewed_result, + 'Feedback Result': feedback_result, + 'Predicted Result': predicted_result, 'Is Test Request': subscription_request.is_test_request, 'Client Request Time (ms)': subscription_request.client_request_time, 'Server Processing Time (ms)': subscription_request.preprocessing_time + subscription_request.ai_inference_time, @@ -646,6 +678,9 @@ class RequestViewSet(viewsets.ViewSet): subscription_request_files = SubscriptionRequestFile.objects.filter(request=subscription_request.id) reviewed_result = json.loads(data["reviewed_result"]) + for field in ['retailername', 'sold_to_party', 'purchase_date', 'imei_number']: + if not field in reviewed_result.keys(): + raise RequiredFieldException(excArgs=f'reviewed_result.{field}') for subscription_request_file in subscription_request_files: if subscription_request_file.doc_type == 'invoice': @@ -655,9 +690,6 @@ class RequestViewSet(viewsets.ViewSet): subscription_request_file.reviewed_result = {"retailername": None, "sold_to_party": None, "purchase_date": [], "imei_number": [reviewed_result["imei_number"][subscription_request_file.index_in_request]]} subscription_request_file.save() - for field in ['retailername', 'sold_to_party', 'purchase_date', 'imei_number']: - if not field in reviewed_result.keys(): - raise RequiredFieldException(excArgs=f'reviewed_result.{field}') subscription_request.reviewed_result = reviewed_result subscription_request.reviewed_result['request_id'] = request_id subscription_request.is_reviewed = True diff --git a/cope2n-api/fwd_api/utils/file.py b/cope2n-api/fwd_api/utils/file.py index ff613fa..7a0fa11 100644 --- a/cope2n-api/fwd_api/utils/file.py +++ b/cope2n-api/fwd_api/utils/file.py @@ -429,6 +429,8 @@ def build_media_url_v2(media_id: str, user_id: int, sub_id: int, u_sync_id: str) token = image_authenticator.generate_img_token_v2(user_id, sub_id, u_sync_id) return f'{settings.BASE_URL}/api/ctel/v2/media/request/{media_id}/?token={token}' +def build_S3_url(s3_key, exp_time): + return s3_client.create_url_with_expiration(s3_key, exp_time) def get_value(_dict, keys): keys = keys.split('.') diff --git a/cope2n-api/fwd_api/utils/s3.py b/cope2n-api/fwd_api/utils/s3.py index 60d8380..c1ee84f 100644 --- a/cope2n-api/fwd_api/utils/s3.py +++ b/cope2n-api/fwd_api/utils/s3.py @@ -51,6 +51,21 @@ class MinioS3Client: return res except Exception as e: print(f"Error downloading file from S3: {str(e)}") + + def create_url_with_expiration(self, s3_key, expiration_time): + try: + res = self.s3_client.generate_presigned_url( + ClientMethod="get_object", ExpiresIn=expiration_time, + Params={ + "Bucket": self.bucket_name, + "Key": s3_key, + }, + ) + # print(f"URL for file '{s3_key}' expires in {expiration_time} seconds") + return res + except Exception as e: + print(f"Error generating URL for file '{s3_key}': {str(e)}") + if __name__=="__main__": FILE = "/app/media/users/1/subscriptions/33/requests/sbt_invoice/SAP00c6c229c2954e498b119968a318d366/temp_SAP00c6c229c2954e498b119968a318d366.jpg"