Merge pull request #36 from SDSRV-IDP/vietanh99-update-xlsx

Vietanh99 update xlsx
This commit is contained in:
Đỗ Xuân Tân 2024-02-19 09:55:49 +07:00 committed by GitHub Enterprise
commit a5a770fd61
2 changed files with 85 additions and 1 deletions

View File

@ -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}')

View File

@ -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!'))