class ModelTestCase(TestCase): def setUp(self): self.customer_name = 'Test Name' self.customer_address = 'Test Address' self.customer = Customer(customer_name=self.customer_name, customer_address=self.customer_address) self.pizza_size = True self.pizza_flavor = 'Surprise' self.pizza = Pizza(pizza_size=self.pizza_size, pizza_flavor=self.pizza_flavor) self.order = Order(customer=self.customer, pizza=self.pizza) def test_model_create_order(self): old_count = Customer.objects.count() self.customer.save() new_count = Customer.objects.count() self.assertNotEqual(old_count, new_count) old_count = Pizza.objects.count() self.pizza.save() new_count = Pizza.objects.count() self.assertNotEqual(old_count, new_count) old_count = Order.objects.count() self.order = Order(customer=Customer.objects.latest('customer_id'), pizza=Pizza.objects.latest('pizza_id')) self.order.save() new_count = Order.objects.count() self.assertNotEqual(old_count, new_count)
def orderSicilian_view(request): size = request.POST["size"] topping1 = request.POST["topping1"] topping2 = request.POST["topping2"] topping3 = request.POST["topping3"] topping4 = request.POST["topping4"] topping5 = request.POST["topping5"] pizza = Pizza(type="Sicilian", topping1=topping1, topping2=topping2, topping3=topping3, topping4=topping4, topping5=topping5, size=size) pizza.save() return render(request, "orders/pizza.html")
def add_order(request): if request.POST.get('name') and request.POST.get('address') and request.POST.get('size')\ and request.POST.get('flavor'): # create customer customer_name = request.POST.get('name') customer_address = request.POST.get('address') customer = Customer(customer_name=customer_name, customer_address=customer_address) customer.save() # create pizza pizza_size = request.POST.get('size') pizza_flavor = request.POST.get('flavor') pizza = Pizza(pizza_size=pizza_size, pizza_flavor=pizza_flavor) pizza.save() # create order c = Customer.objects.latest('customer_id') p = Pizza.objects.latest('pizza_id') order = Order(pizza=p, customer=c) order.save() return render(request, 'orders/order_completed.html') return render(request, 'orders/place_order.html')
import os import csv from orders.models import Pizza f = open("../csv2/pizza.csv") reader = csv.reader(f) for st, top, si, pr in reader: t = Pizza(style=st, size=si, topping=top, price=pr) t.save()
class TestApi(APITestCase): def setUp(self): # create pizzas self.pizza_regular = Pizza(name='Margherita') self.pizza_regular.save() self.pizza_medium = Pizza(name='Cheese & Corn', size='medium', type='veg') self.pizza_medium.save() self.pizza_large = Pizza(name='Chicken delight', size='medium', type='non-veg') self.pizza_large.save() self.customer = Customer(customer_name='Madhuri', customer_address='Ecity Bangalore', customer_phone='9483917071') self.customer.save() self.delivered_order = Order.objects.create(ordered_by=self.customer, status='delivered') self.delivered_order.pizza_list.add(self.pizza_medium) self.delivered_order.save() def test_order_creation(self): response = self.client.post(reverse('orders-list'), { 'ordered_by': { 'customer_name': "Shashank", "customer_address": "HSR Layout Bangalore", "customer_phone": "9535441964" }, "pizza_list": [{ "id": self.pizza_regular.id }, { "id": self.pizza_medium.id }], }, format='json') # assert a created status code was returned self.assertEqual(201, response.status_code) def test_retrieve_order(self): response = self.client.post(reverse('orders-list'), { 'ordered_by': { 'customer_name': "Shashank", "customer_address": "HSR Layout Bangalore", "customer_phone": "9535441964" }, "pizza_list": [{ "id": self.pizza_medium.id }], }, format='json') response = self.client.get(reverse('orders-list'), format='json') self.assertEqual(len(response.data['results']), Order.objects.all().count()) def test_updating_order_from_customer_end(self): order = Order.objects.create(ordered_by=self.customer, ) order.pizza_list.add(self.pizza_regular) order.save() response = self.client.put(reverse( 'orders-detail', kwargs={'pk': order.id}), { 'ordered_by': { 'id': self.customer.id }, 'status': "confirmed", "pizza_list": [{ "id": self.pizza_regular.id, "action": "remove" }, { "id": self.pizza_large.id, "action": "add" }], }, format="json") # check info returned has the update self.assertEqual(self.pizza_large.id, response.data['pizza_list'][0]['id']) def test_updating_order_from_restuarant_end(self): order = Order.objects.create(ordered_by=self.customer, ) order.pizza_list.add(self.pizza_regular, self.pizza_medium) order.save() url = reverse('orders-detail', kwargs={'pk': order.id}) data = { "status": "out_for_delivery", "ordered_by": { 'id': self.customer.id }, "pizza_list": order.get_pizza_list_base_action() } response = self.client.put(url, data, format="json") order.refresh_from_db() self.assertEqual(response.data['status'], order.status) def test_status_change_when_order_out_for_delivery(self): ''' Adding pizza when order is out for delivery :return: ''' order = Order.objects.create(ordered_by=self.customer, ) order.pizza_list.add(self.pizza_medium) order.status = 'out_for_delivery' order.save() response = self.client.put(reverse( 'orders-detail', kwargs={'pk': order.id}), { "pizza_list": [{ "id": self.pizza_regular.id, "action": "add" }, { "id": self.pizza_large.id, "action": "add" }], "status": 'out_for_delivery', 'ordered_by': { 'id': self.customer.id } }, format="json") self.assertEqual(400, response.status_code) def test_change_order_status_when_delivered(self): response = self.client.put( reverse('orders-detail', kwargs={'pk': self.delivered_order.id}), { 'status': 'confirmed', "ordered_by": { 'id': self.customer.id }, "pizza_list": self.delivered_order.get_pizza_list_base_action() }, format="json") self.assertEqual(400, response.status_code) def test_remove_order(self): order = Order.objects.all().last() self.client.delete(reverse('orders-detail', kwargs={'pk': self.delivered_order.id}), format='json') # check status order.refresh_from_db() self.assertEqual('cancelled', order.status)
def add_default_pizza(): ''' add default items in database ''' pizza = Pizza(name="Cheese", size=BaseItem.SMALL, price=12.70, crust=Pizza.REGULAR, max_toppings=0) pizza.save() pizza = Pizza(name="Cheese", size=BaseItem.LARGE, price=17.95, crust=Pizza.REGULAR, max_toppings=0) pizza.save() pizza = Pizza(name="1 topping", size=BaseItem.SMALL, price=13.70, crust=Pizza.REGULAR, max_toppings=1) pizza.save() pizza = Pizza(name="1 topping", size=BaseItem.LARGE, price=19.95, crust=Pizza.REGULAR, max_toppings=1) pizza.save() pizza = Pizza(name="2 topping", size=BaseItem.SMALL, price=15.20, crust=Pizza.REGULAR, max_toppings=2) pizza.save() pizza = Pizza(name="2 topping", size=BaseItem.LARGE, price=21.95, crust=Pizza.REGULAR, max_toppings=2) pizza.save() pizza = Pizza(name="3 topping", size=BaseItem.SMALL, price=16.20, crust=Pizza.REGULAR, max_toppings=3) pizza.save() pizza = Pizza(name="3 topping", size=BaseItem.LARGE, price=23.95, crust=Pizza.REGULAR, max_toppings=3) pizza.save() pizza = Pizza(name="Special", size=BaseItem.SMALL, price=17.75, crust=Pizza.REGULAR, max_toppings=5) pizza.save() pizza = Pizza(name="Special", size=BaseItem.LARGE, price=25.95, crust=Pizza.REGULAR, max_toppings=5) pizza.save() pizza = Pizza(name="Cheese", size=BaseItem.SMALL, price=24.45, crust=Pizza.SICILIAN, max_toppings=0) pizza.save() pizza = Pizza(name="Cheese", size=BaseItem.LARGE, price=38.70, crust=Pizza.SICILIAN, max_toppings=0) pizza.save() pizza = Pizza(name="1 topping", size=BaseItem.SMALL, price=26.45, crust=Pizza.SICILIAN, max_toppings=1) pizza.save() pizza = Pizza(name="1 topping", size=BaseItem.LARGE, price=40.70, crust=Pizza.SICILIAN, max_toppings=1) pizza.save() pizza = Pizza(name="2 topping", size=BaseItem.SMALL, price=28.45, crust=Pizza.SICILIAN, max_toppings=2) pizza.save() pizza = Pizza(name="2 topping", size=BaseItem.LARGE, price=42.70, crust=Pizza.SICILIAN, max_toppings=2) pizza.save() pizza = Pizza(name="3 topping", size=BaseItem.SMALL, price=29.45, crust=Pizza.SICILIAN, max_toppings=3) pizza.save() pizza = Pizza(name="3 topping", size=BaseItem.LARGE, price=44.70, crust=Pizza.SICILIAN, max_toppings=3) pizza.save() pizza = Pizza(name="Special", size=BaseItem.SMALL, price=30.45, crust=Pizza.SICILIAN, max_toppings=5) pizza.save() pizza = Pizza(name="Special", size=BaseItem.LARGE, price=45.70, crust=Pizza.SICILIAN, max_toppings=5) pizza.save()
import csv from orders.models import Pizza from orders.models import Topping f = open('data.txt', 'r') for line in f: line = line.split(',') product = Pizza() product.product = line[0] product.tipe = line[1] product.size = line[2] product.price = float(line[3]) #data is missing from file product.save() f.close() f = open('data1.txt', 'r') for line in f: line = line.split(',') print(line) product = Topping() product.product = line[0] product.tipo = line[1] product.save() f.close()
def create_order(request): if not request.user.is_authenticated: return render(request, "login.html", {"message": None}) else: order_pending = Order.objects.filter(user=request.user).filter( status=ACTIVE).exists() context = { "user": request.user, "address": request.user.customer.address, "city": request.user.customer.city, "state": request.user.customer.state, "zip_code": request.user.customer.zip_code, "first": request.user.first_name, "last": request.user.last_name, "sizes": Pizza_Size.objects.all(), "toppings": Topping.objects.all(), "styles": Pizza_Style.objects.all(), "types": Pizza_Topping_Type.objects.all(), "order_pending": order_pending, "order": Order.objects.filter(user=request.user).filter( status=ACTIVE).first(), "logged_in": True } if request.method == 'POST': if 'Cancel' in request.POST: the_order = Order.objects.filter(user=request.user).filter( status=ACTIVE).first() the_order.delete() return render(request, "menu.html", context) elif 'Update' in request.POST: current_items = request.POST.getlist('lineitems') quantity_items = request.POST.getlist('quantity') the_order = Order.objects.filter(user=request.user).filter( status=ACTIVE).first() updated_order = Order(user=request.user, status='A') updated_order.save() for i in range(0, len(quantity_items)): pizza = the_order.contents.all()[i] pizza.quantity = quantity_items[i] pizza.save() the_order.total = total( Order.objects.filter(user=request.user).filter( status=ACTIVE).first().contents.all()) the_order.save() return render(request, "cart.html", context) elif 'Order' in request.POST: the_order = Order.objects.filter(user=request.user).filter( status=ACTIVE).first() the_order.status = SUBMITTED the_order.save() return render(request, "confirmation.html", context) else: p_size = Pizza_Size.objects.get(size=request.POST.get("size")) p_style = Pizza_Style.objects.get( style=request.POST.get("style")) p_number_of_toppings = Pizza_Topping_Type.objects.get( value=request.POST.get("number_of_toppings")) p_toppings = request.POST.getlist('toppings') p_quantity = request.POST.get("quantity") p_type = get_object_or_404(Pizza_Type, size=p_size, style=p_style, topping_type=p_number_of_toppings) p = Pizza(pizza_type=p_type, quantity=p_quantity) p.save() for t in p_toppings: topping = get_object_or_404(Topping, name=t) p.toppings.add(topping) p_username = request.user customer = Customer.objects.get(user=p_username) the_order = Order.objects.filter(user=request.user).filter( status=ACTIVE).first() if the_order == None: the_order = Order(user=request.user, status=ACTIVE, total=p_type.price) the_order.save() the_order.contents.add(p) the_order.total = total( Order.objects.filter(user=request.user).filter( status=ACTIVE).first().contents.all()) the_order.save() else: the_order.contents.add(p) the_order.total = total( Order.objects.filter(user=request.user).filter( status=ACTIVE).first().contents.all()) the_order.save() context['order'] = Order.objects.filter( user=request.user).filter(status=ACTIVE).first() return render(request, "cart.html", context) else: return render(request, "create_order.html", context)