Exemplo n.º 1
0
Arquivo: views.py Projeto: gmist/f-toy
def show(request):
    gifts = []
    order_items = request.session.get('order', {})
    if not order_items:
        return render_to_response('cart/show.html', {'gifts':gifts})
    total_price = 0
    for k, val in order_items.iteritems():
        gift = Gift.get_by_id(k)
        if gift:
            gift.count = val
            gifts.append(gift)
            total_price += gift.price * val
    form = OrderForm()
    if request.method == 'POST' and form.validate(request.form):
        order = form.save(commit=False)
        order.email = request.form.get('email', None)
        order.address = request.form.get('address', None)
        order.put()
        for k, v in request.session.get('order', {}).items():
            gift = Gift.get_by_id(k)
            if gift:
                oi = OrderItem(gift_id=gift, count=v)
                oi.in_order = order
                oi.put()
        clear_cart(request)
        return render_to_response('cart/confirm_complete.html')
    return render_to_response('cart/show.html', {'gifts':gifts,
                                                 'total_price':total_price,
                                                 'form':form.as_widget()})
Exemplo n.º 2
0
Arquivo: views.py Projeto: gmist/f-toy
def confirm(request):
    if not request.session.get('order', {}):
        return redirect('/cart/show/')
    form = OrderForm()
    if request.method == 'POST' and form.validate(request.form):
        order = form.save()
        for k, v in request.session.get('order', {}).items():
            gift = Gift.get_by_id(k)
            if gift:
                oi = OrderItem(gift_id=gift, count=v)
                oi.in_order = order
                oi.put()
        clear_cart(request)
        return render_to_response('cart/confirm_complete.html')
    return render_to_response('cart/confirm.html', {'form':form.as_widget()})
Exemplo n.º 3
0
    def post(self, request, *args, **kwargs):
        cookie_list = get_cookie_list_from_cookie(request)
        order = self.form_class(json.loads(request.POST.get('form_fields')))
        price = request.POST.get('product_sum')
        count_container = request.POST.get('count_container')
        product_ids = [i['product_id'] for i in cookie_list]
        count = [i['count'] for i in cookie_list]
        count_two = [i['count_two'] for i in cookie_list]
        products = Product.objects.filter(id__in=product_ids)
        order_items = []
        product_dict = dict((product.id, product) for product in products)
        ordered_products = [
            product_dict.get(product_item_id)
            for product_item_id in product_ids
        ]
        sum_container = int(count_container) * 8

        if order.is_valid():
            order_model = order.save(commit=False)
            order_model.price = price
            order_model.count_container = f'{count_container}шт.*8сом = {sum_container}сом'
            order_model.save()
            text = get_all_telegram_text(order_model, ordered_products, price,
                                         count, count_two)
            send_telegram_notification(text, 853892764)

            for product in ordered_products:
                if product.price_two:
                    price_two = product.price_two
                else:
                    price_two = 0
                index_prod = ordered_products.index(product)
                order_items.append(
                    OrderItem(
                        product=product,
                        order=order_model,
                        quantity_big=count[index_prod],
                        quantity_small=count_two[index_prod],
                        price_big=product.price,
                        price_small=price_two,
                    ))

            OrderItem.objects.bulk_create(order_items)

            clear_basket = ClearBasketView()

            response = clear_basket.post(self.request, *args, **kwargs)

            return response
        else:
            order = self.form_class(json.loads(
                request.POST.get('form_fields')))
            message = []

            for item in order.errors:
                message.append([item, order.errors[item]])

            return JsonResponse(dict(success=False, message=message),
                                status=400)
Exemplo n.º 4
0
Arquivo: views.py Projeto: gmist/f-toy
def simple_buy(request, id, count):
    gift = Gift.get_by_id(id)
    if not gift:
        return render_to_response('empty.html')
    form = OrderForm()
    if request.method == 'POST' and form.validate(request.form) and count:
        order = form.save()
        oi = OrderItem(gift_id=gift, count=count)
        oi.in_order = order
        oi.put()
        def txn():
                taskqueue.add(url=url_for('cart/send_order_to_manager',
                                    order_key=str(order.key())),
                                    transactional=True)
        db.run_in_transaction(txn)
        return render_to_response('cart/confirm_complete_text.html')
    return render_to_response('cart/cart_block.html', {'gift':gift, 'form':form.as_widget()})