def cria_pizza_personalizada(request): cliente_criando_pessoalmente = len(request.user.cliente_set.all()) != 0 if cliente_criando_pessoalmente: cliente = request.user.cliente_set.all()[0] else: cliente = Cliente.objects.get(nome='Personalizadas') ingredientes = Ingrediente.objects.all() if request.method == 'POST': form = PizzaPersonalizadaForm(request.POST) if form.is_valid(): nome = form.cleaned_data['nome'] preco = 15 pizza = Pizza(nome=nome, preco=preco, inventor=cliente, personalizada=True) pizza.save() for ingrediente in ingredientes: if request.POST.get(ingrediente.nome) == 'on': pizza.ingredientes.add(ingrediente) pizza.preco += ingrediente.preco pizza.save() if cliente_criando_pessoalmente: return HttpResponseRedirect('/pizzer/pizzas/personalizadas') return HttpResponseRedirect('/pizzer/pizzas/personalizadas/telefone') else: form = PizzaPersonalizadaForm() return render_to_response('criacao_pizza_personalizada.html', {'form': form, 'ingredientes': ingredientes}, context_instance=RequestContext(request))
def edita_pizza_personalizada(request, object_id): pizza = Pizza.objects.get(pk=object_id) inventor = pizza.inventor ingredientes = Ingrediente.objects.all() if request.method == 'POST': pizza.delete() form = PizzaPersonalizadaForm(request.POST) if form.is_valid(): nome = form.cleaned_data['nome'] pizza = Pizza(nome=nome, inventor=inventor, preco=15, personalizada=True) pizza.save() for ingrediente in ingredientes: if request.POST.get(ingrediente.nome) == 'on': pizza.ingredientes.add(ingrediente) pizza.preco += ingrediente.preco pizza.save() return HttpResponseRedirect('/pizzer/') else: ingredientes1 = pizza.ingredientes.all() ingredientes2 = ingredientes for ingrediente in ingredientes1: ingredientes2 = ingredientes2.exclude(nome=ingrediente.nome) form = PizzaPersonalizadaForm(instance=pizza) return render_to_response('edicao_pizza_personalizada.html', {'form': form, 'pizza': pizza, 'ingredientes': ingredientes, 'ingredientes1': ingredientes1, 'ingredientes2': ingredientes2}, context_instance=RequestContext(request))
def make_pizzas(halves): pairs = get_pairs(halves) wholes = [] for pair in pairs: if len(pair) == 2: half1 = db.session.query(Half).filter_by(id=pair[0]).first() half2 = db.session.query(Half).filter_by(id=pair[1]).first() person1 = Person(email=half1.email, location=half1.location) person2 = Person(email=half2.email, location=half2.location) whole = Pizza(person1=person1, person2=person2) whole.topping1_left = half1.topping1 whole.topping2_left = half1.topping2 whole.topping3_left = half1.topping3 whole.topping1_right = half2.topping1 whole.topping2_right = half2.topping2 whole.topping3_right = half2.topping3 whole.sauce = 'Tomato' whole.size = 'Large' whole.time_added = datetime.datetime.now() wholes.append(whole) else: wholes.append(db.session.query(Half).filter_by(id=pair[0]).first()) return wholes
from create_db import engine from models import Pizza, Choice, Base import json import argparse session = sessionmaker() session.configure(bind=engine) s = session() if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("filename") args = parser.parse_args() Base.metadata.drop_all(engine) Base.metadata.create_all(engine) with open(args.filename, "r") as file: catalog = json.load(file) for pizza in catalog: new_pizza = Pizza(title=pizza['title'], description=pizza['description']) for choice in pizza['choices']: new_choice = Choice(title=choice['title'], price=int(choice['price'])) new_pizza.choices.append(new_choice) s.add(new_pizza) s.commit()
# Create addresses and append them to user. for address in user['addresses']: db_address = Address(user=db_user, street=address['street'], city=address['city'], country=address['country'], type=address['type']) session.add(db_address) session.add(db_user) # Create pizza for pizza_item in pizza: pizza_price = 0 db_pizza = Pizza(name=pizza_item['name'], price=pizza_price) for ingredient in pizza_item['ingredients']: db_ingredient = session.query(Ingredient).filter( Ingredient.name == ingredient['name']).first() if not db_ingredient: db_ingredient = Ingredient(name=ingredient['name'], price=ingredient['price']) session.add(db_ingredient) pizza_ingredient = Recipe(pizza=db_pizza, ingredient=db_ingredient, quantity=ingredient['quantity']) session.add(pizza_ingredient) pizza_price += db_ingredient.price * ingredient['quantity']
def populate(dbname, jsondata): """docstring for populate""" session = connect(dbname) with open(jsondata) as f: data = json.load(f) for i in data['vat']: vat = Vat(rate=i) session.add(vat) for i in data['pay_status']: p_status = Payment_status(status=i) session.add(p_status) for i in data['order_status']: o_status = Order_status(status=i) session.add(o_status) for i in data['ingredients']: ingredient = Ingredient(name=i['name'], delivunit=i['delivunit'], storunit=i['storunit'], storqty_delivunit=i['storqty_delivunit'], produnit=i['produnit'], prodqty_storunit=i['prodqty_storunit'], cost=i['cost']) session.add(ingredient) for i in data['clients']: client = Client(family_name=i['family_name'], first_name=i['first_name'], email=i['email'], phone=i['phone']) session.add(client) for i in data['clients']: name = [(n.id, n.family_name) for n in session.query(Client).all()] for n in name: if n[1] == i['family_name']: for index, address in enumerate(i['address']): if index == 0: addresses = Address(client=n[0], address=address, invoice=True) session.add(addresses) else: addresses = Address(client=n[0], address=address, invoice=False) session.add(addresses) for i in data['pizzas']: vat = [(n.id, n.rate) for n in session.query(Vat).all()] for n in vat: if n[1] == i['vat']: pizza = Pizza(name=i['name'], price=i['price'], vat=n[0]) session.add(pizza) for i in data['recipes']: name = [(n.id, n.name) for n in session.query(Pizza).all()] for n in name: if n[1] == i['name']: for rep in i['recipe']: ingr = [(n.id, n.name) for n in session.query(Ingredient).all()] for ningr in ingr: if ningr[1] == rep[0]: recipe = Recipe_ingredient(pizza=n[0], ingredient=ningr[0], quantity=rep[1]) session.add(recipe) for i in data['restaurants']: restaurant = Restaurant(name=i) session.add(restaurant) for i in data['stocks']: restaurant = [(n.id, n.name) for n in session.query(Restaurant).all()] for r in restaurant: if r[1] == i['stock_loc']: name = [(n.id, n.name) for n in session.query(Ingredient).all()] for n in name: if n[1] == i['name']: stock = Stock(stock_loc=r[0], ingredient=n[0], quantity=i['quantity']) session.add(stock) for i in data['orders']: name = [(n.id, n.family_name) for n in session.query(Client).all()] for n in name: if n[1] == i['client_name']: o_status = [(n.id, n.status) for n in session.query(Order_status).all()] for o in o_status: if o[1] == i['order_status']: p_status = [(n.id, n.status) for n in session.query(Payment_status).all()] for p in p_status: if p[1] == i['pay_status']: dstring = i['datetime'] dt = datetime.datetime.strptime(dstring, '%Y, %m, %d, \ %H, %M, %S') order = Order(date=dt, client=n[0], order_status=o[0], payment_status=p[0]) session.add(order) for i in data['orders']: dstring = i['datetime'] dt = datetime.datetime.strptime(dstring, '%Y, %m, %d, %H, %M, %S') order = [(n.id, n.date) for n in session.query(Order).all()] for o in order: if o[1] == dt: pizza = [(n.id, n.name) for n in session.query(Pizza).all()] for o_pizza in i['pizza_name']: for p in pizza: if p[1] == o_pizza: pizza_ordered = Pizza_ordered(order_cd=o[0], pizza=p[0], quantity=1) session.add(pizza_ordered) session.commit()
from app import db from models import Pizza, Choice def load_catalog(filename): with open(filename) as file_handler: return json.loads(file_handler.read()) if __name__ == '__main__': catalog_filename = sys.argv[1] if len(sys.argv) > 1 else 'catalog.json' catalog = list() for entry in load_catalog(catalog_filename): pizza = Pizza(title=entry['title'], description=entry['description']) for choice in entry['choices']: pizza.choices.append( Choice(title=choice['title'], price=choice['price'])) catalog.append(pizza) db.session.add_all(catalog) db.session.commit()
import json from app import db from models import Pizza, Choice from db.catalog import catalog choice_id = 1 for pizza_id, pizza_data in enumerate(catalog, 1): pizza = Pizza( id=pizza_id, title=pizza_data['title'], description=pizza_data['description']) print(pizza_id, pizza) db.session.add(pizza) for choice_data in pizza_data['choices']: choice_id += 1 choice = Choice( id=choice_id, title=choice_data['title'], price=choice_data['price'], pizza_id=pizza_id) print(choice_id, choice) db.session.add(choice) db.session.commit()
from models import Pizza, Topping t1 = Topping(name='Pepperoni') t2 = Topping(name='Spinach') t3 = Topping(name='Mushrooms') t4 = Topping(name='Pineapple') t5 = Topping(name='Olives') t1.save() t2.save() t3.save() t4.save() t5.save() p1 = Pizza(name="Pizza 1", size="small", quantity=3) p1.save() p1.toppings.add(t1, t4)