def brand_category_page(request, category_slug, brand_slug): if category_slug != "-": try: category = Category.objects.get(slug=category_slug) except Category.DoesNotExist: raise Http404(_('Cagtegory "%s" does not exist') % category_slug) try: brand = category.brands.get(slug=brand_slug) except Brand.DoesNotExist: raise Http404( _('Brand "%s" does not exist in "%s"') % (brand_slug, category_slug)) else: category = None try: brand = Brand.objects.get(slug=brand_slug) except Brand.DoesNotExist: raise Http404( _('Brand "%s" does not exist in "%s"') % (brand_slug, category_slug)) if category: products = brand.active_products(category) else: products = brand.active_products() sale = find_best_auto_discount(products) context = RequestContext(request, { 'products': products, 'category': category, 'brand': brand, 'sale': sale, }) return render_to_response('product/brand/view_brand.html', context)
def brand_category_page(request, category_slug, brand_slug): if category_slug != "-": try: category = Category.objects.get(slug=category_slug) except Category.DoesNotExist: raise Http404(_('Cagtegory "%s" does not exist') % category_slug) try: brand = category.brands.get(slug=brand_slug) except Brand.DoesNotExist: raise Http404(_('Brand "%s" does not exist in "%s"') % (brand_slug, category_slug)) else: category = None try: brand = Brand.objects.get(slug=brand_slug) except Brand.DoesNotExist: raise Http404(_('Brand "%s" does not exist in "%s"') % (brand_slug, category_slug)) if category: products = brand.active_products(category) else: products = brand.active_products() sale = find_best_auto_discount(products) context = RequestContext(request, { 'products': products, 'category': category, 'brand': brand, 'sale': sale, }) return render_to_response('product/brand/view_brand.html', context)
def brand_category_page(request, category_slug, brand_slug): if category_slug != "-": try: category = Category.objects.get(slug=category_slug) except Category.DoesNotExist: raise Http404(_('Cagtegory "%s" does not exist') % category_slug) try: brand = category.brands.get(slug=brand_slug) except Brand.DoesNotExist: raise Http404( _('Brand "%s" does not exist in "%s"') % (brand_slug, category_slug) ) else: category = None try: brand = Brand.objects.get(slug=brand_slug) except Brand.DoesNotExist: raise Http404( _('Brand "%s" does not exist in "%s"') % (brand_slug, category_slug) ) if category: products = brand.active_products(category) else: products = brand.active_products() sale = find_best_auto_discount(products) context = {"products": products, "category": category, "brand": brand, "sale": sale} return render(request, "product/brand/view_brand.html", context)
def category_view(request, slug, parent_slugs='', template='base_category.html'): """Display the category, its child categories, and its products. Parameters: - slug: slug of category - parent_slugs: ignored """ try: category = Category.objects.get(slug=slug) products = category.active_products() sale = find_best_auto_discount(products) except Category.DoesNotExist: return bad_or_missing(request, _('The category you have requested does not exist.')) child_categories = category.get_all_children() ctx = { 'category': category, 'child_categories': child_categories, 'sale': sale, 'products': products, } index_prerender.send(Product, request=request, context=ctx, category=category, object_list=products) return render_to_response(template, RequestContext(request, ctx))
def category_view(request, slug, parent_slugs='', template='base_category.html'): """Display the category, its child categories, and its products. Parameters: - slug: slug of category - parent_slugs: ignored """ try: category = Category.objects.get(slug=slug) products = category.active_products() sale = find_best_auto_discount(products) except Category.DoesNotExist: return bad_or_missing( request, _('The category you have requested does not exist.')) child_categories = category.get_all_children() ctx = { 'category': category, 'child_categories': child_categories, 'sale': sale, 'products': products, } index_prerender.send(Product, request=request, context=ctx, category=category, object_list=products) return render_to_response(template, RequestContext(request, ctx))
def category_view(request, slug, parent_slugs="", template="base_category.html"): """Display the category, its child categories, and its products. Parameters: - slug: slug of category - parent_slugs: ignored """ try: category = Category.objects.get(slug=slug) products = category.active_products() sale = find_best_auto_discount(products) except Category.DoesNotExist: return bad_or_missing( request, _("The category you have requested does not exist.") ) child_categories = category.get_all_children() ctx = { "category": category, "child_categories": child_categories, "sale": sale, "products": products, } index_prerender.send( Product, request=request, context=ctx, category=category, object_list=products ) return render(request, template, ctx)
def category_view(request, slug, parent_slugs="", template="base_category.html"): """Display the category, its child categories, and its products. Parameters: - slug: slug of category - parent_slugs: ignored """ try: category = Category.objects.get(slug=slug) products = category.active_products() sale = find_best_auto_discount(products) except Category.DoesNotExist: return bad_or_missing( request, _("The category you have requested does not exist.")) child_categories = category.get_all_children() ctx = { "category": category, "child_categories": child_categories, "sale": sale, "products": products, } index_prerender.send(Product, request=request, context=ctx, category=category, object_list=products) return render(request, template, ctx)
def __init__(self, request, paymentmodule, *args, **kwargs): super(SimplePayShipForm, self).__init__(*args, **kwargs) self.order = None self.orderpayment = None try: self.tempCart = Cart.objects.from_request(request) if self.tempCart.numItems > 0: products = [item.product for item in self.tempCart.cartitem_set.all()] sale = find_best_auto_discount(products) if sale: self.fields['discount'].initial = sale.code except Cart.DoesNotExist: self.tempCart = None try: self.tempContact = Contact.objects.from_request(request) except Contact.DoesNotExist: self.tempContact = None if 'default_view_tax' in kwargs: default_view_tax = kwargs['default_view_tax'] else: default_view_tax = config_value('TAX', 'TAX_SHIPPING') shipping_choices, shipping_dict = _get_shipping_choices(request, paymentmodule, self.tempCart, self.tempContact, default_view_tax=default_view_tax) self.fields['shipping'].choices = shipping_choices self.shipping_dict = shipping_dict signals.payment_form_init.send(SimplePayShipForm, form=self)
def get_product(request, category_slug, brand_slug, product_slug, selected_options=(), include_tax=NOTSET, default_view_tax=NOTSET): """Basic product view""" try: product = Product.objects.get_by_site(active=True, slug=product_slug) except Product.DoesNotExist: return bad_or_missing(request, _('The product you have requested does not exist.')) try: category = Category.objects.get(slug=category_slug) except Category.DoesNotExist: category = None brand = Brand.objects.get(slug=brand_slug) if default_view_tax == NOTSET: default_view_tax = config_value('TAX', 'DEFAULT_VIEW_TAX') if default_view_tax: include_tax = True elif include_tax == NOTSET: include_tax = default_view_tax if default_view_tax: include_tax = True subtype_names = product.get_subtypes() if 'ProductVariation' in subtype_names: selected_options = product.productvariation.unique_option_ids # Display the ConfigurableProduct that this ProductVariation belongs to. product = product.productvariation.parent.product subtype_names = product.get_subtypes() best_discount = find_best_auto_discount(product) extra_context = { 'product': product, 'category': category, 'brand': brand, 'default_view_tax': default_view_tax, 'sale': best_discount, } # Get the template context from the Product. extra_context = product.add_template_context( context=extra_context, request=request, selected_options=selected_options, include_tax=include_tax, default_view_tax=default_view_tax ) if include_tax: tax_amt = get_tax(request.user, product, 1) extra_context['product_tax'] = tax_amt extra_context['price_with_tax'] = product.unit_price + tax_amt template = find_product_template(product, producttypes=subtype_names) context = RequestContext(request, extra_context) return http.HttpResponse(template.render(context))
def sale_price(product): """Returns the product unit price with the best auto discount applied.""" disc = find_best_auto_discount(product) price = product.unit_price if disc: return calc_by_percentage(price, disc.percentage) else: return price
def untaxed_sale_price(product): """Returns the product unit price with the best auto discount applied.""" discount = find_best_auto_discount(product) price = product.unit_price if discount and discount.valid_for_product(cartitem.product): price = calc_by_percentage(price, disc.percentage) return price
def pay_ship_render_form(request, form, template, payment_module, cart): template = lookup_template(payment_module, template) if cart.numItems > 0: products = [item.product for item in cart.cartitem_set.all()] sale = find_best_auto_discount(products) else: sale = None ctx = {"form": form, "sale": sale, "PAYMENT_LIVE": payment_live(payment_module)} return render(request, template, ctx)
def pay_ship_render_form(request, form, template, payment_module, cart): template = lookup_template(payment_module, template) if cart.numItems > 0: products = [item.product for item in cart.cartitem_set.all()] sale = find_best_auto_discount(products) else: sale = None ctx = RequestContext(request, { 'form': form, 'sale' : sale, 'PAYMENT_LIVE': payment_live(payment_module)}) return render_to_response(template, ctx)
def pay_ship_render_form(request, form, template, payment_module, cart): template = lookup_template(payment_module, template) if cart.numItems > 0: products = [item.product for item in cart.cartitem_set.all()] sale = find_best_auto_discount(products) else: sale = None ctx = RequestContext(request, { 'form': form, 'sale': sale, 'PAYMENT_LIVE': payment_live(payment_module) }) return render_to_response(template, ctx)
def pay_ship_render_form(request, form, template, payment_module, cart): template = lookup_template(payment_module, template) if cart.numItems > 0: products = [item.product for item in cart.cartitem_set.all()] sale = find_best_auto_discount(products) else: sale = None ctx = { "form": form, "sale": sale, "PAYMENT_LIVE": payment_live(payment_module) } return render(request, template, ctx)
def brand_page(request, brandname): try: brand = Brand.objects.by_slug(brandname) except Brand.DoesNotExist: raise Http404(_('Brand "%s" does not exist') % brandname) products = brand.active_products() sale = find_best_auto_discount(products) ctx = { 'brand': brand, 'products': products, 'sale': sale, } ctx = RequestContext(request, ctx) return render_to_response('product/brand/view_brand.html', ctx)
def brand_category_page(request, brandname, catname): try: cat = BrandCategory.objects.by_slug(brandname, catname) except Brand.DoesNotExist: raise Http404(_('Brand "%s" does not exist') % brandname) except BrandCategory.DoesNotExist: raise Http404(_('No category "%s" in brand "%s"') % (catname, brandname)) products = list(cat.active_products()) sale = find_best_auto_discount(products) ctx = RequestContext(request, { 'brand' : cat, 'sale' : sale, }) return render_to_response('product/brand/view_brand.html', ctx)
def __init__(self, request, paymentmodule, *args, **kwargs): super(SimplePayShipForm, self).__init__(*args, **kwargs) self.order = None self.orderpayment = None self.request = request try: self.tempCart = Cart.objects.from_request(request) if self.tempCart.numItems > 0: products = [ item.product for item in self.tempCart.cartitem_set.all() ] sale = find_best_auto_discount(products) if sale: self.fields["discount"].initial = sale.code except Cart.DoesNotExist: self.tempCart = None try: self.tempContact = Contact.objects.from_request(request) except Contact.DoesNotExist: self.tempContact = None if "default_view_tax" in kwargs: default_view_tax = kwargs["default_view_tax"] else: default_view_tax = config_value("TAX", "TAX_SHIPPING") shipping_choices, shipping_dict = _get_shipping_choices( request, paymentmodule, self.tempCart, self.tempContact, default_view_tax=default_view_tax, ) self.fields["shipping"].choices = shipping_choices self.shipping_dict = shipping_dict signals.payment_form_init.send(SimplePayShipForm, form=self)
def display(request, cart=None, error_message='', default_view_tax=NOTSET): """Display the items in the cart.""" if default_view_tax == NOTSET: default_view_tax = config_value('TAX', 'DEFAULT_VIEW_TAX') if not cart: cart = Cart.objects.from_request(request) if cart.numItems > 0: products = [item.product for item in cart.cartitem_set.all()] sale = find_best_auto_discount(products) else: sale = None context = RequestContext(request, { 'cart': cart, 'error_message': error_message, 'default_view_tax' : default_view_tax, 'sale' : sale, }) return render_to_response('base_cart.html', context)
def display(request, cart=None, error_message='', default_view_tax=NOTSET): """Display the items in the cart.""" if default_view_tax == NOTSET: default_view_tax = config_value('TAX', 'DEFAULT_VIEW_TAX') if not cart: cart = Cart.objects.from_request(request) if cart.numItems > 0: products = [item.product for item in cart.cartitem_set.all()] sale = find_best_auto_discount(products) else: sale = None context = RequestContext( request, { 'cart': cart, 'error_message': error_message, 'default_view_tax': default_view_tax, 'sale': sale }) return render_to_response('base_cart.html', context)
def display(request, cart=None, error_message="", default_view_tax=NOTSET): """Display the items in the cart.""" if default_view_tax == NOTSET: default_view_tax = config_value("TAX", "DEFAULT_VIEW_TAX") if not cart: cart = Cart.objects.from_request(request) if cart.numItems > 0: products = [item.product for item in cart.cartitem_set.all()] sale = find_best_auto_discount(products) else: sale = None # Try to get the user's country from the session country_iso2_code = request.GET.get( "side-cart-country", request.session.get("shipping_country", None)) try: country = Country.objects.get(iso2_code=country_iso2_code) except Country.DoesNotExist: country = None # Try to get the user's country from their contact (if they have one) try: contact = Contact.objects.from_request(request) if country is None: country = contact.shipping_address.country else: # We have a country set from the form, so this is a cheap # and nasty way to get a dummy contact instead... raise Contact.DoesNotExist except (AttributeError, IndexError, Contact.DoesNotExist): pass # Try to look up the user's country from their IP address if country is None and hasattr(settings, "GEOIP_PATH"): ip = get_real_ip(request) if ip: geoip = GeoIP2() try: ip_country = geoip.country(ip) except AddressNotFoundError: pass else: try: country = Country.objects.get( active=True, iso2_code=ip_country["country_code"]) except Country.DoesNotExist: country = None # If the user doesn't have a contact, make a dummy contact so # we can work out the shipping if country is None: config = Config.objects.get_current() try: country = config.country except Country.DoesNotExist: country = None class DummyContact: if country: shipping_address = AddressBook(country_id=country.id) contact = DummyContact() if country: request.session["shipping_country"] = country.iso2_code # Calculate the shipping cost try: __, shipping_dict = _get_shipping_choices(request, {}, cart, contact) cheapest_shipping = min(shipping_dict.values()) except (AttributeError, ValueError): cheapest_shipping = Decimal("0.00") try: cheapest_shipping = convert_to_currency(cheapest_shipping, cart.currency.iso_4217_code) except Currency.DoesNotExist: pass try: display_shipping = money_format(cheapest_shipping, cart.currency.iso_4217_code) except Currency.DoesNotExist: display_shipping = cheapest_shipping try: display_total = money_format(cart.total + cheapest_shipping, cart.currency.iso_4217_code) except Currency.DoesNotExist: display_total = cart.total + cheapest_shipping context = { "cart": cart, "error_message": error_message, "default_view_tax": default_view_tax, "sale": sale, "cheapest_shipping": display_shipping, "display_total": display_total, "country": country, } return render(request, "base_cart.html", context)
def get_product( request, category_slug, brand_slug, product_slug, selected_options=(), include_tax=NOTSET, default_view_tax=NOTSET, ): """Basic product view""" try: product = Product.objects.active().get(slug=product_slug) except Product.DoesNotExist: return bad_or_missing( request, _("The product you have requested does not exist.")) try: category = Category.objects.get(slug=category_slug) except Category.DoesNotExist: category = None brand = Brand.objects.get(slug=brand_slug) if default_view_tax == NOTSET: default_view_tax = config_value("TAX", "DEFAULT_VIEW_TAX") if default_view_tax: include_tax = True elif include_tax == NOTSET: include_tax = default_view_tax if default_view_tax: include_tax = True subtype_names = product.get_subtypes() if "ProductVariation" in subtype_names: selected_options = product.productvariation.unique_option_ids # Display the ConfigurableProduct that this ProductVariation belongs to. product = product.productvariation.parent.product subtype_names = product.get_subtypes() best_discount = find_best_auto_discount(product) context = { "product": product, "category": category, "brand": brand, "default_view_tax": default_view_tax, "sale": best_discount, } # Get the template context from the Product. context = product.add_template_context( context=context, request=request, selected_options=selected_options, include_tax=include_tax, default_view_tax=default_view_tax, ) if include_tax: tax_amt = get_tax(request.user, product, 1) context["product_tax"] = tax_amt context["price_with_tax"] = product.unit_price + tax_amt price = context["price_with_tax"] else: price = product.unit_price context["all_prices"] = [{ "currency": currency.iso_4217_code, "price": convert_to_currency(price, currency.iso_4217_code), } for currency in Currency.objects.filter(accepted=True)] template = find_product_template(product, producttypes=subtype_names) return TemplateResponse(request, template, context)
def display(request, cart=None, error_message="", default_view_tax=NOTSET): """Display the items in the cart.""" if default_view_tax == NOTSET: default_view_tax = config_value("TAX", "DEFAULT_VIEW_TAX") if not cart: cart = Cart.objects.from_request(request) if cart.numItems > 0: products = [item.product for item in cart.cartitem_set.all()] sale = find_best_auto_discount(products) else: sale = None # Try to get the user's country from the session country_iso2_code = request.GET.get( "side-cart-country", request.session.get("shipping_country", None) ) try: country = Country.objects.get(iso2_code=country_iso2_code) except Country.DoesNotExist: country = None # Try to get the user's country from their contact (if they have one) try: contact = Contact.objects.from_request(request) if country is None: country = contact.shipping_address.country else: # We have a country set from the form, so this is a cheap # and nasty way to get a dummy contact instead... raise Contact.DoesNotExist except (AttributeError, IndexError, Contact.DoesNotExist): pass # Try to look up the user's country from their IP address if country is None and hasattr(settings, "GEOIP_PATH"): ip = get_real_ip(request) if ip: geoip = GeoIP2() try: ip_country = geoip.country(ip) except AddressNotFoundError: pass else: try: country = Country.objects.get( active=True, iso2_code=ip_country["country_code"] ) except Country.DoesNotExist: country = None # If the user doesn't have a contact, make a dummy contact so # we can work out the shipping if country is None: config = Config.objects.get_current() try: country = config.country except Country.DoesNotExist: country = None class DummyContact: if country: shipping_address = AddressBook(country_id=country.id) contact = DummyContact() if country: request.session["shipping_country"] = country.iso2_code # Calculate the shipping cost try: __, shipping_dict = _get_shipping_choices(request, {}, cart, contact) cheapest_shipping = min(shipping_dict.values()) except (AttributeError, ValueError): cheapest_shipping = Decimal("0.00") try: cheapest_shipping = convert_to_currency( cheapest_shipping, cart.currency.iso_4217_code ) except Currency.DoesNotExist: pass try: display_shipping = money_format(cheapest_shipping, cart.currency.iso_4217_code) except Currency.DoesNotExist: display_shipping = cheapest_shipping try: display_total = money_format( cart.total + cheapest_shipping, cart.currency.iso_4217_code ) except Currency.DoesNotExist: display_total = cart.total + cheapest_shipping context = { "cart": cart, "error_message": error_message, "default_view_tax": default_view_tax, "sale": sale, "cheapest_shipping": display_shipping, "display_total": display_total, "country": country, } return render(request, "base_cart.html", context)
def get_product(request, category_slug, brand_slug, product_slug, selected_options=(), include_tax=NOTSET, default_view_tax=NOTSET): """Basic product view""" try: product = Product.objects.get_by_site(active=True, slug=product_slug) except Product.DoesNotExist: return bad_or_missing( request, _('The product you have requested does not exist.')) try: category = Category.objects.get(slug=category_slug) except Category.DoesNotExist: category = None brand = Brand.objects.get(slug=brand_slug) if default_view_tax == NOTSET: default_view_tax = config_value('TAX', 'DEFAULT_VIEW_TAX') if default_view_tax: include_tax = True elif include_tax == NOTSET: include_tax = default_view_tax if default_view_tax: include_tax = True subtype_names = product.get_subtypes() if 'ProductVariation' in subtype_names: selected_options = product.productvariation.unique_option_ids # Display the ConfigurableProduct that this ProductVariation belongs to. product = product.productvariation.parent.product subtype_names = product.get_subtypes() best_discount = find_best_auto_discount(product) extra_context = { 'product': product, 'category': category, 'brand': brand, 'default_view_tax': default_view_tax, 'sale': best_discount, } # Get the template context from the Product. extra_context = product.add_template_context( context=extra_context, request=request, selected_options=selected_options, include_tax=include_tax, default_view_tax=default_view_tax) if include_tax: tax_amt = get_tax(request.user, product, 1) extra_context['product_tax'] = tax_amt extra_context['price_with_tax'] = product.unit_price + tax_amt template = find_product_template(product, producttypes=subtype_names) context = RequestContext(request, extra_context) return http.HttpResponse(template.render(context))
def productvariation_details(product, include_tax, user, create=False): """Build the product variation details, for conversion to javascript. Returns variation detail dictionary built like so: details = { "OPTION_KEY" : { "SLUG": "Variation Slug", "PRICE" : {"qty" : "$price", [...]}, "SALE" : {"qty" : "$price", [...]}, "TAXED" : "$taxed price", # omitted if no taxed price requested "QTY" : 1 }, [...] } """ config = Config.objects.get_current() ignore_stock = config.no_stock_checkout discount = find_best_auto_discount(product) use_discount = discount and discount.percentage > 0 if include_tax: taxer = get_taxprocessor(user) tax_class = product.taxClass details = {'SALE' : use_discount} curr = config_value('SHOP', 'CURRENCY') curr = curr.replace("_", " ") variations = ProductPriceLookup.objects.filter(parentid=product.id) if variations.count() == 0: if create: log.debug('Creating price lookup for %s', product) ProductPriceLookup.objects.smart_create_for_product(product) variations = ProductPriceLookup.objects.filter(parentid=product.id) else: log.warning('You must run satchmo_rebuild_pricing and add it to a cron-job to run every day, or else the product details will not work for product detail pages.') for detl in variations: key = detl.key if details.has_key(key): detail = details[key] else: detail = {} detail['SLUG'] = detl.productslug if not detl.active: qty = -1 elif ignore_stock: qty = 10000 else: qty = detl.items_in_stock detail['QTY'] = qty detail['PRICE'] = {} if use_discount: detail['SALE'] = {} if include_tax: detail['TAXED'] = {} if use_discount: detail['TAXED_SALE'] = {} details[key] = detail price = detl.dynamic_price detail['PRICE'][detl.quantity] = moneyfmt(price, curr=curr) if use_discount: detail['SALE'][detl.quantity] = moneyfmt(calc_by_percentage(price, discount.percentage), curr=curr) if include_tax: tax_price = taxer.by_price(tax_class, price) + price detail['TAXED'][detl.quantity] = moneyfmt(tax_price, curr=curr) detail['TAXED_SALE'][detl.quantity] = moneyfmt(calc_by_percentage(tax_price, discount.percentage), curr=curr) return details
def get_product( request, category_slug, brand_slug, product_slug, selected_options=(), include_tax=NOTSET, default_view_tax=NOTSET, ): """Basic product view""" try: product = Product.objects.get_by_site(active=True, slug=product_slug) except Product.DoesNotExist: return bad_or_missing( request, _("The product you have requested does not exist.") ) try: category = Category.objects.get(slug=category_slug) except Category.DoesNotExist: category = None brand = Brand.objects.get(slug=brand_slug) if default_view_tax == NOTSET: default_view_tax = config_value("TAX", "DEFAULT_VIEW_TAX") if default_view_tax: include_tax = True elif include_tax == NOTSET: include_tax = default_view_tax if default_view_tax: include_tax = True subtype_names = product.get_subtypes() if "ProductVariation" in subtype_names: selected_options = product.productvariation.unique_option_ids # Display the ConfigurableProduct that this ProductVariation belongs to. product = product.productvariation.parent.product subtype_names = product.get_subtypes() best_discount = find_best_auto_discount(product) context = { "product": product, "category": category, "brand": brand, "default_view_tax": default_view_tax, "sale": best_discount, } # Get the template context from the Product. context = product.add_template_context( context=context, request=request, selected_options=selected_options, include_tax=include_tax, default_view_tax=default_view_tax, ) if include_tax: tax_amt = get_tax(request.user, product, 1) context["product_tax"] = tax_amt context["price_with_tax"] = product.unit_price + tax_amt price = context["price_with_tax"] else: price = product.unit_price context["all_prices"] = [ { "currency": currency.iso_4217_code, "price": convert_to_currency(price, currency.iso_4217_code), } for currency in Currency.objects.filter(accepted=True) ] template = find_product_template(product, producttypes=subtype_names) return TemplateResponse(request, template, context)