2024-02-06 05:52:22 +00:00
|
|
|
# myapp/management/commands/mycustomcommand.py
|
2024-03-05 08:39:51 +00:00
|
|
|
from fwd_api.constant.common import FileCategory
|
2024-02-06 05:52:22 +00:00
|
|
|
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
|
|
|
|
|
2024-02-28 11:45:10 +00:00
|
|
|
PREDICT_INDEX = 3
|
|
|
|
FEEDBACK_INDEX = 2
|
|
|
|
REVIEWED_INDEX = 4
|
|
|
|
REASON_INDEX = 6
|
|
|
|
COUNTER_INDEX = 9
|
|
|
|
|
2024-02-06 05:52:22 +00:00
|
|
|
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')
|
|
|
|
|
2024-02-28 11:45:10 +00:00
|
|
|
def process_request(self, total, failed, request, predict_result, user_feedback, reviewed_result, reason, counter):
|
2024-02-06 08:28:07 +00:00
|
|
|
if len(request.request_id.split(".")[0].split("_")) < 2:
|
|
|
|
return
|
2024-02-28 11:45:10 +00:00
|
|
|
total[0] += 1
|
2024-02-22 20:58:10 +00:00
|
|
|
|
2024-02-06 08:28:07 +00:00
|
|
|
request_review = copy.deepcopy(request.reviewed_result)
|
|
|
|
|
|
|
|
if not request_review:
|
|
|
|
request_review = {
|
|
|
|
"request_id": request.request_id,
|
|
|
|
"imei_number": [],
|
2024-04-04 07:07:02 +00:00
|
|
|
"retailername": None,
|
|
|
|
"purchase_date": None,
|
|
|
|
"sold_to_party": None,
|
|
|
|
"invoice_no": None
|
2024-02-06 08:28:07 +00:00
|
|
|
}
|
|
|
|
|
2024-03-05 08:39:51 +00:00
|
|
|
images = SubscriptionRequestFile.objects.filter(request=request, file_category=FileCategory.Origin.value)
|
2024-02-06 08:28:07 +00:00
|
|
|
is_match = False
|
|
|
|
try:
|
2024-02-06 05:52:22 +00:00
|
|
|
for i, image in enumerate(images):
|
2024-02-06 08:28:07 +00:00
|
|
|
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}")
|
|
|
|
|
2024-03-29 10:47:28 +00:00
|
|
|
for field in ['retailername', 'purchase_date', 'imei_number', 'invoice_no']:
|
2024-02-06 08:28:07 +00:00
|
|
|
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':
|
2024-02-22 20:58:10 +00:00
|
|
|
if not (reviewed_result in request_review["imei_number"]):
|
2024-02-06 08:28:07 +00:00
|
|
|
request_review["imei_number"].append(reviewed_result)
|
|
|
|
else:
|
2024-02-15 07:36:01 +00:00
|
|
|
if not reviewed_result == request_review[field]:
|
2024-02-06 08:28:07 +00:00
|
|
|
request_review[field] = reviewed_result
|
|
|
|
|
|
|
|
_reviewed_result = copy.deepcopy(request.reviewed_result)
|
|
|
|
|
|
|
|
if not _reviewed_result:
|
|
|
|
_reviewed_result = {
|
2024-02-22 20:58:10 +00:00
|
|
|
"request_id": image.request_id,
|
2024-02-06 08:28:07 +00:00
|
|
|
"imei_number": [],
|
2024-04-04 07:07:02 +00:00
|
|
|
"retailername": None,
|
|
|
|
"purchase_date": None,
|
|
|
|
"sold_to_party": None,
|
|
|
|
"invoice_no": None
|
2024-02-06 08:28:07 +00:00
|
|
|
}
|
2024-02-06 05:52:22 +00:00
|
|
|
|
2024-03-29 10:47:28 +00:00
|
|
|
if image.doc_type == "invoice" and field in ['retailername', 'purchase_date', 'invoice_no']:
|
2024-02-06 08:28:07 +00:00
|
|
|
if _reviewed_result:
|
|
|
|
_reviewed_result[field] = reviewed_result
|
|
|
|
_reviewed_result["imei_number"] = []
|
|
|
|
else:
|
|
|
|
None
|
2024-02-22 20:58:10 +00:00
|
|
|
elif image.doc_type == "imei" and field == "imei_number":
|
2024-02-06 08:28:07 +00:00
|
|
|
_reviewed_result = {
|
|
|
|
"retailername": None,
|
|
|
|
"sold_to_party": None,
|
|
|
|
"purchase_date": None,
|
2024-03-29 10:47:28 +00:00
|
|
|
"imei_number": [reviewed_result],
|
|
|
|
"invoice_no": None
|
2024-02-06 08:28:07 +00:00
|
|
|
} if _reviewed_result else None
|
|
|
|
image.reviewed_result = _reviewed_result
|
2024-02-28 11:45:10 +00:00
|
|
|
if reason:
|
|
|
|
image.reason = reason
|
2024-03-29 10:47:28 +00:00
|
|
|
else:
|
|
|
|
image.reason = ""
|
2024-02-28 11:45:10 +00:00
|
|
|
if counter:
|
|
|
|
image.counter_measures = counter
|
2024-02-06 08:28:07 +00:00
|
|
|
image.save()
|
|
|
|
request.reviewed_result = request_review
|
|
|
|
request.reviewed_result["request_id"] = request.request_id
|
|
|
|
request.is_reviewed = True
|
|
|
|
request.save()
|
2024-02-06 05:52:22 +00:00
|
|
|
|
2024-02-06 08:28:07 +00:00
|
|
|
except Exception as e:
|
|
|
|
self.stdout.write(self.style.ERROR(f"Request: {request.request_id} failed with {e}"))
|
2024-02-28 11:45:10 +00:00
|
|
|
failed[0] += 1
|
2024-02-06 08:28:07 +00:00
|
|
|
print(traceback.format_exc())
|
|
|
|
|
|
|
|
if not is_match:
|
2024-02-28 11:45:10 +00:00
|
|
|
failed[0] += 1
|
2024-02-06 08:28:07 +00:00
|
|
|
print("FAIL =====>", image.feedback_result, predict_result, user_feedback, reviewed_result)
|
2024-02-06 05:52:22 +00:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2024-02-28 11:45:10 +00:00
|
|
|
test = options['test']
|
|
|
|
total = [0]
|
|
|
|
failed = [0]
|
2024-02-06 05:52:22 +00:00
|
|
|
#open csv file
|
|
|
|
with open(test, 'r') as csvfile:
|
|
|
|
reader = csv.reader(csvfile)
|
2024-02-06 08:28:07 +00:00
|
|
|
index = 0
|
2024-02-06 05:52:22 +00:00
|
|
|
for row in reader:
|
2024-02-06 08:28:07 +00:00
|
|
|
if index != 0:
|
|
|
|
request = SubscriptionRequest.objects.filter(request_id=row[0]).first()
|
|
|
|
if not request:
|
|
|
|
print("Not found ====>", row)
|
|
|
|
else:
|
2024-02-28 11:45:10 +00:00
|
|
|
# request, predict_result, user_feedback, reviewed_result, reason, counter
|
|
|
|
self.process_request(total, failed, request, row[PREDICT_INDEX], row[FEEDBACK_INDEX], row[REVIEWED_INDEX], row[REASON_INDEX], row[COUNTER_INDEX])
|
2024-02-06 08:28:07 +00:00
|
|
|
index += 1
|
2024-02-06 05:52:22 +00:00
|
|
|
|
2024-02-28 11:45:10 +00:00
|
|
|
self.stdout.write(self.style.SUCCESS(f"Failed/Total: {failed[0]}/{total[0]}"))
|
2024-02-06 05:52:22 +00:00
|
|
|
self.stdout.write(self.style.SUCCESS('Sample Django management command executed successfully!'))
|
2024-02-28 11:45:10 +00:00
|
|
|
|