Merge pull request #46 from SDSRV-IDP/vietanh99-request-detail

Vietanh99 request detail
This commit is contained in:
Nguyen Viet Anh 2024-02-22 08:46:19 +07:00 committed by GitHub Enterprise
commit 63a0114aae
3 changed files with 61 additions and 12 deletions

View File

@ -15,11 +15,13 @@ import json
from ..exception.exceptions import InvalidException, RequiredFieldException, NotFoundException from ..exception.exceptions import InvalidException, RequiredFieldException, NotFoundException
from ..models import SubscriptionRequest, Report, ReportFile, SubscriptionRequestFile from ..models import SubscriptionRequest, Report, ReportFile, SubscriptionRequestFile
from ..utils.accuracy import shadow_report, MonthReportAccumulate, first_of_list, extract_report_detail_list, IterAvg 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.redis import RedisUtils
from ..utils.process import string_to_boolean from ..utils.process import string_to_boolean
from ..request.ReportCreationSerializer import ReportCreationSerializer from ..request.ReportCreationSerializer import ReportCreationSerializer
from ..utils.subsidiary import map_subsidiary_long_to_short, map_subsidiary_short_to_long 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() redis_client = RedisUtils()
@ -573,6 +575,14 @@ class RequestViewSet(viewsets.ViewSet):
raise NotFoundException(excArgs=request_id) raise NotFoundException(excArgs=request_id)
subscription_request = subscription_request.first() 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 = [] data = []
files = [] files = []
@ -584,26 +594,48 @@ class RequestViewSet(viewsets.ViewSet):
user_id = sub.user.id user_id = sub.user.id
sync_id = sub.user.sync_id sync_id = sub.user.sync_id
sub_id = sub.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({ files.append({
'File Name': subscription_request_file.file_name, 'File Name': subscription_request_file.file_name,
'File Path': subscription_request_file.file_path, 'File Path': subscription_request_file.file_path,
'File Category': subscription_request_file.file_category, '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), 'File URL': build_S3_url("sbt_invoice/" + subscription_request.request_id + "/" + subscription_request_file.file_name, 600),
'Origin_Name': subscription_request_file.origin_name, 'Original Name': subscription_request_file.origin_name,
'Is Bad Image Quality': subscription_request_file.is_bad_image_quality, 'Is Bad Image Quality': subscription_request_file.is_bad_image_quality,
'Doc Type': subscription_request_file.doc_type, 'Doc Type': subscription_request_file.doc_type,
'Processing Time (ms)': subscription_request_file.processing_time, 'Processing Time (ms)': subscription_request_file.processing_time,
'Reason': subscription_request_file.reason, 'Reason': subscription_request_file.reason,
'Counter Measures': subscription_request_file.counter_measures, 'Counter Measures': subscription_request_file.counter_measures,
'Predicted Result': subscription_request_file.predict_result, 'Predicted Result': predicted_result,
'Feedback Result': subscription_request_file.feedback_result, 'Feedback Result': feedback_result,
'Reviewed Result': subscription_request_file.reviewed_result, 'Reviewed Result': reviewed_result,
'Feedback Accuracy': subscription_request_file.feedback_accuracy, 'Feedback Accuracy': subscription_request_file.feedback_accuracy,
'Reviewed Accuracy': subscription_request_file.reviewed_accuracy, 'Reviewed Accuracy': subscription_request_file.reviewed_accuracy,
'Created At': subscription_request_file.created_at.isoformat(), 'Created At': subscription_request_file.created_at.isoformat(),
'Updated At': subscription_request_file.updated_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({ data.append({
'Document Type': subscription_request.doc_type, 'Document Type': subscription_request.doc_type,
'RequestID': subscription_request.request_id, 'RequestID': subscription_request.request_id,
@ -612,9 +644,9 @@ class RequestViewSet(viewsets.ViewSet):
'Provider Code': subscription_request.provider_code, 'Provider Code': subscription_request.provider_code,
'Status': subscription_request.status, 'Status': subscription_request.status,
'Files': files, 'Files': files,
'Reviewed Result': subscription_request.reviewed_result, 'Reviewed Result': reviewed_result,
'Feedback Result': subscription_request.feedback_result, 'Feedback Result': feedback_result,
'Predicted Result': subscription_request.predict_result, 'Predicted Result': predicted_result,
'Is Test Request': subscription_request.is_test_request, 'Is Test Request': subscription_request.is_test_request,
'Client Request Time (ms)': subscription_request.client_request_time, 'Client Request Time (ms)': subscription_request.client_request_time,
'Server Processing Time (ms)': subscription_request.preprocessing_time + subscription_request.ai_inference_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) subscription_request_files = SubscriptionRequestFile.objects.filter(request=subscription_request.id)
reviewed_result = json.loads(data["reviewed_result"]) 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: for subscription_request_file in subscription_request_files:
if subscription_request_file.doc_type == 'invoice': 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.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() 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 = reviewed_result
subscription_request.reviewed_result['request_id'] = request_id subscription_request.reviewed_result['request_id'] = request_id
subscription_request.is_reviewed = True subscription_request.is_reviewed = True

View File

@ -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) 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}' 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): def get_value(_dict, keys):
keys = keys.split('.') keys = keys.split('.')

View File

@ -51,6 +51,21 @@ class MinioS3Client:
return res return res
except Exception as e: except Exception as e:
print(f"Error downloading file from S3: {str(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__": if __name__=="__main__":
FILE = "/app/media/users/1/subscriptions/33/requests/sbt_invoice/SAP00c6c229c2954e498b119968a318d366/temp_SAP00c6c229c2954e498b119968a318d366.jpg" FILE = "/app/media/users/1/subscriptions/33/requests/sbt_invoice/SAP00c6c229c2954e498b119968a318d366/temp_SAP00c6c229c2954e498b119968a318d366.jpg"