def add_product_to_cart(self): product = DiaryProduct(isbn='1234567890', number_of_pages=100, name='test', slug='test', active=True, unit_price=Decimal('1.23')) product.save() self.cart = get_or_create_cart(self.request, True) self.cart.add_product(product, 1) self.cart.save()
def get_context_data(self, **kwargs): context = {'object_list': []} cart = get_or_create_cart(self.request, True) context['object_list'] = CartModifierCode.objects.filter(cart=cart) context.update(kwargs) return super(CartModifierCodeCreateView, self).\ get_context_data(**context)
def test_03_passing_user_returns_proper_cart(self): self.cart.user = self.user self.cart.save() setattr(self.request, 'user', self.user) ret = get_or_create_cart(self.request) self.assertNotEqual(ret, None) self.assertEqual(ret, self.cart)
def get_context(self, context): request = context['request'] cart = get_or_create_cart(request) cart.update(request) return { 'cart': cart }
def handle_billingshipping_forms(self, js_enabled, update_only, shipping_adress_form, billing_address_form): all_valid = False billingshipping_form = \ self.get_billing_and_shipping_selection_form() if billingshipping_form.is_valid(): self.request.session['payment_backend'] = \ billingshipping_form.cleaned_data['payment_method'] self.request.session['shipping_backend'] = \ billingshipping_form.cleaned_data['shipping_method'] shipping_choices_form = False payment_choices_form = False if billingshipping_form.is_valid(): shipping_method = billingshipping_form.cleaned_data['shipping_method'] payment_method = billingshipping_form.cleaned_data['payment_method'] if self.request.method == 'POST' and js_enabled: items = get_or_create_cart(self.request).items.all() shipping_choices_form = self.get_backend_choices_form('shipping', shipping_method, items, shipping_adress_form, billing_address_form) payment_choices_form = self.get_backend_choices_form('payment', payment_method, items, shipping_adress_form, billing_address_form) if not update_only: if shipping_choices_form: if shipping_choices_form.is_valid(): self.request.session['shipping_choices'] = shipping_choices_form.cleaned_data if payment_choices_form: if payment_choices_form.is_valid(): self.request.session['payment_choices'] = payment_choices_form.cleaned_data return (billingshipping_form, shipping_choices_form, payment_choices_form)
def get_context(self, context): cart = get_or_create_cart(context['request']) cart.update() return { 'cart': cart, 'cart_items': cart.get_updated_cart_items() }
def get_context_data(self, **kwargs): ctx = super(ShopTemplateView, self).get_context_data(**kwargs) # Set the order status: order = get_order_from_request(self.request) if order: order.status = Order.COMPLETED order.save() else: order = Order.objects.get_latest_for_user(self.request.user) #TODO: Is this ever the case? ctx.update({'order': order, }) # TODO: move away from shop!! # ctx.update({'downloads': [1], }) completed.send(sender=self, order=order) # Empty the customers basket, to reflect that the purchase was # completed cart_object = get_or_create_cart(self.request) cart_object.empty() return ctx
def get_context_data(self, **kwargs): ctx = super(ShopTemplateView, self).get_context_data(**kwargs) # Set the order status: order = get_order_from_request(self.request) if order: order.status = Order.COMPLETED order.save() else: order = Order.objects.get_latest_for_user(self.request.user) #TODO: Is this ever the case? ctx.update({ 'order': order, }) # TODO: move away from shop!! # ctx.update({'downloads': [1], }) completed.send(sender=self, order=order) # Empty the customers basket, to reflect that the purchase was # completed cart_object = get_or_create_cart(self.request) cart_object.empty() return ctx
def delete(self, *args, **kwargs): """ Empty shopping cart. """ cart_object = get_or_create_cart(self.request) cart_object.empty() return self.delete_success()
def cart(context): """Inclusion tag for displaying cart summary.""" request = context['request'] cart = get_or_create_cart(request) cart.update(request) return { 'cart': cart }
def get_context_data(self, **kwargs): # There is no get_context_data on super(), we inherit from the mixin! ctx = {} cart = get_or_create_cart(self.request) cart.update(self.request) ctx.update({'cart': cart}) ctx.update({'cart_items': cart.get_updated_cart_items()}) return ctx
def test_having_two_empty_carts_returns_database_cart(self): setattr(self.request, 'user', self.user) setattr(self.request, 'session', {'cart_id': self.cart.pk}) database_cart = Cart.objects.create(user=self.user) ret = get_or_create_cart(self.request) self.assertNotEqual(ret, None) self.assertEqual(ret, database_cart) self.assertNotEqual(ret, self.cart)
def render(self, context, instance, placeholder): request = context['request'] cart = get_or_create_cart(request) cart_items = cart.items context.update({'cart': cart}) context.update({'cart_items': cart_items}) return context
def success(self): """ Generic hook by default redirects to cart """ if self.request.is_ajax(): cart_object = get_or_create_cart(self.request) return HttpResponse(json.dumps({"success": True, "cart": cart_object.total_quantity})) else: return HttpResponseRedirect(reverse("cart"))
def add_to_cart(self): variation = self.get_variation() product_quantity = self.request.POST.get('add_item_quantity') if not product_quantity: product_quantity = 1 product = self.get_object() cart = get_or_create_cart(self.request) cart.add_product(product, product_quantity, variation) cart.save()
def create_order_object_from_cart(self): """ This will create an Order object form the current cart, and will pass a reference to the Order on either the User object or the session. """ cart = get_or_create_cart(self.request) order = Order.objects.create_from_cart(cart) request = self.request add_order_to_request(request, order)
def render_tag(self, context): cart = get_or_create_cart(context['request']) cart.update() context['cart'] = cart context['cart_items': cart.get_updated_cart_items()] formset = get_cart_item_formset(cart_items=context['cart_items']) context['formset': formset] return ''
def create_order_object_from_cart(self): ''' This will create an Order object form the current cart, and will pass a reference to the Order on either the User object or the session. ''' cart = get_or_create_cart(self.request) order = Order.objects.create_from_cart(cart) request = self.request add_order_to_request(request, order)
def render(self, context, instance, placeholder): request = context['request'] cart = get_or_create_cart(request) cart_items = cart.items context.update({'cart':cart}) context.update({'cart_items':cart_items}) return context
def post(self, *args, **kwargs): #it starts similar to the original post method product_id = self.request.POST['add_item_id'] product_quantity = self.request.POST.get('add_item_quantity') if not product_quantity: product_quantity = 1 product = Product.objects.get(pk=product_id) cart_object = get_or_create_cart(self.request) #now we need to find out which options have been chosen by the user option_ids = [] text_option_ids = {} # A dict of {TextOption.id:CartItemTextOption.text} for key in self.request.POST.keys(): if key.startswith('add_item_option_group_'): option_ids.append(self.request.POST[key]) elif key.startswith('add_item_text_option_'): id = key.split('add_item_text_option_')[1] text_option_ids.update({id:self.request.POST[key]}) #now we need to find out if there are any cart items that have the exact #same set of options qs = CartItem.objects.filter(cart=cart_object).filter(product=product) found_cartitem_id = None merge = False for cartitem in qs: # for each CartItem in the Cart, get it's options and text options cartitemoptions = CartItemOption.objects.filter( cartitem=cartitem, option__in=option_ids ) cartitemtxtoptions = CartItemTextOption.objects.filter( text_option__in=text_option_ids.keys(), text__in=text_option_ids.values() ) if (len(cartitemoptions) + len(cartitemtxtoptions) == (len(option_ids) + len(text_option_ids))): found_cartitem_id = cartitem.id merge = True break #if we found a CartItem object that has the same options, we need #to select this one instead of just any CartItem that belongs to this #cart and this product. if found_cartitem_id: qs = CartItem.objects.filter(pk=found_cartitem_id) cart_item = cart_object.add_product( product, product_quantity, merge=merge, queryset=qs) if isinstance(product, Cigarette): try: cart_object.add_product(product.cartridge, 0) for accessory in product.accessories.all(): cart_object.add_product(accessory, 0) except Cartridge.DoesNotExist, AttributeError: pass
def post(self, request, id, *args, **kwargs): split_template = get_object_or_404(SplitTemplate, id=id) split_form = UserSplitForm(split_template, request.POST) # Make sure we empty the cart cart = get_or_create_cart(request, save=True) cart.delete() cart = get_or_create_cart(request, save=True) if split_form.is_valid(): for key, product_qtty in split_form.cleaned_data.iteritems(): if key.startswith('product_'): product_id = key.lstrip('product_') product = get_object_or_404(Product, id=product_id) cart.add_product(product, product_qtty) cart.save() return redirect(reverse('cart'))
def test_having_empty_session_cart_and_filled_database_cart_returns_database_cart(self): setattr(self.request, 'user', self.user) setattr(self.request, 'session', {'cart_id': self.cart.pk}) database_cart = Cart.objects.create(user=self.user) product = Product.objects.create(name='pizza', slug='pizza', unit_price=0) CartItem.objects.create(cart=database_cart, quantity=1, product=product) ret = get_or_create_cart(self.request) self.assertNotEqual(ret, None) self.assertEqual(ret, database_cart) self.assertNotEqual(ret, self.cart)
def test_having_empty_session_cart_and_filled_database_cart_returns_database_cart(self): setattr(self.request, 'user', self.user) setattr(self.request, 'session', {'cart_id': self.cart.id}) database_cart = Cart.objects.create(user=self.user) product = Product.objects.create(name='pizza', slug='pizza', unit_price=0) CartItem.objects.create(cart=database_cart, quantity=1, product=product) ret = get_or_create_cart(self.request) self.assertNotEqual(ret, None) self.assertEqual(ret, database_cart) self.assertNotEqual(ret, self.cart)
def post(self, *args, **kwargs): #it starts similar to the original post method product_id = self.request.POST['add_item_id'] product_quantity = self.request.POST.get('add_item_quantity') if not product_quantity: product_quantity = 1 product = Product.objects.get(pk=product_id) cart_object = get_or_create_cart(self.request, save=True) #now we need to find out which options have been chosen by the user option_ids = [] text_option_ids = {} # A dict of {TextOption.id:CartItemTextOption.text} for key in self.request.POST.keys(): if key.startswith('add_item_option_group_'): option_ids.append(self.request.POST[key]) elif key.startswith('add_item_text_option_'): id = key.split('add_item_text_option_')[1] txt = self.request.POST[key] if txt != '': text_option_ids.update({id:txt}) #now we need to find out if there are any cart items that have the exact #same set of options qs = CartItem.objects.filter(cart=cart_object).filter(product=product) found_cartitem_id = None merge = False # TODO: Something funky happening with the merged qs preventing new # carts from being created. We can live with separate line items for now... # for cartitem in qs: # # for each CartItem in the Cart, get it's options and text options # cartitemoptions = CartItemOption.objects.filter( # cartitem=cartitem, option__in=option_ids # ) # # cartitemtxtoptions = CartItemTextOption.objects.filter( # text_option__in=text_option_ids.keys(), # text__in=text_option_ids.values() # ) # # if len(cartitemoptions) + len(cartitemtxtoptions) == (len(option_ids) + len(text_option_ids)): # found_cartitem_id = cartitem.id # merge = True # break #if we found a CartItem object that has the same options, we need #to select this one instead of just any CartItem that belongs to this #cart and this product. if found_cartitem_id: qs = CartItem.objects.filter(pk=found_cartitem_id) cart_item = cart_object.add_product( product, product_quantity, merge=merge, queryset=qs) cart_object.save() return self.post_success(product, cart_item)
def post(self, *args, **kwargs): #it starts similar to the original post method product_id = self.request.POST['add_item_id'] product_quantity = self.request.POST.get('add_item_quantity') if not product_quantity: product_quantity = 1 product = Product.objects.get(pk=product_id) cart_object = get_or_create_cart(self.request) #now we need to find out which options have been chosen by the user option_ids = [] text_option_ids = { } # A dict of {TextOption.id:CartItemTextOption.text} for key in self.request.POST.keys(): if key.startswith('add_item_option_group_'): option_ids.append(self.request.POST[key]) elif key.startswith('add_item_text_option_'): id = key.split('add_item_text_option_')[1] txt = self.request.POST[key] if txt != '': text_option_ids.update({id: txt}) #now we need to find out if there are any cart items that have the exact #same set of options qs = CartItem.objects.filter(cart=cart_object).filter(product=product) found_cartitem_id = None merge = False for cartitem in qs: # for each CartItem in the Cart, get it's options and text options cartitemoptions = CartItemOption.objects.filter( cartitem=cartitem, option__in=option_ids) cartitemtxtoptions = CartItemTextOption.objects.filter( text_option__in=text_option_ids.keys(), text__in=text_option_ids.values()) if len(cartitemoptions) + len(cartitemtxtoptions) == ( len(option_ids) + len(text_option_ids)): found_cartitem_id = cartitem.id merge = True break #if we found a CartItem object that has the same options, we need #to select this one instead of just any CartItem that belongs to this #cart and this product. if found_cartitem_id: qs = CartItem.objects.filter(pk=found_cartitem_id) cart_item = cart_object.add_product(product, product_quantity, merge=merge, queryset=qs) cart_object.save() return self.post_success(product, cart_item, cart_object)
def delete(self, request, *args, **kwargs): """ Deletes one of the cartItems. This should be posted to a properly RESTful URL (that should contain the item's ID): http://example.com/shop/cart/item/12345 """ cart_object = get_or_create_cart(self.request) item_id = self.kwargs.get('id') cart_object.delete_item(item_id) return self.delete_success()
def create_order_object_from_cart(self): """Create an Order object form the current cart. Pass a reference to the Order on either the User object or the session. """ from shop.models import LazyOrder cart = get_or_create_cart(self.request) cart.update(self.request) order = LazyOrder().objects.create_from_cart(cart, self.request) add_order_to_request(self.request, order) return order
def delete(self, request, *args, **kwargs): cart_object = get_or_create_cart(self.request) item_id = self.kwargs.get('id') item = cart_object.delete_item(item_id) messages.add_message(request,messages.INFO, _('Product (%s) has been deleted from basket') % (item.product),extra_tags='basket_only') if cart_object.get_items_count() == 0: messages.add_message(request,messages.WARNING, _('You have deleted all products. Basket in empty now.'),extra_tags='basket_only') return self.redirect()
def _create_cart(self): self.product = DiaryProduct(isbn='1234567890', number_of_pages=100) self.product.name = 'test' self.product.slug = 'test' self.product.short_description = 'test' self.product.long_description = 'test' self.product.unit_price = Decimal('1.0') self.product.save() self.cart = get_or_create_cart(self.request) self.cart.add_product(self.product, 1) self.cart.save()
def test_having_two_filled_carts_returns_session_cart(self): setattr(self.request, "user", self.user) setattr(self.request, "session", {"cart_id": self.cart.pk}) database_cart = Cart.objects.create(user=self.user) product = Product.objects.create(name="pizza", slug="pizza", unit_price=0) CartItem.objects.create(cart=database_cart, quantity=1, product=product) CartItem.objects.create(cart=self.cart, quantity=1, product=product) ret = get_or_create_cart(self.request) self.assertNotEqual(ret, None) self.assertNotEqual(ret, database_cart) self.assertEqual(ret, self.cart) self.assertEqual(Cart.objects.filter(user=self.user).count(), 1)
def post(self, *args, **kwargs): ''' We expect to be posted with add_item_id and add_item_quantity set in the POST ''' item_id = self.request.POST['add_item_id'] quantity = self.request.POST['add_item_quantity'] item = Product.objects.get(pk=item_id) cart_object = get_or_create_cart(self.request) cart_object.add_product(item, quantity) cart_object.save() return self.add_to_cart_redirect
def render(self, context, instance, placeholder): state = {} cart_object = get_or_create_cart(context['request']) cart_object.update(state) context.update({'cart': cart_object}) context.update({'cart_items': cart_object.get_updated_cart_items()}) if 'formset' in context.get('request').POST: context.update(context.get('request').POST) else: context.update({'formset': get_cart_item_formset(cart_items=context['cart_items'])}) return context
def post(self, *args, **kwargs): cart_object = get_or_create_cart(self.request) f = CartForm(self.request.POST,instance=cart_object) if f.is_valid(): f.save() #Message only for EU cart clickers if cart_object.is_eu_cart: messages.add_message(self.request,messages.INFO, _('Remember that you need to have valid EU VAT number to claim 0% EU VAT rate'),extra_tags='basket_only') else: messages.add_message(self.request,messages.ERROR, _('Error changing VAT rate.'),extra_tags='basket_only') return redirect(self.request.POST.get('next'))
def get_context_data(self, **kwargs): ctx = super(ShopTemplateView, self).get_context_data(**kwargs) # put the latest order in the context only if it is completed order = get_order_from_request(self.request) if order and order.status == Order.COMPLETED: ctx.update({'order': order, }) # Empty the customers basket, to reflect that the purchase was # completed cart_object = get_or_create_cart(self.request) cart_object.empty() return ctx
def copy_item_to_cart(request, item_id): """ Copy an item from the active wishlist to the cart. """ wishlist = get_or_create_wishlist(request) items = WishlistItem.objects.filter(wishlist=wishlist, pk=item_id) if not items.exists(): raise exceptions.ObjectDoesNotExist('This item is not member of the active wishlist') product = items[0].product variation = items[0].variation cart = get_or_create_cart(request) cart.add_product(product, 1, variation) cart.save()
def render(self, context, instance, placeholder): request = context['request'] cart = get_or_create_cart(request) cart_items = cart.items.all() # hm, w ? t ?? f ??? # help(cart) print cart.total_quantity context.update({'cart': cart}) context.update({'cart_items': cart_items}) return context
def get_context_data(self, **kwargs): ctx = super(ShopTemplateView, self).get_context_data(**kwargs) # put the latest order in the context only if it is completed order = get_order_from_request(self.request) if order and order.status == Order.COMPLETED: ctx.update({ 'order': order, }) # Empty the customers basket, to reflect that the purchase was # completed cart_object = get_or_create_cart(self.request) cart_object.empty() return ctx
def get_context_data(self, **kwargs): # There is no get_context_data on super(), we inherit from the mixin! ctx = {} cart_object = get_or_create_cart(self.request) cart_object.update() ctx.update({'cart': cart_object}) cart_items = CartItem.objects.filter(cart=cart_object) final_items = [] for item in cart_items: item.update() final_items.append(item) ctx.update({'cart_items': final_items}) return ctx
def get_context_data(self, **kwargs): ctx = super(CartDetails, self).get_context_data(**kwargs) cart_object = get_or_create_cart(self.request) cart_object.update() ctx.update({'cart': cart_object}) cart_items = CartItem.objects.filter(cart=cart_object) final_items = [] for item in cart_items: item.update() final_items.append(item) ctx.update({'cart_items': final_items}) return ctx
def post(self, *args, **kwargs): ''' We expect to be posted with add_item_id and add_item_quantity set in the POST ''' item_id = self.request.POST['add_item_id'] quantity = self.request.POST['add_item_quantity'] item = Product.objects.get(pk=item_id) cart_object = get_or_create_cart(self.request) cart_object.add_product(item, quantity) cart_object.save() if self.request.is_ajax(): return self.add_to_cart_ajax_redirect return self.add_to_cart_normal_redirect
def post(self, *args, **kwargs): """ This is to *add* a new item to the cart. Optionally, you can pass it a quantity parameter to specify how many you wish to add at once (defaults to 1) """ product_id = self.request.POST['add_item_id'] product_quantity = self.request.POST.get('add_item_quantity') if not product_quantity: product_quantity = 1 product = Product.objects.get(pk=product_id) cart_object = get_or_create_cart(self.request, save=True) cart_item = cart_object.add_product(product, product_quantity) cart_object.save() return self.post_success(product, cart_item)
def post(self, *args, **kwargs): """ This is to *add* a new item to the cart. Optionally, you can pass it a quantity parameter to specify how many you wish to add at once (defaults to 1) """ try: product_id = int(self.request.POST['add_item_id']) product_quantity = int(self.request.POST.get('add_item_quantity', 1)) except (KeyError, ValueError): return HttpResponseBadRequest("The quantity and ID have to be numbers") product = Product.objects.get(pk=product_id, active=True) cart_object = get_or_create_cart(self.request, save=True) cart_item = cart_object.add_product(product, product_quantity) cart_object.save() return self.post_success(product, cart_item)
def put(self, *args, **kwargs): """ Update shopping cart items quantities. Data should be in update_item-ID=QTY form, where ID is id of cart item and QTY is quantity to set. """ field_prefix = 'update_item-' cart_item_fields = [ k for k in self.request.POST.keys() if k.startswith(field_prefix) ] cart_object = get_or_create_cart(self.request) for key in cart_item_fields: id = key[len(field_prefix):] cart_object.update_quantity(id, int(self.request.POST[key])) return self.put_success()
def post(self, request, *args, **kwargs): """ Update one of the cartItem's quantities. This requires a single ``item_quantity`` POST parameter, but should be posted to a properly RESTful URL (that should contain the item's ID): http://example.com/shop/cart/item/12345 """ cart_object = get_or_create_cart(self.request) item_id = self.kwargs.get('id') # NOTE: it seems logic to be in POST but as tests client shows # with PUT request, data is in GET variable # TODO: test in real client # quantity = self.request.POST['item_quantity'] quantity = self.request.POST['item_quantity'] cart_object.update_quantity(item_id, int(quantity)) return self.put_success()
def handle_billingshipping_forms(self, js_enabled, update_only, shipping_adress_form, billing_address_form): all_valid = False billingshipping_form = \ self.get_billing_and_shipping_selection_form() if billingshipping_form.is_valid(): self.request.session['payment_backend'] = \ billingshipping_form.cleaned_data['payment_method'] self.request.session['shipping_backend'] = \ billingshipping_form.cleaned_data['shipping_method'] shipping_choices_form = False payment_choices_form = False if billingshipping_form.is_valid(): shipping_method = billingshipping_form.cleaned_data[ 'shipping_method'] payment_method = billingshipping_form.cleaned_data[ 'payment_method'] if self.request.method == 'POST' and js_enabled: items = get_or_create_cart(self.request).items.all() shipping_choices_form = self.get_backend_choices_form( 'shipping', shipping_method, items, shipping_adress_form, billing_address_form) payment_choices_form = self.get_backend_choices_form( 'payment', payment_method, items, shipping_adress_form, billing_address_form) if not update_only: if shipping_choices_form: if shipping_choices_form.is_valid(): self.request.session[ 'shipping_choices'] = shipping_choices_form.cleaned_data if payment_choices_form: if payment_choices_form.is_valid(): self.request.session[ 'payment_choices'] = payment_choices_form.cleaned_data return (billingshipping_form, shipping_choices_form, payment_choices_form)
def post(self, *args, **kwargs): """ This is to *add* items in bulk to the cart. Optionally, you can pass it quantity parameters to specify how many you wish to add at once (defaults to 0) """ qty_field_prefix = 'add_item_quantity-' qty_fields = [ k for k in self.request.POST.keys() if k.startswith(qty_field_prefix) ] cart_object = get_or_create_cart(self.request) for key in qty_fields: id = key[len(qty_field_prefix):] product = Product.objects.get(pk=id) if int(self.request.POST[key]) > 0: cart_item = cart_object.add_product( product, int(self.request.POST[key])) cart_object.save() return HttpResponseRedirect(reverse('cart'))
def get_context(self, context): cart = get_or_create_cart(context['request']) return {'cart': cart}
def test_06_anonymous_user_is_like_no_user(self): setattr(self.request, 'user', AnonymousUser()) ret = get_or_create_cart(self.request) self.assertEqual(ret, None)
def test_05_passing_session_returns_proper_cart(self): setattr(self.request, 'session', {'cart_id': self.cart.id}) ret = get_or_create_cart(self.request) self.assertNotEqual(ret, None) self.assertEqual(ret, self.cart)
def test_04_passing_session_returns_new_cart(self): setattr(self.request, 'session', {}) ret = get_or_create_cart(self.request) self.assertNotEqual(ret, None) self.assertNotEqual(ret, self.cart)
def test_02_passing_user_returns_new_cart(self): setattr(self.request, 'user', self.user) ret = get_or_create_cart(self.request) self.assertNotEqual(ret, None) self.assertNotEqual(ret, self.cart)