def add_orderitem(request, order_id): order = get_object_or_404(Order, id = order_id) info_msg(request, request.POST.items()) form = OrderItemForm(data = request.POST) if form.is_valid(): item = form.save(commit = False) item.cost_type = request.POST['cost_type'] item.save() order.items.add(item) if order.budget: item.create_budget_line() info_msg(request, u"'%s' ajouté à la commande avec succès." % item.name) else: error_msg(request, "Le formulaire n'est pas valide, veuillez remplir les champs obligatoires. %s %s" % ( form.errors, request.POST.items())) return redirect(request.POST.get('next', order))
def _orderitem_update(request, orderitem): order = orderitem.order_set.get() data = request.POST.copy() data["provider"] = order.provider.name form = OrderItemForm(instance=orderitem, data=data) if form.is_valid(): form.save() if order.number: orderitem.update_budget_line() if orderitem.product_id: orderitem.update_product() info_msg(request, "Commande modifiée avec succès.") return redirect(order) return render(request, "order/orderitem_detail.html", {"form": form, "orderitem": orderitem, "order": order})
def add_orderitem(request, order_id): order = get_object_or_404(Order, id=order_id) info_msg(request, request.POST.items()) form = OrderItemForm(data=request.POST) if form.is_valid(): item = form.save(commit=False) item.cost_type = request.POST['cost_type'] item.save() order.items.add(item) if order.number: item.create_budget_line() info_msg(request, u"'%s' ajouté à la commande avec succès." % item.name) else: error_msg( request, "Le formulaire n'est pas valide, veuillez remplir les champs obligatoires. %s %s" % (form.errors, request.POST.items())) return redirect(request.POST.get('next', order))
def _orderitem_update(request, orderitem): order = orderitem.order_set.get() data = request.POST.copy() data['provider'] = order.provider.name form = OrderItemForm(instance=orderitem, data=data) if form.is_valid(): form.save() if order.number: orderitem.update_budget_line() if orderitem.product_id: orderitem.update_product() info_msg(request, "Commande modifiée avec succès.") return redirect(order) return render(request, 'order/orderitem_detail.html', { 'form': form, 'orderitem': orderitem, '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 })