def settings(request): shop_config = Config.get_shop_config() cart = Cart.get_session_cart(request) all_categories = Category.objects.all() # handle secure requests media_url = site_settings.MEDIA_URL secure = request_is_secure(request) if secure: try: media_url = site_settings.MEDIA_SECURE_URL except AttributeError: media_url = media_url.replace('http://', 'https://') return { 'shop_base': site_settings.SHOP_BASE, 'shop': shop_config, 'shop_name': shop_config.store_name, 'media_url': media_url, 'cart_count': cart.numItems, 'cart': cart, 'categories': all_categories, 'is_secure': secure, 'request': request, }
def settings(request): shop_config = Config.get_shop_config() cart = Cart.get_session_cart(request) all_categories = Category.objects.all() # handle secure requests media_url = site_settings.MEDIA_URL secure = request_is_secure(request) if secure: try: media_url = site_settings.MEDIA_SECURE_URL except AttributeError: media_url = media_url.replace("http://", "https://") return { "shop_base": site_settings.SHOP_BASE, "shop": shop_config, "shop_name": shop_config.store_name, "media_url": media_url, "cart_count": cart.numItems, "cart": cart, "categories": all_categories, "is_secure": secure, "request": request, }
def add_ajax(request, id=0, template="json.html"): data = {'errors': []} product = None productname = request.POST['productname'] log.debug('CART_AJAX: slug=%s', productname) try: product = Product.objects.get(slug=request.POST['productname']) if 'ConfigurableProduct' in product.get_subtypes(): log.debug('Got a configurable product, trying by option') # This happens when productname cannot be updated by javascript. cp = product.configurableproduct chosenOptions = optionset_from_post(cp, request.POST) product = cp.get_product_from_options(chosenOptions) except Product.DoesNotExist: log.warn("Could not find product: %s", productname) product = None if not product: data['errors'].append( ('product', _('The product you have requested does not exist.'))) else: data['id'] = product.id data['name'] = product.name try: quantity = int(request.POST['quantity']) if quantity < 0: data['errors'].append(('quantity', _('Choose a quantity.'))) except ValueError: data['errors'].append(('quantity', _('Choose a whole number.'))) tempCart = Cart.get_session_cart(request, create=True) if not data['errors']: tempCart.add_item(product, number_added=quantity) request.session['cart'] = tempCart.id data['results'] = _('Success') else: data['results'] = _('Error') data['cart_count'] = tempCart.numItems encoded = JSONEncoder().encode(data) log.debug('CART AJAX: %s', data) return render_to_response(template, {'json': encoded})
def add_ajax(request, id=0, template="json.html"): data = {'errors': []} product = None productname = request.POST['productname']; log.debug('CART_AJAX: slug=%s', productname) try: product = Product.objects.get(slug=request.POST['productname']) if 'ConfigurableProduct' in product.get_subtypes(): log.debug('Got a configurable product, trying by option') # This happens when productname cannot be updated by javascript. cp = product.configurableproduct chosenOptions = optionset_from_post(cp, request.POST) product = cp.get_product_from_options(chosenOptions) except Product.DoesNotExist: log.warn("Could not find product: %s", productname) product = None if not product: data['errors'].append(('product', _('The product you have requested does not exist.'))) else: data['id'] = product.id data['name'] = product.name try: quantity = int(request.POST['quantity']) if quantity < 0: data['errors'].append(('quantity', _('Choose a quantity.'))) except ValueError: data['errors'].append(('quantity', _('Choose a whole number.'))) tempCart = Cart.get_session_cart(request, create=True) if not data['errors']: tempCart.add_item(product, number_added=quantity) request.session['cart'] = tempCart.id data['results'] = _('Success') else: data['results'] = _('Error') data['cart_count'] = tempCart.numItems encoded = JSONEncoder().encode(data) log.debug('CART AJAX: %s', data) return render_to_response(template, {'json' : encoded})