84 lines
3.5 KiB
Python
84 lines
3.5 KiB
Python
# 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!'))
|