def create_pod_file(lr_number, upload_file, user, booking): orig_filename = upload_file.name if isinstance(booking, ManualBooking): serial = get_new_serial(PODFile, booking=booking) new_filename = 'POD-%s-%s.%s' % (booking.booking_id, serial, get_ext(orig_filename)) mb = ManualBooking.objects.get(booking_id=booking.booking_id) mb.pod_status = 'unverified' mb.pod_date = datetime.now() mb.save() elif isinstance(lr_number, LrNumber): serial = get_new_serial(PODFile, lr_number=lr_number) new_filename = 'POD-%s-%s.%s' % (lr_number.lr_number, serial, get_ext(orig_filename)) mb = ManualBooking.objects.get(booking_id=lr_number.booking.booking_id) mb.pod_status = 'unverified' mb.pod_date = datetime.now() mb.save() else: serial = random_id(num_digits=8) new_filename = 'POD-%s-%s.%s' % (serial, serial, get_ext(orig_filename)) s3_upload = s3util.save_to_s3_uploads_pod(new_filename, upload_file) pod_file = PODFile.objects.create( lr_number=lr_number if isinstance(lr_number, LrNumber) else None, serial=serial, s3_upload=s3_upload, uploaded_by=user, booking=booking if isinstance(booking, ManualBooking) else None) return pod_file
def create_pod_file(lr_number, upload_file, user, booking): orig_filename = upload_file.name if isinstance(booking, ManualBooking): serial = get_new_serial(PODFile, booking=booking) new_filename = 'POD-%s-%s.%s' % (slugify(booking.booking_id), serial, get_ext(orig_filename)) mb = ManualBooking.objects.get(booking_id=booking.booking_id) mb.pod_status = 'unverified' mb.pod_date = datetime.now() mb.save() elif isinstance(lr_number, LrNumber): serial = get_new_serial(PODFile, lr_number=lr_number) new_filename = 'POD-%s-%s.%s' % (slugify(lr_number.lr_number), serial, get_ext(orig_filename)) mb = ManualBooking.objects.get(booking_id=lr_number.booking.booking_id) mb.pod_status = 'unverified' mb.pod_date = datetime.now() mb.save() else: serial = random_id(num_digits=8) new_filename = 'POD-%s-%s.%s' % (serial, serial, get_ext(orig_filename)) s3_upload = s3util.save_to_s3_uploads_pod(new_filename, upload_file) podfile_serializer = PODFileSerializer(data={ 'serial': serial, 'lr_number': lr_number.id if isinstance(lr_number, LrNumber) else None, 'booking': booking.id if isinstance(booking, ManualBooking) else None, 'uploaded_by': user.username, 'changed_by': user.username, 's3_upload': s3_upload.id }) if podfile_serializer.is_valid(): pod_file = podfile_serializer.save() print(pod_file) return pod_file return None
def create_beneficiary_account(request): account_number = request.POST.get('account_number') bank = get_or_none(Bank, account_number=account_number) if bank: return render(request=request, template_name='mobile/beneficiary_exists.html', context={'bank': bank}) try: account_document = request.FILES['account_document'] except MultiValueDictKeyError: account_document = None bank = Bank.objects.create( bank=request.POST.get('bank_name'), account_holder_name=request.POST.get('account_holder_name'), beneficiary_code=request.POST.get('beneficiary_code'), account_number=account_number, transaction_type=request.POST.get('transaction_type'), account_type=request.POST.get('account_type'), ifsc=request.POST.get('ifsc'), address=request.POST.get('address'), city=request.POST.get('city'), ) email_add_new_beneficiary(bank=bank) if account_document: filename = '%s.%s' % (account_number, get_ext(account_document.name)) s3util.save_to_s3_uploads_bank_doc(filename, account_document) return json_success_response('success')
def create_vehicle_file(vehicle, document_category, upload_file, user): orig_filename = upload_file.name serial = get_new_serial(VehicleFile, supplier_vehicle=vehicle) new_filename = 'vehicle-%s-%s-%s.%s' % ( document_category.lower(), slugify(vehicle.vehicle_number), serial, get_ext(orig_filename) ) s3_upload = s3util.save_to_s3_uploads_vehicle(new_filename, upload_file) vehicle_file = VehicleFile.objects.create(supplier_vehicle=vehicle, document_category=document_category, serial=serial, s3_upload=s3_upload, uploaded_by=user) return vehicle_file
def create_weighing_slip_file(booking, upload_file, user): orig_filename = upload_file.name serial = get_new_serial(WeighingSlip, booking=booking) new_filename = 'weighing-slip-{}-{}.{}'.format( slugify(booking.booking_id), serial, get_ext(orig_filename) ) s3_upload = s3util.save_to_s3_uploads_weighing_slip(new_filename, upload_file) weighing_slip = WeighingSlip.objects.create(booking=booking, serial=serial,verified=True,is_valid=True, s3_upload=s3_upload, uploaded_by=user) return weighing_slip
def create_driver_file(driver, document_category, upload_file, user): orig_filename = upload_file.name serial = get_new_serial(DriverFile, supplier_driver=driver) new_filename = 'driver-%s-%s-%s-%s.%s' % ( document_category.lower(), slugify(driver.name), slugify(driver.phone), serial, get_ext(orig_filename) ) s3_upload = s3util.save_to_s3_uploads_driver(new_filename, upload_file) driver_file = DriverFile.objects.create(supplier_driver=driver, document_category=document_category, serial=serial, s3_upload=s3_upload, uploaded_by=user) return driver_file
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_cheque_file(customer_name, amount, cheque_number, remarks, upload_file, user, cheque_date): orig_filename = upload_file.name serial = get_new_serial(ChequeFile, customer_name=customer_name) new_filename = 'cheque-%s-%s.%s' % ( slugify(customer_name), serial, get_ext(orig_filename) ) s3_upload = s3util.save_to_s3_payment_cheque(new_filename, upload_file) cheque_file = ChequeFile.objects.create( customer_name=customer_name, amount=amount, cheque_number=cheque_number, remarks=remarks, serial=serial, s3_upload=s3_upload, uploaded_by=user, cheque_date=cheque_date ) return cheque_file
def create_invoice_receipt_file(invoice_number, upload_file, user, invoice_sent_mode, invoice_confirm_mode, invoice_confirm_by_name, invoice_confirm_by_phone): serial = get_new_serial(InvoiceReceiptFile, invoice_number=invoice_number) if upload_file: orig_filename = upload_file.name new_filename = 'inv-receipt-%s-%s.%s' % ( slugify(invoice_number), serial, get_ext(orig_filename) ) s3_upload = s3util.save_to_s3_payment_invoice_receipt(new_filename, upload_file) else: s3_upload = None invoice_receipt_file = InvoiceReceiptFile.objects.create( serial=serial, invoice_number=invoice_number, invoice_receipt=Invoice.objects.filter(invoice_number=invoice_number).last(), s3_upload=s3_upload, uploaded_by=user, invoice_sent_mode=invoice_sent_mode, invoice_confirm_mode=invoice_confirm_mode, invoice_confirm_by_name=invoice_confirm_by_name, invoice_confirm_by_phone=invoice_confirm_by_phone, ) return invoice_receipt_file
def report(report_id): if request.method == 'GET': if report_id is None: reports = Report.query.all() reports = [r.to_dict() for r in reports] return jsonify({ 'data': reports }) else: reports = Report.query.filter_by(id=report_id).first() return jsonify({ 'data': reports.to_dict() }) elif request.method == 'POST': data = request.json print(data) valid, missing = Report.validate_json(data) if not valid: return jsonify({ 'message': '{} not given in request.'.format(', '.join(missing)) }), 422 report = Report(data['user_id'], data['report_type'], data['timestamp'], data['location']['longitude'], data['location']['latitude']) db.session.add(report) db.session.commit() return jsonify(report.id) elif request.method == 'PUT': print(request.headers) if 'video' not in request.files: return jsonify({'message': 'Request does not have a file'}), 422 file = request.files['video'] if file.filename == '': return jsonify({'message': 'Request does not have a file'}), 422 if file and utils.allowed_file(file.filename): ext = utils.get_ext(file.filename) file_id = uuid.uuid4().int filename = str(file_id) + '.' + ext abs_fpath = os.path.join(app.config['UPLOAD_FOLDER'], filename) file.save(abs_fpath) rel_path = os.path.join(os.path.basename(app.config['UPLOAD_FOLDER']), filename) video = Video(path=rel_path, ext=ext) db.session.add(video) db.session.commit() url = os.path.join('video', str(video.id)) video.url = url db.session.commit() report = Report.query.filter_by(id=report_id).first() report.video_id = video.id db.session.commit() if report.report_type == 'hit': pipelines.hit(report, abs_fpath) elif report.report_type == 'witness': pipelines.witness(report, abs_fpath) return jsonify({'message': 'Successfully uploaded video.'}) elif request.method == 'DELETE': report = Report.query.filter_by(id=report_id).first() db.session.delete(report) db.session.commit() return jsonify({ 'message': 'Successfully deleted.' })