def partial_update(self, request, pk=None): owner = get_or_none(Owner, id=pk) if not isinstance(owner, Owner): return error_response(status=status.HTTP_400_BAD_REQUEST, msg='Owner Doesnot Exists', data={}) profile = get_or_none(Profile, user=owner.name) parsed_data = parse_update_owner_data(request.data, id=pk) parsed_data["changed_by"] = self.request.user.username if isinstance(profile, Profile) and parsed_data['profile_data']: profile_serializer = ProfileSerializer(instance=profile, data=parsed_data['profile_data'], partial=True) if profile_serializer.is_valid(): profile_serializer.save() else: return error_response( status=status.HTTP_400_BAD_REQUEST, msg='Invalid owner data', data=profile_serializer.errors) else: return error_response(status=status.HTTP_400_BAD_REQUEST, msg='Profile Doesnot Exists', data={}) vehicles_id = parsed_data.pop('vehicles_detail') Vehicle.objects.filter(id__in=vehicles_id).update(owner=owner) Vehicle.objects.exclude(id__in=vehicles_id).filter(owner=owner).update(owner=None) owner_serializer = OwnerSerializer( instance=owner, data=parsed_data, partial=True ) if owner_serializer.is_valid(): owner_serializer.save() return success_response(status=status.HTTP_202_ACCEPTED, msg='Owner updated', data=owner_serializer.data) return error_response(status=status.HTTP_400_BAD_REQUEST, msg='Something went wrong', data=owner_serializer.errors)
def approve(self, request): booking = ManualBooking.objects.get(id=request.data.get('booking_id')) if isinstance(booking, ManualBooking): data = { 'delivered_weight': request.data.get('delivered_weight'), 'loaded_weight': request.data.get('loaded_weight'), 'pod_date': datetime.now(), 'delivery_datetime': request.data.get('delivery_datetime'), 'pod_status': 'completed' } manual_booking_serializer = ManualBookingSerializer( instance=booking, data=data, partial=True) if manual_booking_serializer.is_valid(): manual_booking_serializer.save() for pod in PODFile.objects.filter(booking=booking).exclude( verified=True): pod.is_valid = True pod.verified = True pod.verified_by = request.user pod.verified_datetime = datetime.now() pod.save() S3Upload.objects.filter(id=pod.s3_upload_id).update( is_valid=True, verified=True) booking_unloaded = check_booking_status( booking, 'unloaded') if not booking_unloaded: create_new_booking_status( booking, 'unloaded', User.objects.get(username='******')) else: update_booking_status( booking, 'unloaded', 'in_progress', User.objects.get(username='******')) booking_pod_uploaded = check_booking_status( booking, 'pod_uploaded') if not booking_pod_uploaded: create_new_booking_status( booking, 'pod_uploaded', User.objects.get(username='******')) else: update_booking_status( booking, 'pod_uploaded', 'in_progress', User.objects.get(username='******')) booking_pod_verified = check_booking_status( booking, 'pod_verified') if not booking_pod_verified: create_new_booking_status(booking, 'pod_verified', request.user) else: update_booking_status(booking, 'pod_verified', 'in_progress', request.user) return success_response(status=status.HTTP_200_OK, msg='verified', data={}) return error_response(status=status.HTTP_400_BAD_REQUEST, msg='Something went wrong', data={}) return error_response(status=status.HTTP_400_BAD_REQUEST, msg='Something went wrong', data={})
def web_upload(self, request): lr_number_id = request.data.get('lr_number', None) if not lr_number_id: return error_response(msg='lr_number id not provided', status=400, data={}) if str(lr_number_id).startswith('BROKER'): booking = get_or_none(ManualBooking, booking_id=lr_number_id) lr_obj = None else: lr_obj = get_or_none(LrNumber, id=lr_number_id) booking = None if not isinstance(lr_obj, LrNumber) else lr_obj.booking if not (isinstance(lr_obj, LrNumber) or isinstance(booking, ManualBooking)): return error_response(msg='LrNumber with id=%s not found' % lr_number_id, status=404, data={}) if not request.FILES: return error_response(msg='no file to upload', status=400, data={}) upload_file = request.FILES.get('file') pod_file = create_pod_file(lr_number=lr_obj, upload_file=upload_file, user=User.objects.last(), booking=booking) pod_serializer = PODFileSerializer(pod_file) booking_unloaded = check_booking_status(booking, 'unloaded') if not booking_unloaded: create_new_booking_status(booking, 'unloaded', User.objects.get(username='******')) else: update_booking_status(booking, 'unloaded', 'in_progress', User.objects.get(username='******')) booking_pod_uploaded = check_booking_status(booking, 'pod_uploaded') if not booking_pod_uploaded: create_new_booking_status(booking, 'pod_uploaded', request.user) else: update_booking_status(booking, 'pod_uploaded', 'in_progress', request.user) return Response(status=status.HTTP_200_OK, data={ 'files': [pod_serializer.data], 'Content-Disposition': 'inline; filename=files.json' })
def create(self, request): request.data["created_by"] = self.request.user.username request.data["changed_by"] = self.request.user.username if 'beneficiary_code' in request.data and request.data.get( 'beneficiary_code', None) and len(request.data.get('beneficiary_code', None)) > 3: if Bank.objects.filter(beneficiary_code=request.data.get( 'beneficiary_code')).exists(): return error_response( msg= "Beneficiary Code Already exists, Pls try with other berneficiary code", data={}, status=status.HTTP_400_BAD_REQUEST) if 'account_number' in request.data and request.data.get( 'account_number', None) and len(request.data.get('account_number', None)) > 3: if Bank.objects.filter(account_number=request.data.get( 'account_number')).exists(): return error_response( msg= "A/C No.: {} is already registered, Pls verify account number" .format(request.data.get('account_number')), data={}, status=status.HTTP_400_BAD_REQUEST) bank_serializer = BankSerializer(data=request.data) if bank_serializer.is_valid(): bank = bank_serializer.save() try: account_document = request.FILES['account_document'] except MultiValueDictKeyError: account_document = None if settings.ENABLE_MAIL: if settings.ENABLE_CELERY: tasks.email_add_new_beneficiary.delay(bank_id=bank.id) else: tasks.email_add_new_beneficiary(bank_id=bank.id) if account_document: filename = '%s.%s' % (bank.account_number, get_ext(account_document.name)) # s3util.save_to_s3_uploads_bank_doc(filename, account_document) return success_response( data=bank_serializer.data, msg='Bank account number {} is created'.format( bank.account_number), status=status.HTTP_201_CREATED) return error_response(data=bank_serializer.errors, msg='Something went wrong', status=status.HTTP_400_BAD_REQUEST)
def create(self, request): request.data['user'] = request.user.username data = request.data booking = get_or_none(ManualBooking, booking_id=data['lr_number']) if not isinstance(booking, ManualBooking): lr_number = get_or_none(LrNumber, lr_number=data.get('lr_number', None)) booking = lr_number.booking if isinstance(lr_number.booking, ManualBooking) else None parsed_data = parse_pod_upload_fms(data) for pod in parsed_data: s3_upload_serializer = S3UploadSerializer( data=pod.pop('s3_upload_data')) if s3_upload_serializer.is_valid(): s3_upload = s3_upload_serializer.save() pod['s3_upload'] = s3_upload.id else: return error_response( status=status.HTTP_400_BAD_REQUEST, data=s3_upload_serializer.errors, msg="Something went wrong, Please try later") pod_file_serializer = PODFileSerializer(data=pod) if pod_file_serializer.is_valid(): pod_file_serializer.save() else: return error_response( status=status.HTTP_400_BAD_REQUEST, data=s3_upload_serializer.errors, msg="Something went wrong, Please try later") booking_unloaded = check_booking_status(booking, 'unloaded') if not booking_unloaded: create_new_booking_status(booking, 'unloaded', User.objects.get(username='******')) else: update_booking_status(booking, 'unloaded', 'in_progress', User.objects.get(username='******')) booking_pod_uploaded = check_booking_status(booking, 'pod_uploaded') if not booking_pod_uploaded: create_new_booking_status(booking, 'pod_uploaded', request.user) else: update_booking_status(booking, 'pod_uploaded', 'in_progress', request.user) return success_response(data={}, status=status.HTTP_201_CREATED, msg="POD Files created")
def resubmit(self, request): resubmission_remark = request.data.get('resubmission_remark') booking_id = request.data.get('booking_id') resubmitted_pod = request.data.get('resubmitted_pod') if not resubmission_remark: return error_response(msg="Remarks is mandatory", status=status.HTTP_400_BAD_REQUEST, data={}) if not booking_id: return error_response(msg="Booking id is required", status=status.HTTP_400_BAD_REQUEST, data={}) booking = get_or_none(ManualBooking, booking_id=booking_id) if not isinstance(booking, ManualBooking): return error_response(msg="BAD request", status=status.HTTP_400_BAD_REQUEST, data={}) if not PODFile.objects.filter(booking=booking).exists(): return error_response(msg="BAD Request", status=status.HTTP_400_BAD_REQUEST, data={}) PODFile.objects.filter(id__in=resubmitted_pod).update(verified=False, is_valid=False) PODFile.objects.filter(booking=booking).exclude( id__in=resubmitted_pod).update(verified=True, is_valid=False) booking.pod_status = 'unverified' booking.save() booking_unloaded = check_booking_status(booking, 'unloaded') if booking_unloaded: unloaded_obj = get_booking_status_mapping_object( booking, 'unloaded') if unloaded_obj.created_by == User.objects.get(username='******') and \ unloaded_obj.changed_by == User.objects.get(username='******'): update_booking_status(booking, 'unloaded', 'in_progress', User.objects.get(username='******')) booking_pod_uploaded = check_booking_status(booking, 'pod_uploaded') if not booking_pod_uploaded: create_new_booking_status(booking, 'pod_uploaded', request.user) else: update_booking_status(booking, 'pod_uploaded', 'in_progress', request.user) return success_response(msg="success", status=status.HTTP_200_OK, data={})
def put(self, request, *args, **kwargs): self.object = self.get_object() serializer = ChangePasswordSerializer(data=request.data) if serializer.is_valid(): # Check old password old_password = serializer.data.get("old_password") if not self.object.check_password(old_password): return error_response( data={"old_password": ["Wrong password."]}, status=status.HTTP_400_BAD_REQUEST, msg="Old password is not valid") # set_password also hashes the password that the user will get self.object.set_password(serializer.data.get("new_password")) self.object.save() return success_response(msg='Password updated', status=status.HTTP_200_OK, data={}) return error_response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST, msg='Something went wrong, Plz try later')
def post(self, request): token = request.data.get('token') token_obj = get_or_none(Token, key=token) if isinstance(token_obj, Token) and isinstance(token_obj.user, User): user_serializer = UserSerializer(token_obj.user) return Response( { 'status': 'success', 'msg': 'Login Successful', 'token': token_obj.key, 'data': user_serializer.data, 'status_code': status.HTTP_200_OK }, status=status.HTTP_200_OK) return error_response(status=status.HTTP_400_BAD_REQUEST, msg='User doesnot exits', data={})
def reject(self, request): booking = ManualBooking.objects.get(id=request.data.get('booking_id')) if not isinstance(booking, ManualBooking): return error_response(status=status.HTTP_400_BAD_REQUEST, msg='Booking id is not valid', data={}) booking.pod_status = 'rejected' booking.save() for lr in booking.lr_numbers.all(): RejectedPOD.objects.create( booking=booking, lr=lr, remarks=request.data.get('rejection_remark'), rejected_by=request.user) if not booking.lr_numbers.exists(): RejectedPOD.objects.create( booking=booking, remarks=request.data.get('rejection_remark'), rejected_by=request.user) for pod in PODFile.objects.filter(booking=booking).exclude( verified=True): pod.is_valid = False pod.verified = True pod.verified_by = request.user pod.verified_datetime = datetime.now() pod.save() S3Upload.objects.filter(id=pod.s3_upload_id).update(is_valid=False, verified=True) booking_unloaded = check_booking_status(booking, 'unloaded') booking_pod_uploaded = check_booking_status( booking, 'pod_uploaded') if booking_unloaded: unloaded_obj = get_booking_status_mapping_object( booking, 'unloaded') if unloaded_obj.created_by == User.objects.get(username='******') and \ unloaded_obj.changed_by == User.objects.get(username='******'): update_booking_status(booking, 'unloaded', 'reverted', User.objects.get(username='******')) if booking_pod_uploaded: update_booking_status(booking, 'pod_uploaded', 'reverted', request.user) return success_response(msg='POD for booking ID {} is rejected'.format( booking.booking_id), status=status.HTTP_200_OK, data={})
def create(self, request, pk=None): request.data['vehicle_number'] = compare_format(request.data['vehicle_number']) if "created_by" not in request.data: request.data["created_by"] = self.request.user.username if "changed_by" not in request.data: request.data["changed_by"] = self.request.user.username if request.data.get('gps_enabled', None) == 'yes': request.data['gps_enabled'] = True else: request.data['gps_enabled'] = False if request.data.get('registration_year', None) and 'registration_year' in request.data: request.data['registration_year'] = '01-Jan-{}'.format(request.data['registration_year'].strip()) vehicle_serializer = VehicleSerializer(data=request.data) if vehicle_serializer.is_valid(): vehicle_serializer.save() return success_response(status=status.HTTP_201_CREATED, msg="Vehicle Created", data=vehicle_serializer.data) return error_response(status=status.HTTP_400_BAD_REQUEST, msg="Vehicle not Created", data=vehicle_serializer.errors)
def create(self, request): invoice = get_or_none(Invoice, invoice_number=request.data.get( 'invoice_number', -1)) if not isinstance(invoice, Invoice): return error_response(msg="Bad request", status=status.HTTP_400_BAD_REQUEST, data={}) request.data["created_by"] = self.request.user.username request.data["changed_by"] = self.request.user.username request.data["uploaded_by"] = self.request.user.username request.data["invoice_receipt"] = invoice.id request.data["serial"] = generate_random_string(N=20) invoice_receipt_file_serializer = InvoiceReceiptFileSerializer( data=request.data) if invoice_receipt_file_serializer.is_valid(): invoice_receipt_file_serializer.save() invoice_sent_mode = request.data.get('invoice_sent_mode', None) invoice_confirm_mode = request.data.get('invoice_confirm_mode', None) invoice_confirm_by_name = request.data.get( 'invoice_confirm_by_name', None) invoice_confirm_by_phone = request.data.get( 'invoice_confirm_by_phone', None) INVOICE_SENT_MODE_CHOICES = (('CR', 'Courier'), ('HD', 'Hand Delivered'), ('EM', 'Email Screenshot')) INVOICE_CONFIRM_MODE_CHOICES = (('PH', 'Phone'), ('WA', 'Written Acknowledgement'), ('EM', 'Email Screenshot')) if invoice_sent_mode: if invoice_sent_mode not in [ x[0] for x in INVOICE_SENT_MODE_CHOICES ]: return error_response(msg='Invalid Invoice Sent Mode', status=400, data={}) if invoice_confirm_mode: if invoice_confirm_mode not in [ x[0] for x in INVOICE_CONFIRM_MODE_CHOICES ]: return error_response(msg='Invalid Invoice Confirm Mode', status=400, data={}) if invoice_confirm_mode == 'PH' and not invoice_confirm_by_name and not invoice_confirm_by_phone: return error_response( msg='Invoice Confirm Name and Phone required', status=400, data={}) if not invoice_sent_mode and not invoice_confirm_mode: return error_response( msg='Invoice Sent or Confirm Mode Required', status=status.HTTP_400_BAD_REQUEST, data={}) if invoice_sent_mode: update_invoice_booking_status(invoice.id, 'party_invoice_sent', request.user) if invoice_confirm_mode: update_invoice_booking_status(invoice.id, 'invoice_confirmed', request.user) return success_response(data=invoice_receipt_file_serializer.data, status=status.HTTP_201_CREATED, msg="InvoiceReceiptFile Created") return error_response(msg="InvoiceReceiptFile not Created", data=invoice_receipt_file_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def fms_update(self, request, pk): vehicle_id = pk if vehicle_id and vehicle_id.isnumeric(): vehicle = get_or_none(Vehicle, id=int_or_none(vehicle_id)) if not isinstance(vehicle, Vehicle): return error_response(msg='Vehicle with id=%s does not exist' % vehicle_id, status=status.HTTP_404_NOT_FOUND, data=None) if request.data.get('gps_enabled', None) == 'yes': request.data['gps_enabled'] = True else: request.data['gps_enabled'] = False data = request.data parsed_data = parse_vehicle_docs(request.data) request.data["changed_by"] = self.request.user.username vehicle = get_or_none(Vehicle, id=pk) vehicle_serializers = VehicleSerializer( instance=vehicle, data=parsed_data, partial=True ) if vehicle_serializers.is_valid(): vehicle_serializers.save() owner = vehicle.owner if isinstance(vehicle.owner, Owner) else None driver = vehicle.driver if isinstance(vehicle.driver, Driver) else None if isinstance(owner, Owner) and parsed_data.get('owner_data', None): owner_serializer = OwnerSerializer( instance=owner, data=parsed_data.get('owner_data', None), partial=True ) if owner_serializer.is_valid(): owner_serializer.save() else: pass if isinstance(driver, Driver) and parsed_data.get('driver_data', None): driver_serializer = DriverSerializer( instance=driver, data=parsed_data.get('driver_data', None), partial=True ) if driver_serializer.is_valid(): driver_serializer.save() doc_key = 'rc_doc' if doc_key in data and data[doc_key].get('url'): if not VehicleFile.objects.filter( Q(s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None) | Q( s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None)).exists(): if S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).exists(): s3_upload = S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).last() else: s3_upload = s3util.get_or_create_s3_upload( bucket=data[doc_key].get('bucketName', None), folder=data[doc_key].get('folderName', None), filename=data[doc_key].get('fileName', None), verified=False, is_valid=False, uuid=data[doc_key].get('uuid', None), ) VehicleFile.objects.create( uploaded_by=request.user, vehicle=vehicle, document_category='REG', s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None, s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None, serial=get_new_serial(VehicleFile, vehicle=vehicle), s3_upload=s3_upload ) doc_key = 'permit_doc' if doc_key in data and data[doc_key].get('url'): if not VehicleFile.objects.filter( Q(s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None) | Q( s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None)).exists(): if S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).exists(): s3_upload = S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).last() else: s3_upload = s3util.get_or_create_s3_upload( bucket=data[doc_key].get('bucketName', None), folder=data[doc_key].get('folderName', None), filename=data[doc_key].get('fileName', None), verified=False, is_valid=False, uuid=data[doc_key].get('uuid', None), ) VehicleFile.objects.create( uploaded_by=request.user, vehicle=vehicle, document_category='PERM', s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None, s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None, serial=get_new_serial(VehicleFile, vehicle=vehicle), s3_upload=s3_upload ) doc_key = 'insurance_doc' if doc_key in data and data[doc_key].get('url'): if not VehicleFile.objects.filter( Q(s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None) | Q( s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None)).exists(): if S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).exists(): s3_upload = S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).last() else: s3_upload = s3util.get_or_create_s3_upload( bucket=data[doc_key].get('bucketName', None), folder=data[doc_key].get('folderName', None), filename=data[doc_key].get('fileName', None), verified=False, is_valid=False, uuid=data[doc_key].get('uuid', None), ) VehicleFile.objects.create( uploaded_by=request.user, vehicle=vehicle, document_category='INS', s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None, s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None, serial=get_new_serial(VehicleFile, vehicle=vehicle), s3_upload=s3_upload ) doc_key = 'fitness_doc' if doc_key in data and data[doc_key].get('url'): if not VehicleFile.objects.filter( Q(s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None) | Q( s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None)).exists(): if S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).exists(): s3_upload = S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).last() else: s3_upload = s3util.get_or_create_s3_upload( bucket=data[doc_key].get('bucketName', None), folder=data[doc_key].get('folderName', None), filename=data[doc_key].get('fileName', None), verified=False, is_valid=False, uuid=data[doc_key].get('uuid', None), ) VehicleFile.objects.create( uploaded_by=request.user, vehicle=vehicle, document_category='FIT', s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None, s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None, serial=get_new_serial(VehicleFile, vehicle=vehicle), s3_upload=s3_upload ) doc_key = 'puc_doc' if doc_key in data and data[doc_key].get('url'): if not VehicleFile.objects.filter( Q(s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None) | Q( s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None)).exists(): if S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).exists(): s3_upload = S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).last() else: s3_upload = s3util.get_or_create_s3_upload( bucket=data[doc_key].get('bucketName', None), folder=data[doc_key].get('folderName', None), filename=data[doc_key].get('fileName', None), verified=False, is_valid=False, uuid=data[doc_key].get('uuid', None), ) VehicleFile.objects.create( uploaded_by=request.user, vehicle=vehicle, document_category='PUC', s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None, s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None, serial=get_new_serial(VehicleFile, vehicle=vehicle), s3_upload=s3_upload, ) doc_key = 'owner_pan_doc' if doc_key in data and vehicle.owner and data[doc_key].get('url'): if not OwnerFile.objects.filter( Q(s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None) | Q( s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None)).exists(): if S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).exists(): s3_upload = S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).last() else: s3_upload = s3util.get_or_create_s3_upload( bucket=data[doc_key].get('bucketName', None), folder=data[doc_key].get('folderName', None), filename=data[doc_key].get('fileName', None), verified=False, is_valid=False, uuid=data[doc_key].get('uuid', None), ) OwnerFile.objects.create( uploaded_by=request.user, owner=owner, document_category='PAN', s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None, s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None, serial=get_new_serial(OwnerFile, owner=owner), s3_upload=s3_upload ) doc_key = 'owner_dec_doc' if doc_key in data and vehicle.owner and data[doc_key].get('url'): if not OwnerFile.objects.filter( Q(s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None) | Q( s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None)).exists(): if S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).exists(): s3_upload = S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).last() else: s3_upload = s3util.get_or_create_s3_upload( bucket=data[doc_key].get('bucketName', None), folder=data[doc_key].get('folderName', None), filename=data[doc_key].get('fileName', None), verified=False, is_valid=False, uuid=data[doc_key].get('uuid', None), ) OwnerFile.objects.create( uploaded_by=request.user, owner=owner, document_category='DEC', s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None, s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None, serial=get_new_serial(OwnerFile, owner=owner), s3_upload=s3_upload, ) doc_key = 'driver_dl_doc' if doc_key in data and vehicle.driver and data[doc_key].get('url'): if not DriverFile.objects.filter( Q(s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None) | Q( s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None)).exists(): if S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).exists(): s3_upload = S3Upload.objects.filter(uuid=data[doc_key].get('uuid', None)).last() else: s3_upload = s3util.get_or_create_s3_upload( bucket=data[doc_key].get('bucketName', None), folder=data[doc_key].get('folderName', None), filename=data[doc_key].get('fileName', None), verified=False, is_valid=False, uuid=data[doc_key].get('uuid', None), ) DriverFile.objects.create( uploaded_by=request.user, driver=driver, document_category='DL', s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None, s3_thumb_url=data[doc_key].get('thumb_url') if validate_url( data[doc_key].get('thumb_url')) else None, serial=get_new_serial(DriverFile, driver=driver), s3_upload=s3_upload, ) vehicle = get_or_none(Vehicle, id=pk) vehicle_serializer = FMSVehicleSerializer(instance=vehicle) return success_response( msg='vehicle details %s' % ('edited' if vehicle_id else 'saved'), data=vehicle_serializer.data, status=status.HTTP_202_ACCEPTED )
def fms_partial_update(self, request, pk=None): data = request.data parsed_data = parse_profile_data(data=data) supplier = get_or_none(Broker, name=request.user) profile = get_or_none(Profile, user=request.user) if (not isinstance(supplier, Supplier)) and not isinstance( profile, Profile): return error_response(msg="Something went wrong, pls try later", status=status.HTTP_404_NOT_FOUND, data={}) if parsed_data['profile_data'] and isinstance(profile, Profile): profile_serializer = ProfileSerializer( instance=profile, data=parsed_data['profile_data'], partial=True) if profile_serializer.is_valid(): profile_serializer.save() else: return error_response(status=status.HTTP_400_BAD_REQUEST, data=profile_serializer.errors, msg='Error') if parsed_data['broker_data'] and isinstance(supplier, Broker): broker_serializer = BrokerSerializer( instance=supplier, data=parsed_data['broker_data'], partial=True) if broker_serializer.is_valid(): broker_serializer.save() else: return error_response(status=status.HTTP_400_BAD_REQUEST, data=broker_serializer.errors, msg='Error') doc_key = 'pan_doc' if doc_key in data and supplier and data[doc_key].get('url'): if not OwnerFile.objects.filter( Q(s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None) | Q(s3_thumb_url=data[doc_key].get('thumb_url') if validate_url(data[doc_key].get('thumb_url')) else None) ).exists(): if S3Upload.objects.filter( uuid=data[doc_key].get('uuid', None)).exists(): s3_upload = S3Upload.objects.filter( uuid=data[doc_key].get('uuid', None)).last() else: s3_upload = s3util.get_or_create_s3_upload( bucket=data[doc_key].get('bucketName', None), folder=data[doc_key].get('folderName', None), filename=data[doc_key].get('fileName', None), verified=False, is_valid=False, uuid=data[doc_key].get('uuid', None), ) OwnerFile.objects.create( uploaded_by=request.user, supplier=supplier, document_category='PAN', s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None, s3_thumb_url=data[doc_key].get('thumb_url') if validate_url(data[doc_key].get('thumb_url')) else None, serial=get_new_serial(OwnerFile, supplier=supplier), s3_upload=s3_upload) doc_key = 'dec_doc' if doc_key in data and supplier and data[doc_key].get('url'): if not OwnerFile.objects.filter( Q(s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None) | Q(s3_thumb_url=data[doc_key].get('thumb_url') if validate_url(data[doc_key].get('thumb_url')) else None) ).exists(): if S3Upload.objects.filter( uuid=data[doc_key].get('uuid', None)).exists(): s3_upload = S3Upload.objects.filter( uuid=data[doc_key].get('uuid', None)).last() else: s3_upload = s3util.get_or_create_s3_upload( bucket=data[doc_key].get('bucketName', None), folder=data[doc_key].get('folderName', None), filename=data[doc_key].get('fileName', None), verified=False, is_valid=False, uuid=data[doc_key].get('uuid', None), ) OwnerFile.objects.create( uploaded_by=request.user, supplier=supplier, document_category='DEC', s3_url=data[doc_key].get('url') if validate_url(data[doc_key].get('url')) else None, s3_thumb_url=data[doc_key].get('thumb_url') if validate_url(data[doc_key].get('thumb_url')) else None, serial=get_new_serial(OwnerFile, supplier=supplier), s3_upload=s3_upload, ) return success_response(msg='profile edited', data={}, status=status.HTTP_200_OK)