def order_process(request, pk=None): cart = MyCart(request) if cart.get_total_item() <= 0: return redirect("main:shop") else: order = OrdCart(request) if not isinstance(order.order_obj, Order): return HttpResponseRedirect(request.META.get('HTTP_REFERER')) order.reload(cart.cart_obj) if not isinstance(order.order_obj.address_delivery, Address): return redirect("cart:order_delivery", pk) if not isinstance(order.order_obj.address_invoice, Address): return redirect("cart:order_invoice", pk) else: try: invoice = OrderInvoice.objects.get(order_id=order.order_obj) OrderInvoicePayment.objects.get(order_id=order.order_obj, order_invoice_id=invoice) order.order_obj.cart_id = None order.order_obj.save() cart.clear() except (OrderInvoice.DoesNotExist, OrderInvoicePayment.DoesNotExist, OrderPayment.DoesNotExist): return redirect("cart:order_payment", pk) template_name = 'order/success.html' return render(request, template_name, {'order': order.order_obj})
def cart_reset(request): cart = MyCart(request) cart.clear() order = OrdCart(request) if isinstance(order.order_obj, Order): order.reload(cart.cart_obj) return redirect("main:shop")
def cart_process(request, pk=None, act=None): cart = MyCart(request) if cart.get_total_item() <= 0: return redirect("main:shop") else: order = OrdCart(request) if not isinstance(order.order_obj, Order): return HttpResponseRedirect(request.META.get('HTTP_REFERER')) order.load(cart.cart_obj) return render(request, 'order/index.html', { 'order': order.order_obj, 'items': order.get_items() })
def cart_clear(request, pk): cart = MyCart(request) try: item = CartItem.objects.get(find_item_id=pk) cart.remove(item) except CartItem.DoesNotExist: return redirect("main:shop") if cart.get_total_item() > 0: order = OrdCart(request) if isinstance(order.order_obj, Order): order.reload(cart.cart_obj) return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: return redirect("main:shop")
def get(self, request, *args, **kwargs): """ return regular list view on page load and then json data on datatables ajax request. """ cart = MyCart(request) if cart.get_total_item() <= 0: return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: order = OrdCart(request) if not isinstance(order.order_obj, Order): return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: order.load(cart.cart_obj) context = super(CartProcessView, self).get_context_data(**kwargs) context['order'] = order.order_obj context['items'] = order.get_items() return self.render_to_response(context)
def order_payment(request, pk=None): cart = MyCart(request) if cart.get_total_item() <= 0: return redirect("main:shop") else: order = OrdCart(request) if not isinstance(order.order_obj, Order): return HttpResponseRedirect(request.META.get('HTTP_REFERER')) order.reload(cart.cart_obj) if not isinstance(order.order_obj.address_delivery, Address): return redirect("cart:order_delivery", pk) if not isinstance(order.order_obj.address_invoice, Address): return redirect("cart:order_invoice", pk) else: try: invoice = OrderInvoice.objects.get(order_id=order.order_obj) except OrderInvoice.DoesNotExist: invoice = OrderInvoice.objects.create(order_id=order.order_obj) invoice.amount = order.order_obj.total_paid_real invoice.note = "Auto general in process payment" invoice.save() mid_obj = None max_loop = 1 while mid_obj is None and max_loop >= 0: try: mid_obj = OrderInvoicePayment.objects.get( order_id=order.order_obj, order_invoice_id=invoice) object = mid_obj.order_payment_id except (OrderInvoicePayment.DoesNotExist, OrderPayment.DoesNotExist): object = OrderPayment.objects.create( amount=order.order_obj.total_paid_real, payment_method=METHOD_PAYMENT1, order_reference=order.order_obj.secure_key) mid_obj = OrderInvoicePayment.objects.create( order_payment_id=object, order_id=order.order_obj, order_invoice_id=invoice, sign=TYPE1) max_loop -= 1 object.amount = order.order_obj.total_paid_real object.payment_method = METHOD_PAYMENT1 if object.payment_method is None else object.payment_method object.save() if request.method == 'POST': form = OrderPaymentForm( instance=object, data=request.POST ) if object is not None else OrderPaymentForm( data=request.POST) if form.is_valid(): object.order_reference = order.order_obj.secure_key object.amount = form.cleaned_data['amount'] object.payment_method = form.cleaned_data['payment_method'] object.data_payment = form.cleaned_data['data_payment'] object.save() for i in range(1, MAX_SIZE_UPLOADED + 1): name = "file%s" % i _file = request.FILES[ name] if name in request.FILES else False if _file is not None and ( isinstance(_file, InMemoryUploadedFile) or isinstance(_file, TemporaryUploadedFile)): try: con_type = _file.content_type if con_type in IMGCON_TYPES: save_image(request, object) elif con_type in DOCCON_TYPES: save_file(request, object) else: continue except (AttributeError, IndexError, ValueError): continue if not request.is_ajax( ) and 'payment' in request.POST and request.POST[ 'payment'] == 'finished': return redirect("cart:order_process", pk) if not request.is_ajax(): next = request.META['PATH_INFO'] return HttpResponseRedirect(next) else: form = OrderPaymentForm( ) if object is None else OrderPaymentForm(instance=object) template_name = 'order/payment.html' return render( request, template_name, { 'order': order.order_obj, 'items': order.get_items(), 'form': form, 'object': object })
def order_invoice(request, pk=None): cart = MyCart(request) if cart.get_total_item() <= 0: return redirect("main:shop") else: order = OrdCart(request) if not isinstance(order.order_obj, Order): return HttpResponseRedirect(request.META.get('HTTP_REFERER')) order.load(cart.cart_obj) if request.is_ajax(): template_name = 'order/address_inner.html' else: template_name = 'order/invoice.html' object = order.order_obj.address_invoice if isinstance( order.order_obj.address_invoice, Address) else None if request.method == 'POST': form = AddressForm( instance=object, data=request.POST) if object is not None else AddressForm( data=request.POST) if form.is_valid(): if order.order_obj.address_invoice is None: address = form.save() order.order_obj.address_invoice = address order.order_obj.save() else: address = order.order_obj.address_invoice address.address = form.cleaned_data['address'] address.mobile = form.cleaned_data['mobile'] address.city_id = form.cleaned_data['city_id'] address.district_id = form.cleaned_data['district_id'] address.ward_id = form.cleaned_data['ward_id'] address.save() if not request.is_ajax( ) and 'invoice' in request.POST and request.POST[ 'invoice'] == 'delivery': if isinstance( order.order_obj.address_invoice, Address) and not isinstance( order.order_obj.address_delivery, Address): obj = Address.objects.get(pk=address.pk) obj.pk = None obj.save() order.order_obj.address_delivery = obj order.order_obj.save() return redirect("cart:order_delivery", pk) if not request.is_ajax( ) and 'invoice' in request.POST and request.POST[ 'invoice'] == 'payment': return redirect("cart:order_payment", pk) if not request.is_ajax(): next = request.META['PATH_INFO'] return HttpResponseRedirect(next) else: form = AddressForm() if object is None else AddressForm( instance=object) return render(request, template_name, { 'order': order.order_obj, 'items': order.get_items(), 'form': form })