From 0900b44e6554e978f85c9a7c4f2765cb74d67432 Mon Sep 17 00:00:00 2001 From: daovietanh99 Date: Mon, 19 Feb 2024 09:18:43 +0700 Subject: [PATCH] ADD: migrate review --- cope2n-api/fwd_api/api/accuracy_view.py | 3 +- .../management/commands/migrate-review.py | 83 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 cope2n-api/fwd_api/management/commands/migrate-review.py diff --git a/cope2n-api/fwd_api/api/accuracy_view.py b/cope2n-api/fwd_api/api/accuracy_view.py index d69d30b..a5660e2 100644 --- a/cope2n-api/fwd_api/api/accuracy_view.py +++ b/cope2n-api/fwd_api/api/accuracy_view.py @@ -616,6 +616,8 @@ class RequestViewSet(viewsets.ViewSet): subscription_request_files = SubscriptionRequestFile.objects.filter(file_query) + reviewed_result = json.loads(data["reviewed_result"]) + for subscription_request_file in subscription_request_files: if subscription_request_file.doc_type == 'invoice': subscription_request_file.reviewed_result = reviewed_result @@ -624,7 +626,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() - 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}') diff --git a/cope2n-api/fwd_api/management/commands/migrate-review.py b/cope2n-api/fwd_api/management/commands/migrate-review.py new file mode 100644 index 0000000..5f99e63 --- /dev/null +++ b/cope2n-api/fwd_api/management/commands/migrate-review.py @@ -0,0 +1,83 @@ +# myapp/management/commands/mycustomcommand.py +from django.core.management.base import BaseCommand +from fwd_api.models import SubscriptionRequestFile, SubscriptionRequest +import traceback +import copy +from openpyxl import load_workbook +import datetime + +class Command(BaseCommand): + help = 'Migrate reviewed result' + + def add_arguments(self, parser): + # Add your command-line arguments here + parser.add_argument('test', type=str, help='Value for the argument') + + def process_request(self, subscription_request, ws, row): + if len(subscription_request.request_id.split(".")[0].split("_")) < 2: + return + + try: + reviewed_result = copy.deepcopy(subscription_request.reviewed_result) + + if reviewed_result is None: + reviewed_result = { + 'request_id': subscription_request.request_id, + "retailername": None, + "sold_to_party": None, + "purchase_date": None, + "imei_number": [] + } + + # if ws['J' + str(row)].value: + # if isinstance(ws['J' + str(row)].value, str): + # reviewed_result['purchase_date'] = ws['J' + str(row)].value + # else: + # reviewed_result['purchase_date'] = datetime.date.strftime(ws['J' + str(row)].value, "%d/%m/%Y") + + # bad_reason = ws['R' + str(row)].value + + if ws['N' + str(row)].value: + reviewed_result['retailername'] = ws['N' + str(row)].value + + bad_reason = ws['P' + str(row)].value + + if not bad_reason: + bad_reason = "" + + subscription_request_files = SubscriptionRequestFile.objects.filter(request=subscription_request) + + for subscription_request_file in subscription_request_files: + if subscription_request_file.doc_type == 'invoice': + subscription_request_file.reviewed_result = reviewed_result + subscription_request_file.reviewed_result['imei_number'] = [] + subscription_request_file.reason = bad_reason + # elif subscription_request_file.doc_type == 'imei' and len(reviewed_result['imei_number']) > 0: + # subscription_request_file.reviewed_result = {"retailername": None, "sold_to_party": None, "purchase_date": None, "imei_number": [reviewed_result["imei_number"][subscription_request_file.index_in_request]]} + subscription_request_file.save() + + subscription_request.reviewed_result = reviewed_result + subscription_request.is_reviewed = True + + subscription_request.save() + + except Exception as e: + self.stdout.write(self.style.ERROR(f"Request: {subscription_request.request_id} failed with {e}")) + print(traceback.format_exc()) + + def handle(self, *args, **options): + test = options['test'] + #open csv file + wb = load_workbook(filename = test) + ws = wb['Sheet1'] + for row in range(2, 84): + print(ws['B' + str(row)].value) + request = SubscriptionRequest.objects.filter(request_id=ws['B' + str(row)].value).first() + if not request: + print("Not found ====>", row) + else: + self.process_request(request, ws, row) + print("") + + + self.stdout.write(self.style.SUCCESS('Sample Django management command executed successfully!'))