diff --git a/cope2n-api/fwd_api/management/commands/migrate-csv.py b/cope2n-api/fwd_api/management/commands/migrate-csv.py index 5400bdb..2be1be0 100644 --- a/cope2n-api/fwd_api/management/commands/migrate-csv.py +++ b/cope2n-api/fwd_api/management/commands/migrate-csv.py @@ -14,62 +14,137 @@ class Command(BaseCommand): # Add your command-line arguments here parser.add_argument('test', type=str, help='Value for the argument') - def process_request(self, request, predict_result, user_feedback, reviewed_result, row): - with open('countries.csv', 'w', encoding='UTF8') as of: - writer = csv.writer(of) - writer.writerow(['requestId','redemptionNumber','userSubmitResults','OCRResults','revisedResults_by_SDSRV','accuracy','processingTime','processingTime (by request)']) - if len(request.request_id.split(".")[0].split("_")) < 2: - return - images = SubscriptionRequestFile.objects.filter(request=request) + def process_request(self, request, predict_result, user_feedback, reviewed_result): + if len(request.request_id.split(".")[0].split("_")) < 2: + return + + request_feedback = copy.deepcopy(request.feedback_result) + request_review = copy.deepcopy(request.reviewed_result) + + if not request_feedback: + request_feedback = { + "request_id": request.request_id, + "imei_number": [], + "retailername": "", + "purchase_date": "", + "sold_to_party": "" + } + + if not request_review: + request_review = { + "request_id": request.request_id, + "imei_number": [], + "retailername": "", + "purchase_date": "", + "sold_to_party": "" + } + + images = SubscriptionRequestFile.objects.filter(request=request) + is_match = False + try: for i, image in enumerate(images): - try: - if not request.predict_result: - raise KeyError(f"Key predict_result not found in {request.request_id}") - if request.predict_result.get("status", 200) != 200: - raise AttributeError(f"Failed request: {request.request_id}") - - for field in ['retailername', 'purchase_date', 'imei_number']: - if image.feedback_result[field] == user_feedback: - print(image.feedback_result) - # _predict_result = copy.deepcopy(predict_result_to_ready(request.predict_result)) - # _feedback_result = copy.deepcopy(request.feedback_result) - # _reviewed_result = copy.deepcopy(request.reviewed_result) + if not request.predict_result: + raise KeyError(f"Key predict_result not found in {request.request_id}") + if request.predict_result.get("status", 200) != 200: + raise AttributeError(f"Failed request: {request.request_id}") + + for field in ['retailername', 'purchase_date', 'imei_number']: + if image.feedback_result[field] is not None and ((field == 'imei_number' and len(image.feedback_result[field]) > 0 and image.feedback_result[field][0] == user_feedback) or image.feedback_result[field] == user_feedback): + is_match = True - # if image.doc_type == "invoice": - # _predict_result[field] = predict_result - # _predict_result["imei_number"] = [] - # if _feedback_result: - # _feedback_result[field] = user_feedback - # _feedback_result["imei_number"] = [] - # else: - # None - # if _reviewed_result: - # _reviewed_result[field] = reviewed_result - # _reviewed_result["imei_number"] = [] - # else: - # None - # else: - # _predict_result = {"retailername": None, "sold_to_party": None, "purchase_date": [], "imei_number": [predict_result]} - # _feedback_result = {"retailername": None, "sold_to_party": None, "purchase_date": None, "imei_number": [user_feedback]} if _feedback_result else None - # _reviewed_result = {"retailername": None, "sold_to_party": None, "purchase_date": None, "imei_number": [reviewed_result]} if _reviewed_result else None - # image.predict_result = _predict_result - # image.feedback_result = _feedback_result - # image.reviewed_result = _reviewed_result - # image.save() + if field == 'imei_number': + if not reviewed_result == request_review: + request_review["imei_number"].append(reviewed_result) + if not user_feedback == request_feedback: + request_feedback["imei_number"].append(user_feedback) else: - writer.writerow(row) + if not reviewed_result == request_review: + request_review[field] = reviewed_result + if not user_feedback == request_feedback: + request_feedback[field] = user_feedback - except Exception as e: - self.stdout.write(self.style.ERROR(f"Request: {request.request_id} failed with {e}")) - print(traceback.format_exc()) - continue + _predict_result = copy.deepcopy(predict_result_to_ready(request.predict_result)) + _feedback_result = copy.deepcopy(request.feedback_result) + _reviewed_result = copy.deepcopy(request.reviewed_result) + + if not _feedback_result: + _feedback_result = { + "imei_number": [], + "retailername": "", + "purchase_date": "", + "sold_to_party": "" + } + if not _reviewed_result: + _reviewed_result = { + "imei_number": [], + "retailername": "", + "purchase_date": "", + "sold_to_party": "" + } + + if image.doc_type == "invoice": + _predict_result[field] = predict_result + _predict_result["imei_number"] = [] + if _feedback_result: + _feedback_result[field] = user_feedback + _feedback_result["imei_number"] = [] + else: + None + if _reviewed_result: + _reviewed_result[field] = reviewed_result + _reviewed_result["imei_number"] = [] + else: + None + else: + _predict_result = { + "retailername": None, + "sold_to_party": None, + "purchase_date": [], + "imei_number": [predict_result] + } + _feedback_result = { + "retailername": None, + "sold_to_party": None, + "purchase_date": None, + "imei_number": [user_feedback] + } if _feedback_result else None + _reviewed_result = { + "retailername": None, + "sold_to_party": None, + "purchase_date": None, + "imei_number": [reviewed_result] + } if _reviewed_result else None + image.predict_result = _predict_result + image.feedback_result = _feedback_result + image.reviewed_result = _reviewed_result + image.save() + request.feedback_result = request_feedback + request.reviewed_result = request_review + request.feedback_result["request_id"] = request.request_id + request.reviewed_result["request_id"] = request.request_id + request.is_reviewed = True + request.save() + + except Exception as e: + self.stdout.write(self.style.ERROR(f"Request: {request.request_id} failed with {e}")) + print(traceback.format_exc()) + + if not is_match: + print("FAIL =====>", image.feedback_result, predict_result, user_feedback, reviewed_result) def handle(self, *args, **options): test = options['test'] #open csv file with open(test, 'r') as csvfile: reader = csv.reader(csvfile) + index = 0 for row in reader: - self.process_request(SubscriptionRequest.objects.filter(request_id=row[0]), row[3], row[2], row[4], row) + if index != 0: + request = SubscriptionRequest.objects.filter(request_id=row[0]).first() + if not request: + print("Not found ====>", row) + else: + self.process_request(request, row[3], row[2], row[4]) + index += 1 self.stdout.write(self.style.SUCCESS('Sample Django management command executed successfully!'))