def add_customer(request): if request.method == "POST": form = CustomerForm(request.POST) if form.is_valid(): """ Este método save () acepta un argumento cometer palabra clave opcional, que acepta Verdadero o Falso. Si se llama a save () con comprometes = False, entonces se volverá un objeto que aún no se ha guardado en la base de datos. En este caso, le toca a usted para llamar a save() en la instancia del modelo resultante. Esto es útil si usted quiere hacer un tratamiento personalizado en el objeto antes de guardarlo, o si desea utilizar una de las opciones especializadas de ahorro de modelo. cometer es true de forma predeterminada. """ form.save() return redirect(order_index) else: print(form.errors) else: context = {"form": CustomerForm()} return render(request, "add_customer.html", context)
def order_bread(request, order_id): order = get_object_or_404(Order, id=order_id) if request.method == 'POST': if 'bread' in request.POST and request.POST['bread'] == 'Update': bread_form = BreadForm(request.POST) if bread_form.is_valid(): bread_form.process(order) url = '/order/bread/' + str(order_id) return redirect(url) else: bread_form = BreadForm() if 'customer' in request.POST and request.POST['customer'] == 'Update': customer_form = CustomerForm(request.POST) if customer_form.is_valid(): customer_form.process(order) url = '/order/pizza/' + str(order_id) return redirect(url) else: customer_form = CustomerForm() else: bread_form = BreadForm() customer_form = CustomerForm() return list.ListView(request, queryset=Order.objects.filter(id=order_id), template_name="order/bread.html", template_object_name="order", extra_context={ "bread_form": bread_form, "customer_form": customer_form, "order": order, })
def add_customer(request): context = {} if request.method == 'POST': form = CustomerForm(request.POST) if form.is_valid(): form.save() return redirect(order_index) else: print(form.errors) else: context.update({'form': CustomerForm()}) return render(request, 'add_customer.html',context)
def edit_customer(request, customer_id): context = {} try: customer = get_object_or_404(Customer, id=customer_id) if request.method == "POST": form = CustomerForm(request.POST) if form.is_valid(): customer.customer_name = form.cleaned_data["customer_name"] customer.customer_address = form.cleaned_data["customer_address"] customer.customer_phone = form.cleaned_data["customer_phone"] customer.save() return HttpResponseRedirect("/order/customer/detail/%s/" % customer.id) else: customer_data = { "customer_name": customer.customer_name, "customer_address": customer.customer_address, "customer_phone": customer.customer_phone, } form = CustomerForm(initial=customer_data) except Customer.DoesNotExist: context.update({"error": True}) context.update({"title": "Editar Cliente", "form": form, "update": True, "customer": customer}) return render(request, "add_customer.html", context)
def customer_edit(request, customer_id): context = {} try: customer = get_object_or_404(Customer, id=customer_id) if request.method == 'POST': form = CustomerForm(request.POST) if form.is_valid(): customer.customer_name = form.cleaned_data['customer_name'] customer.customer_address = form.cleaned_data['customer_address'] customer.customer_phone = form.cleaned_data['customer_phone'] customer.save() return HttpResponseRedirect('/order/customer/detail/%s/' % customer.customer_slug) else: customer_data = { 'customer_name': customer.customer_name, 'customer_address': customer.customer_address, 'customer_phone': customer.customer_phone } form = CustomerForm(initial=customer_data) except Customer.DoesNotExist: context.update({'error': True}) context.update({'title': 'Editar Cliente', 'form': form, 'update': True, 'customer': customer}) return render(request, 'add_customer.html', context)
def order_pizza(request, order_id): order = get_object_or_404(Order, id=order_id) if request.method == 'POST': if 'pizza' in request.POST and request.POST['pizza'] == 'Update': pizza_form = PizzaForm(request.POST) if pizza_form.is_valid(): pizza_form.process(order) url = '/order/pizza/' + str(order_id) return redirect(url) else: pizza_form = PizzaForm() if 'customer' in request.POST and request.POST['customer'] == 'Update': customer_form = CustomerForm(request.POST) if customer_form.is_valid(): customer_form.process(order) url = '/order/pizza/' + str(order_id) return redirect(url) else: customer_form = CustomerForm() else: pizza_form = PizzaForm() customer_form = CustomerForm() return list_detail.object_list( request, queryset = Order.objects.filter(id=order_id), template_name = "order/pizza.html", template_object_name = "order", extra_context = { "pizza_form": pizza_form, "customer_form": customer_form, "order": order, }, )
def post(self, request): generic_error_response = self.render_json_response({'status': 'error', 'errors': {'non_field_errors': 'Invalid submission.'}}) try: data = json.loads(request.POST.get('data')) tip = data.get('tip') items = data.get('items') first_name = data.get('first_name') last_name = data.get('last_name') phone_number = data.get('phone_number') cc_number = data.get('crimsoncash') except (IndexError, ValueError): return generic_error_response if any(i is None for i in [tip, items, first_name, last_name, phone_number, cc_number]): return generic_error_response order_form = OrderForm(data={'tip': tip}) if not order_form.is_valid(): return self.render_json_response({'status': 'error', 'errors': order_form.errors}) else: tip = order_form.cleaned_data.get('tip') order_items = [] for pk, quantity in items.iteritems(): order_item_form = OrderItemForm(data={'item': pk, 'quantity': quantity}) if not order_item_form.is_valid(): return self.render_json_response({'status': 'error', 'errors': order_item_form.errors}) else: quantity = order_item_form.cleaned_data.get('quantity') item = order_item_form.cleaned_data.get('item') if quantity > 0: order_items.append((item, quantity)) # add the phone number country code if necessary if not phone_number[:1] == '+' and not phone_number[:1] == '1': phone_number = u'+1{}'.format(phone_number) elif not phone_number[:1] == '+': phone_number = u'+{}'.format(phone_number) customer_form = CustomerForm(data={'first_name': first_name, 'last_name': last_name, 'phone': phone_number, 'cc_number': cc_number}) if not customer_form.is_valid(): return self.render_json_response({'status': 'error', 'errors': customer_form.errors}) # if they submitted an order with all quantities = 0 if not order_items: return generic_error_response first_name = customer_form.cleaned_data.get('first_name') last_name = customer_form.cleaned_data.get('last_name') phone_number = customer_form.cleaned_data.get('phone') cc_number = customer_form.cleaned_data.get('cc_number') customer, created = Customer.objects.get_or_create( first_name=first_name, last_name=last_name, phone=phone_number, cc_number=cc_number, ) order = Order.objects.create( customer=customer, tip=tip, ) m = hashlib.md5() m.update('{}{}'.format(order.customer.last_name, order.pk)) key = m.hexdigest() order.key = key order.save() for item, quantity in order_items: order.add_item(item, quantity) return self.render_json_response({'status': 'success', 'key': order.key})
def post(self, request): generic_error_response = self.render_json_response({ 'status': 'error', 'errors': { 'non_field_errors': 'Invalid submission.' } }) try: data = json.loads(request.POST.get('data')) tip = data.get('tip') items = data.get('items') first_name = data.get('first_name') last_name = data.get('last_name') phone_number = data.get('phone_number') cc_number = data.get('crimsoncash') except (IndexError, ValueError): return generic_error_response if any(i is None for i in [tip, items, first_name, last_name, phone_number, cc_number]): return generic_error_response order_form = OrderForm(data={'tip': tip}) if not order_form.is_valid(): return self.render_json_response({ 'status': 'error', 'errors': order_form.errors }) else: tip = order_form.cleaned_data.get('tip') order_items = [] for pk, quantity in items.iteritems(): order_item_form = OrderItemForm(data={ 'item': pk, 'quantity': quantity }) if not order_item_form.is_valid(): return self.render_json_response({ 'status': 'error', 'errors': order_item_form.errors }) else: quantity = order_item_form.cleaned_data.get('quantity') item = order_item_form.cleaned_data.get('item') if quantity > 0: order_items.append((item, quantity)) # add the phone number country code if necessary if not phone_number[:1] == '+' and not phone_number[:1] == '1': phone_number = u'+1{}'.format(phone_number) elif not phone_number[:1] == '+': phone_number = u'+{}'.format(phone_number) customer_form = CustomerForm( data={ 'first_name': first_name, 'last_name': last_name, 'phone': phone_number, 'cc_number': cc_number }) if not customer_form.is_valid(): return self.render_json_response({ 'status': 'error', 'errors': customer_form.errors }) # if they submitted an order with all quantities = 0 if not order_items: return generic_error_response first_name = customer_form.cleaned_data.get('first_name') last_name = customer_form.cleaned_data.get('last_name') phone_number = customer_form.cleaned_data.get('phone') cc_number = customer_form.cleaned_data.get('cc_number') customer, created = Customer.objects.get_or_create( first_name=first_name, last_name=last_name, phone=phone_number, cc_number=cc_number, ) order = Order.objects.create( customer=customer, tip=tip, ) m = hashlib.md5() m.update('{}{}'.format(order.customer.last_name, order.pk)) key = m.hexdigest() order.key = key order.save() for item, quantity in order_items: order.add_item(item, quantity) return self.render_json_response({ 'status': 'success', 'key': order.key })