def create_order(request, id): offer = Offer.objects.get(id=id) if request.method == "POST": form = CreateOrderForm(request.POST) if form.is_valid(): order_number = request.POST['order_number'] order = Order() order.order_number = order_number order.copy_from(offer) #Archive the offer offer.archived = True offer.save() return redirect('app.orders.views.order.view', order.id) else: form = CreateOrderForm() return render(request, "offers/create_order.html", {'title': offer.title, 'next_order_number': Order.calculate_next_order_number(), 'offer': offer, 'form': form, })
def create_order(): form = OrderCreateForm() if form.validate(): user_id = form.data.get('user_id') o = Order.query.filter( Order.is_canceled == False, Order.customer_id == user_id, Order.driver_id == None ).first() if o: return ujsonify({'errors': 'already took taxi'}) lat = form.data['lat'] lon = form.data['lon'] time = form.data['time'] or datetime.utcnow() time = time if time > datetime.utcnow() else datetime.utcnow() o = Order() o.customer_id = user_id o.order_time = time db.session.add(o) db.session.commit() scheduler.enqueue_at(time, taxi_order_submit, o.id, user_id, lat, lon) logging.debug('{order_id} JOB AT CREATED order at: {time}'.format( order_id=o.id, time=time )) return ujsonify(o.get_public()) return ujsonify({'errors': form.errors}), 400
def test_get_order_product_status_history(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' subcustomer = Subcustomer() order = Order(id=gen_id, user_id=self.user.id, status='pending', country_id='c1', tracking_id='T00', tracking_url='https://tracking.fake/T00') suborder = Suborder(order=order, subcustomer=subcustomer) self.try_add_entities([ order, subcustomer, suborder, OrderProduct(id=10, suborder=suborder, product_id='0000', price=10, quantity=10), OrderProductStatusEntry(order_product_id=10, user_id=self.admin.id, when_created=datetime(2020, 1, 1, 1, 0, 0), status=OrderProductStatus.pending) ]) res = self.try_user_operation( lambda: self.client.get('/api/v1/order/product/10/status/history')) res = self.client.get('/api/v1/order/product/10/status/history') self.assertEqual(res.json, [{ 'when_created': '2020-01-01 01:00:00', 'set_by': 'root_test_orders_api', 'status': 'pending' }]) res = self.client.get('/api/v1/order/product/30/status/history') self.assertEqual(res.status_code, 404)
def test_save_order_add_suborder(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' self.try_add_entities([Order(id=gen_id, user=self.user)]) res = self.try_admin_operation( admin_only=True, operation=lambda: self.client.post(f'/api/v1/order/{gen_id}', json={ 'address': 'Address1', 'country': 'c1', 'customer_name': "Customer1", 'phone': '1', 'zip': '1', 'shipping': '1', 'suborders': [{ 'subcustomer': 'test, test, test', 'items': [{ 'item_code': '0000', 'quantity': 1 }] }] })) self.assertEqual(res.status_code, 200)
def test_save_order(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' self.try_add_entities([Order(id=gen_id, user=self.user)]) res = self.try_admin_operation( admin_only=True, operation=lambda: self.client.post( f'/api/v1/order/{gen_id}', json={ 'address': 'Address1', 'country': 'c1', 'customer_name': "Customer1", 'phone': '1', 'zip': '1', 'shipping': '1', "suborders": [{ "subcustomer": "A000, Subcustomer1, P@ssw0rd", "items": [{ "item_code": "0000", "quantity": "100" }] }] })) self.assertEqual(res.status_code, 200)
def setUp(self): super().setUp() db.create_all() admin_role = Role(id=10, name='admin') self.try_add_entities([ User( id=0, username='******', email='*****@*****.**', password_hash= 'pbkdf2:sha256:150000$bwYY0rIO$320d11e791b3a0f1d0742038ceebf879b8182898cbefee7bf0e55b9c9e9e5576', enabled=True, roles=[admin_role]), User( id=10, username='******', email='*****@*****.**', password_hash= 'pbkdf2:sha256:150000$bwYY0rIO$320d11e791b3a0f1d0742038ceebf879b8182898cbefee7bf0e55b9c9e9e5576', enabled=True), User( id=20, username='******', email='*****@*****.**', password_hash= 'pbkdf2:sha256:150000$bwYY0rIO$320d11e791b3a0f1d0742038ceebf879b8182898cbefee7bf0e55b9c9e9e5576', enabled=True), admin_role, Order() ])
def test_finish_order_with_unfinished_products(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' self.try_add_entities([ Product(id='0001', name='Product 1', price=10, weight=10), Product(id='0002', name='Product 2', price=10, weight=10), Product(id='0003', name='Product 3', price=10, weight=10), Order(id=gen_id) ]) self.try_add_entities([ Suborder(id=gen_id, order_id=gen_id), OrderProduct(suborder_id=gen_id, product_id='0001', quantity=1, status=OrderProductStatus.pending), OrderProduct(suborder_id=gen_id, product_id='0002', quantity=1, status=OrderProductStatus.pending), OrderProduct(suborder_id=gen_id, product_id='0003', quantity=1, status=OrderProductStatus.pending) ]) res = self.try_admin_operation(lambda: self.client.post( f'/api/v1/admin/order/{gen_id}', json={'status': 'shipped'})) self.assertEqual(res.status_code, 409)
def test_set_order_product_unavailable(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' op_id = datetime.now().microsecond order = Order(id=gen_id, user=self.user) self.try_add_entities([ Product(id='0000', name='Test product', price=10, weight=10), Product(id='0001', name='Test product 1', price=10, weight=10) ]) self.try_add_entities([ order, Suborder(id=op_id, order=order), OrderProduct(id=op_id, suborder_id=op_id, product_id='0000', price=10, quantity=10), OrderProduct(id=op_id + 1, suborder_id=op_id, product_id='0001', price=10, quantity=1) ]) self.try_admin_operation(lambda: self.client.post( f'/api/v1/admin/order/product/{op_id + 1}/status/unavailable')) order = Order.query.get(gen_id) self.assertEqual(order.subtotal_krw, 2600)
def test_postpone_order_product(self): op_id = datetime.now().microsecond self.try_add_entities([ Product(id='0000', name='Test product', price=10, weight=10), Product(id='0001', name='Test product 1', price=10, weight=10), Country(id='c1', name='country1'), Shipping(id=1, name='Shipping1'), PostponeShipping(), ShippingRate(shipping_method_id=1, destination='c1', weight=1000, rate=100), ShippingRate(shipping_method_id=1, destination='c1', weight=10000, rate=200), ]) order1 = Order(user=self.user, shipping_method_id=1, country_id='c1') self.try_add_entity(order1) order2 = Order(user=self.user, shipping_method_id=1, country_id='c1') self.try_add_entity(order2) suborder = Suborder(order=order1, subcustomer=Subcustomer()) suborder1 = Suborder(order=order2, subcustomer=Subcustomer()) self.try_add_entities([ OrderProduct(suborder=suborder, product_id='0000', quantity=1), OrderProduct(id=op_id, suborder=suborder, product_id='0001', quantity=1), OrderProduct(id=op_id + 1, suborder=suborder1, product_id='0001', quantity=1) ]) res = self.try_user_operation(lambda: self.client.post( f'/api/v1/order/product/{op_id}/postpone')) self.assertEqual(res.status_code, 200) orders = Order.query.all() self.assertEqual(len(orders), 3) self.assertEqual(orders[0].total_krw, 2610) self.assertEqual(orders[2].total_krw, 2510) self.client.post(f'/api/v1/order/product/{op_id + 1}/postpone') orders = Order.query.all() self.assertEqual(len(orders), 3) self.assertEqual(orders[0].total_krw, 2610) self.assertEqual(orders[2].total_krw, 5020)
def order(app): with app.app_context(): order = Order(id="OI0", customer_id=1, order_status_code="OS1", order_details="En proceso") db.session.add(order) db.session.commit() return order
def test_get_invoice_excel(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' self.try_add_entities([ Product(id=gen_id, weight=1), Invoice(id=gen_id, country_id='c1'), Order(id=gen_id, invoice_id=gen_id, country_id='c1'), InvoiceItem(invoice_id=gen_id, product_id=gen_id, price=1, quantity=1) ]) self.try_admin_operation( lambda: self.client.get(f'/api/v1/admin/invoice/{gen_id}/excel'))
def test_get_orders_to_attach(self): postpone_shipping = PostponeShipping() self.try_add_entities([ postpone_shipping, Order(shipping=postpone_shipping, user=self.user, status=OrderStatus.pending) ]) res = self.try_user_operation( lambda: self.client.get('/api/v1/order?to_attach')) self.assertEqual(len(res.json), 1)
def test_get_invoice_cumulative_excel(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' self.try_add_entities([ Product(id=gen_id, weight=1), Invoice(id=gen_id, country_id='c1'), Order(id=gen_id, invoice_id=gen_id, country_id='c1'), InvoiceItem(invoice_id=gen_id, product_id=gen_id, price=1, quantity=1) ]) res = self.try_admin_operation( lambda: self.client.get(f'/api/v1/admin/invoice/excel?invoices={gen_id}&invoices={gen_id}')) self.assertTrue(res.status_code, 200)
def form(request, id=None): products = [] if id: instance = get_object_or_404(Order, id=id) products.extend(instance.product_lines.all()) order_number = instance.order_number else: instance = Order() order_number = Order.calculate_next_order_number() if request.method == "POST": form = OrderForm(request.POST, instance=instance) products = [] i = 0 for p in request.POST.getlist('product_number'): p = ProductLine() p.description = request.POST.getlist('product_description')[i] p.price = request.POST.getlist('product_unit_cost')[i] p.count = request.POST.getlist('product_qty')[i] p.tax = request.POST.getlist('product_tax')[i] try: p.product = Product.objects.get( id=int(request.POST.getlist('product_number')[i])) except Exception, e: p.product = None products.append(p) i += 1 if form.is_valid(): o = form.save(commit=False) o.save(no_allemployee_group_permissions=True) o.update_products(products) request.message_success(_("Successfully saved order")) return redirect(view, o.id)
def test_get_purchase_orders(self): order = Order() suborder = Suborder(order=order) po = PurchaseOrder(suborder=suborder) self.try_add_entities([ order, suborder, po, ]) self.try_admin_operation( lambda: self.client.get(f"/api/v1/admin/purchase/order/{po.id}"))
def create_order(request, id): offer = Offer.objects.get(id=id) if request.method == "POST": form = CreateOrderForm(request.POST) if form.is_valid(): order_number = request.POST['order_number'] order = Order() order.order_number = order_number order.copy_from(offer) #Archive the offer offer.archived = True offer.save() return redirect('app.orders.views.order.view', order.id) else: form = CreateOrderForm() return render( request, "offers/create_order.html", { 'title': offer.title, 'next_order_number': Order.calculate_next_order_number(), 'offer': offer, 'form': form, })
def test_delete_order(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' order = Order(id=gen_id, user=self.user, status=OrderStatus.pending) order1 = Order(id=gen_id + '1', user=self.user, status=OrderStatus.shipped) order2 = Order(id=gen_id + '2', user=self.user) suborder = Suborder(order=order) self.try_add_entities([ Order(user=self.user), order, order1, order2, suborder, OrderProduct(suborder=suborder, product_id='0000'), OrderProduct(suborder=suborder, product_id='0000') ]) res = self.try_admin_operation( lambda: self.client.delete(f'/api/v1/admin/order/{gen_id}')) self.assertEqual(res.status_code, 200) self.assertEqual(Order.query.count(), 3) res = self.client.delete(f'/api/v1/admin/order/{order1.id}') self.assertEqual(res.status_code, 409) self.assertEqual(Order.query.count(), 3) res = self.client.delete(f'/api/v1/admin/order/{order2.id}') self.assertEqual(res.status_code, 200) self.assertEqual(Order.query.count(), 2)
def test_get_order_products(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' subcustomer = Subcustomer() order = Order(id=gen_id, user_id=self.user.id, status=OrderStatus.pending, country_id='c1', shipping_method_id=1, tracking_id='T00', tracking_url='https://tracking.fake/T00') suborder = Suborder(order=order, subcustomer=subcustomer) self.try_add_entities([order, subcustomer, suborder]) res = self.try_user_operation( lambda: self.client.get('/api/v1/admin/order/product'))
def test_get_order(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' order = Order(id=gen_id, user=self.user) suborder = Suborder(order=order) self.try_add_entities([Product(id=gen_id, price=10, weight=10)]) self.try_add_entities([ order, suborder, OrderProduct(suborder=suborder, product_id=gen_id, price=10, quantity=10), Order(id=gen_id + '1', user=self.user, status=OrderStatus.pending) ]) res = self.try_user_operation( lambda: self.client.get(f'/api/v1/order/{gen_id}')) self.assertEqual(res.json['total'], 2600) self.assertEqual(res.json['total_rur'], 1300.0) self.assertEqual(res.json['total_usd'], 1300.0) self.assertEqual(res.json['user'], self.user.username) self.assertEqual(len(res.json['order_products']), 1) res = self.client.get('/api/v1/order') self.assertEqual(len(res.json), 2) res = self.client.get('/api/v1/order?status=pending') self.assertEqual(res.json[0]['status'], 'pending')
def test_create_purchase_order(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' gen_int_id = int(datetime.now().timestamp()) self.try_add_entities([ Order(id=gen_id, user=self.user, status=OrderStatus.can_be_paid), Subcustomer(id=gen_int_id), Subcustomer(id=gen_int_id + 1), Suborder(id=gen_id, order_id=gen_id, subcustomer_id=gen_int_id), Suborder(id=gen_id + '1', order_id=gen_id, subcustomer_id=gen_int_id + 1), OrderProduct(suborder_id=gen_id, product_id='0000'), OrderProduct(suborder_id=gen_id + '1', product_id='0000'), Address(id=gen_int_id, zip='00000'), Company(id=gen_int_id, address_id=gen_int_id) ])
def test_pay_order(self): self.user.balance = 2600 order = Order(user=self.user, status=OrderStatus.pending) suborder = Suborder(order=order) self.try_add_entities([ order, suborder, OrderProduct(suborder=suborder, product_id='0000', price=10, quantity=10, status='purchased') ]) res = self.try_admin_operation(lambda: self.client.post( f'/api/v1/admin/order/{order.id}', json={'status': 'shipped'})) self.assertEqual(res.status_code, 200) self.assertEqual(self.user.balance, 0)
def test_get_order_excel(self): order = Order(user=self.user, status=OrderStatus.shipped, when_created=datetime.now(), country_id='c1') suborder = Suborder(order=order, subcustomer=Subcustomer(username='******')) self.try_add_entities([ order, suborder, OrderProduct(suborder=suborder, product_id='0000', price=10, quantity=10) ]) res = self.try_user_operation( lambda: self.client.get(f'/api/v1/order/{order.id}/excel')) self.assertEqual(res.status_code, 200)
def test_set_order_product_status(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' op_id = datetime.now().microsecond order = Order(id=gen_id, user=self.user) self.try_add_entities( [Product(id='0000', name='Test product', price=10, weight=10)]) self.try_add_entities([ order, Suborder(id=op_id, order=order), OrderProduct(id=op_id, suborder_id=op_id, product_id='0000', price=10, quantity=10) ]) self.try_user_operation(lambda: self.client.post( f'/api/v1/admin/order/product/{op_id}/status/pending'))
def test_create_invoice_item(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' order = Order(id=gen_id) self.try_add_entities([ Product(id=gen_id, name='Product 1') ]) self.try_add_entities([ order, Suborder(id=gen_id, order=order), OrderProduct(suborder_id=gen_id, product_id=gen_id, price=10, quantity=10), Invoice(id=gen_id, order_id=gen_id) ]) self.try_admin_operation( lambda: self.client.post(f'/api/v1/admin/invoice/{gen_id}/item/new') ) res = self.client.post(f'/api/v1/admin/invoice/{gen_id}/item/new', json={'invoice_id': gen_id, 'product_id': gen_id, 'price': 10, 'quantity': 10}) self.assertTrue(res.status_code, 200)
def test_get_orders(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' order = Order(id=gen_id, user=self.user, country_id='c1', shipping_method_id=1) suborder = Suborder(order=order) self.try_add_entities([ order, suborder, OrderProduct(id=10, suborder=suborder, product_id='0000', price=10, quantity=10) ]) res = self.try_user_operation(lambda: self.client.get('/api/v1/order')) self.assertEqual(res.json[0]['total'], 2700)
def test_get_old_invoice(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' self.try_add_entities([ Product(id=gen_id, name='Product 1', weight=10), Invoice(id=gen_id, country_id='c1', when_created=datetime(2020, 1, 1, 1, 0, 0), when_changed=datetime(2020, 1, 1, 1, 0, 0)), Order(id=gen_id, invoice_id=gen_id, country_id='c1') ]) suborder = Suborder(order_id=gen_id) self.try_add_entities([ suborder, OrderProduct(suborder=suborder, product_id=gen_id, price=10, quantity=1) ]) res = self.try_admin_operation( lambda: self.client.get(f'/api/v1/admin/invoice/{gen_id}')) self.assertEqual(res.status_code, 200) self.assertEqual(len(res.json), 1) self.assertEqual(res.json[0], { 'address': None, 'country': 'country1', 'customer': None, 'payee': None, 'id': gen_id, 'invoice_items': [{ 'id': 1, 'invoice_id': gen_id, 'product_id': gen_id, 'product': 'Product 1', 'price': 5.0, 'weight': 10, 'quantity': 1, 'subtotal': 5.0, 'when_created': None, 'when_changed': None }], 'orders': [gen_id], 'phone': None, 'total': 5.0, 'weight': 10, 'when_changed': '2020-01-01 01:00:00', 'when_created': '2020-01-01 01:00:00' })
def test_finish_order_with_unavailable_products(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' self.try_add_entities([ Product(id='0001', name='Product 1', price=10, weight=10, points=10), Product(id='0002', name='Product 2', price=10, weight=10, points=10), Product(id='0003', name='Product 3', price=10, weight=10, points=10), Order(id=gen_id, country_id='c1', shipping_method_id=1, user=self.user) ]) self.try_add_entities([ Suborder(id=gen_id, order_id=gen_id), OrderProduct(suborder_id=gen_id, product_id='0001', quantity=1, status=OrderProductStatus.purchased), OrderProduct(suborder_id=gen_id, product_id='0002', quantity=1, status=OrderProductStatus.purchased), OrderProduct(suborder_id=gen_id, product_id='0003', quantity=1, status=OrderProductStatus.unavailable) ]) res = self.try_admin_operation(lambda: self.client.post( f'/api/v1/admin/order/{gen_id}', json={'status': 'shipped'})) self.assertEqual(res.status_code, 200) order = Order.query.get(gen_id) self.assertEqual(order.total_krw, 2620) self.assertEqual(order.get_total_points(), 20)
def test_delete_last_order_item_in_suborder(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' self.try_add_entities([ Product(id='0001', name='Product 1', price=10, weight=10, points=10), Order(id=gen_id, country_id='c1', shipping_method_id=1, user=self.user) ]) self.try_add_entities([ Suborder(id=gen_id, order_id=gen_id), OrderProduct(suborder_id=gen_id, product_id='0001', quantity=1, status=OrderProductStatus.pending), Suborder(id=gen_id + "2", order_id=gen_id), OrderProduct(suborder_id=gen_id + "2", product_id='0001', quantity=1, status=OrderProductStatus.pending) ]) res = self.try_admin_operation( admin_only=True, operation=lambda: self.client.post( f'/api/v1/order/{gen_id}', json={ "suborders": [{ 'subcustomer': 'A001, Subcustomer1, P@ssw0rd', "items": [{ 'item_code': '0001', 'quantity': 1 }] }] })) self.assertEqual(res.status_code, 200) order = Order.query.get(gen_id) self.assertEqual(order.suborders.count(), 1)
def test_delete_invoice_item(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' order = Order(id=gen_id) self.try_add_entities([ Product(id=gen_id, name='Product 1') ]) self.try_add_entities([ order, Suborder(id=gen_id, order=order), OrderProduct(suborder_id=gen_id, product_id=gen_id, price=10, quantity=10), Invoice(id=gen_id, order_id=gen_id), InvoiceItem(id=10, invoice_id=gen_id, product_id=gen_id, price=10, quantity=10) ]) res = self.try_admin_operation( lambda: self.client.delete(f'/api/v1/admin/invoice/{gen_id}/item/10') ) self.assertEqual(res.status_code, 200) invoice_item = InvoiceItem.query.get(10) self.assertEqual(invoice_item, None)
def test_get_invoices(self): self.try_add_entities([ Product(id='0001', name='Product 1', name_english='P1', weight=10), Invoice(id='INV-2020-00-00', country_id='c1', customer='Customer 1', when_created=datetime(2020, 1, 1, 1, 0, 0), when_changed=datetime(2020, 1, 1, 1, 0, 0)), InvoiceItem(invoice_id='INV-2020-00-00', product_id='0001', price=10, quantity=1), Order(id=__name__ + '-1', invoice_id='INV-2020-00-00', country_id='c1', customer_name='Customer 1') ]) self.try_admin_operation( lambda: self.client.get('/api/v1/admin/invoice')) res = self.client.get('/api/v1/admin/invoice/INV-2020-00-00') self.assertEqual(len(res.json), 1) self.assertEqual(res.json[0], { 'address': None, 'country': 'country1', 'customer': 'Customer 1', 'payee': None, 'id': 'INV-2020-00-00', 'invoice_items': [{ 'id': 1, 'invoice_id': 'INV-2020-00-00', 'product_id': '0001', 'product': 'P1', 'price': 10.0, 'weight': 10, 'quantity': 1, 'subtotal': 10.0, 'when_created': None, 'when_changed': None }], 'orders': [__name__ + '-1'], 'phone': None, 'total': 10.0, 'weight': 10, 'when_changed': '2020-01-01 01:00:00', 'when_created': '2020-01-01 01:00:00' })
def test_increase_order_amount_over_free_shipping_threshold(self): order = Order(user=self.user) subcustomer = Subcustomer(name='A000', username='******') suborder = Suborder(order=order, subcustomer=subcustomer) self.try_add_entities([ order, suborder, subcustomer, OrderProduct(suborder=suborder, product_id='0000', price=10, quantity=10) ]) res = self.try_admin_operation( admin_only=True, operation=lambda: self.client.post( f'/api/v1/order/{order.id}', json={ 'address': 'Address1', 'country': 'c1', 'customer_name': "Customer1", 'phone': '1', 'zip': '1', 'shipping': '999', 'suborders': [{ 'subcustomer': 'A000', 'seq_num': 1, 'items': [{ 'item_code': '0000', 'quantity': 3000 }] }] })) self.assertEqual(res.status_code, 200) order = Order.query.get(order.id) self.assertEqual(order.total_krw, 30000)
def test_save_order_product(self): gen_id = f'{__name__}-{int(datetime.now().timestamp())}' op_id = datetime.now().microsecond order = Order(id=gen_id, user=self.user) suborder = Suborder(id=op_id, order=order) self.try_add_entities([ order, suborder, OrderProduct(id=op_id, suborder_id=op_id, product_id='0000', price=10, quantity=10, status='pending') ]) res = self.try_admin_operation(lambda: self.client.post( f'/api/v1/admin/order/product/{op_id}', json={'quantity': 100})) self.assertEqual(res.status_code, 200) self.assertEqual(res.json['quantity'], 100) res = self.client.get(f'/api/v1/admin/order/{gen_id}') self.assertTrue(res.json['total_krw'], 1010)
def form(request, id=None): products = [] if id: instance = get_object_or_404(Order, id=id) products.extend(instance.product_lines.all()) order_number = instance.order_number else: instance = Order() order_number = Order.calculate_next_order_number() if request.method == "POST": form = OrderForm(request.POST, instance=instance) products = [] i = 0 for p in request.POST.getlist('product_number'): p = ProductLine() p.description = request.POST.getlist('product_description')[i] p.price = request.POST.getlist('product_unit_cost')[i] p.count = request.POST.getlist('product_qty')[i] p.tax = request.POST.getlist('product_tax')[i] try: p.product = Product.objects.get(id=int(request.POST.getlist('product_number')[i])) except Exception, e: p.product = None products.append(p) i += 1 if form.is_valid(): o = form.save(commit=False) o.save(no_allemployee_group_permissions=True) o.update_products(products) request.message_success(_("Successfully saved order")) return redirect(view, o.id)