# myapp/management/commands/mycustomcommand.py from django.core.management.base import BaseCommand from tqdm import tqdm from fwd_api.models import SubscriptionRequestFile, SubscriptionRequest from fwd_api.utils.accuracy import predict_result_to_ready import traceback import copy import csv class Command(BaseCommand): help = 'Refactor database for image level' 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, 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): 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: # print(f"image.feedback_result[field] is not None is not None - field: {field}") # else: # print("image.feedback_result[field] is None") # if image.feedback_result[field] == user_feedback: # print("image.feedback_result[field] == user_feedback") # else: # print(f"NOT image.feedback_result[field] == user_feedback - field: {field} - image.feedback_result[field]:{image.feedback_result[field]} - user_feedback:{user_feedback}") # if (field == 'imei_number' and len(image.feedback_result[field]) > 0 and image.feedback_result[field][0] == user_feedback): # print("(field == 'imei_number' and len(image.feedback_result[field]) > 0 and image.feedback_result[field][0] == user_feedback)") # else: # print(f"NOT (field == 'imei_number' and len(image.feedback_result[field]) > 0 and image.feedback_result[field][0] == user_feedback) - field: {field}") # 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 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: if not reviewed_result == request_review: request_review[field] = reviewed_result if not user_feedback == request_feedback: request_feedback[field] = user_feedback _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: ", image.feedback_result, "| predict_result: ", predict_result, " | user_feedback: ", user_feedback, "| reviewed_result: ", 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: if index != 0: request = SubscriptionRequest.objects.filter(request_id=row[0]).first() if not request: # print("Not found ====>", row) continue else: self.process_request(request, row[4], row[3], row[5]) index += 1 self.stdout.write(self.style.SUCCESS('Sample Django management command executed successfully!'))