Exemplo n.º 1
0
def get_product_context(request, product, language=None):   # noqa (C901)
    """
    Get product context

    Used in `shuup.front.views.product:ProductDetailView`

    :return: A context dict containing everything needed to render product view
    :rtype: dict
    """
    if not language:
        language = get_language()

    shop_product = product.get_shop_instance(request.shop)
    context = {}
    context["product"] = product
    context["category"] = shop_product.primary_category
    context["orderability_errors"] = list(shop_product.get_orderability_errors(
        supplier=None, quantity=1, customer=request.customer, ignore_minimum=True))
    context["variation_children"] = []
    if product.mode == ProductMode.SIMPLE_VARIATION_PARENT:
        context["variation_children"] = cache_product_things(
            request,
            sorted(
                product.variation_children.language(language).all(),
                key=lambda p: get_string_sort_order(p.variation_name or p.name)
            )
        )
        context["orderable_variation_children"] = []
        for p in context["variation_children"]:
            try:
                if p.get_shop_instance(request.shop).is_orderable(supplier=None, customer=request.customer, quantity=1):
                    context["orderable_variation_children"].append(p)
            except ShopProduct.DoesNotExist:
                pass

    elif product.mode == ProductMode.VARIABLE_VARIATION_PARENT:
        variation_variables = product.variation_variables.all().prefetch_related("values")
        orderable_children, is_orderable = get_orderable_variation_children(product, request, variation_variables)
        context["orderable_variation_children"] = orderable_children
        context["variation_orderable"] = is_orderable
        context["variation_variables"] = variation_variables
    elif product.is_container():
        children = product.get_all_package_children().translated().order_by("translations__name")
        context["package_children"] = cache_product_things(request, children)

    context["shop_product"] = shop_product
    context["attributes"] = product.attributes.filter(
        attribute__visibility_mode=AttributeVisibility.SHOW_ON_PRODUCT_PAGE)
    context["primary_image"] = shop_product.public_primary_image
    context["images"] = shop_product.public_images
    context["order_form"] = _get_order_form(request, context, product, language)

    for provide_object in get_provide_objects("product_context_extra"):
        provider = provide_object(request, product, language)
        if provider.provides_extra_context():
            context.update(provider.extra_context)

    return context
Exemplo n.º 2
0
def get_product_context(request, product, language=None):
    """
    Get product context

    Used in `shuup.front.views.product:ProductDetailView`

    :return: A context dict containing everything needed to render product view
    :rtype: dict
    """
    if not language:
        language = get_language()

    shop_product = product.get_shop_instance(request.shop)
    context = {}
    context["product"] = product
    context["category"] = shop_product.primary_category
    context["orderability_errors"] = list(shop_product.get_orderability_errors(
        supplier=None, quantity=1, customer=request.customer, ignore_minimum=True))
    context["variation_children"] = []
    if product.mode == ProductMode.SIMPLE_VARIATION_PARENT:
        context["variation_children"] = cache_product_things(
            request,
            sorted(
                product.variation_children.language(language).all(),
                key=lambda p: get_string_sort_order(p.variation_name or p.name)
            )
        )
        context["orderable_variation_children"] = [
            p for p in context["variation_children"]
            if p.get_shop_instance(request.shop).is_orderable(supplier=None, customer=request.customer, quantity=1)
        ]
    elif product.mode == ProductMode.VARIABLE_VARIATION_PARENT:
        variation_variables = product.variation_variables.all().prefetch_related("values")
        orderable_children, is_orderable = get_orderable_variation_children(product, request, variation_variables)
        context["orderable_variation_children"] = orderable_children
        context["variation_orderable"] = is_orderable
        context["variation_variables"] = variation_variables
    elif product.is_container():
        children = product.get_all_package_children().translated().order_by("translations__name")
        context["package_children"] = cache_product_things(request, children)

    context["shop_product"] = shop_product
    context["attributes"] = product.attributes.filter(
        attribute__visibility_mode=AttributeVisibility.SHOW_ON_PRODUCT_PAGE)
    context["primary_image"] = shop_product.public_primary_image
    context["images"] = shop_product.public_images
    context["order_form"] = _get_order_form(request, context, product, language)

    for provide_object in get_provide_objects("product_context_extra"):
        provider = provide_object(request, product, language)
        if provider.provides_extra_context():
            context.update(provider.extra_context)

    return context
Exemplo n.º 3
0
    def get_context_data(self, **kwargs):
        context = super(ProductDetailView, self).get_context_data(**kwargs)
        language = self.language = get_language()
        product = self.object
        context["category"] = self.shop_product.primary_category
        context["orderability_errors"] = list(self.shop_product.get_orderability_errors(
            supplier=None,
            quantity=1,
            customer=self.request.customer,
            ignore_minimum=True
        ))
        context["variation_children"] = []
        if product.mode == ProductMode.SIMPLE_VARIATION_PARENT:
            context["variation_children"] = cache_product_things(
                self.request,
                sorted(
                    product.variation_children.language(language).all(),
                    key=lambda p: get_string_sort_order(p.variation_name or p.name)
                )
            )
            context["orderable_variation_children"] = [
                p for p in context["variation_children"]
                if p.get_shop_instance(self.request.shop).is_orderable(
                    supplier=None,
                    customer=self.request.customer,
                    quantity=1
                )
            ]
        elif product.mode == ProductMode.VARIABLE_VARIATION_PARENT:
            context["variation_variables"] = product.variation_variables.all().prefetch_related("values")
        elif product.mode == ProductMode.PACKAGE_PARENT:
            children = (
                product.get_all_package_children()
                .translated()
                .order_by("translations__name")
            )
            context["package_children"] = cache_product_things(self.request, children)

        context["shop_product"] = self.shop_product

        primary_image = None
        if self.shop_product.shop_primary_image_id:
            primary_image = self.shop_product.shop_primary_image
        elif product.primary_image_id:
            primary_image = self.shop_product.primary_image

        context["primary_image"] = primary_image
        context["images"] = self.shop_product.images.all()

        # TODO: Maybe add hook for ProductDetailView get_context_data?
        # dispatch_hook("get_context_data", view=self, context=context)

        return context
Exemplo n.º 4
0
def get_product_context(request, product, language=None):
    """
    Get product context

    Used in `shuup.front.views.product:ProductDetailView`

    :return: A context dict containing everything needed to render product view
    :rtype: dict
    """

    if not language:
        language = get_language()

    shop_product = product.get_shop_instance(request.shop)
    context = {}
    context["product"] = product
    context["category"] = shop_product.primary_category
    context["orderability_errors"] = list(
        shop_product.get_orderability_errors(supplier=None,
                                             quantity=1,
                                             customer=request.customer,
                                             ignore_minimum=True))
    context["variation_children"] = []
    if product.mode == ProductMode.SIMPLE_VARIATION_PARENT:
        context["variation_children"] = cache_product_things(
            request,
            sorted(product.variation_children.language(language).all(),
                   key=lambda p: get_string_sort_order(p.variation_name or p.
                                                       name)))
        context["orderable_variation_children"] = [
            p for p in context["variation_children"]
            if p.get_shop_instance(request.shop).is_orderable(
                supplier=None, customer=request.customer, quantity=1)
        ]
    elif product.mode == ProductMode.VARIABLE_VARIATION_PARENT:
        context["variation_variables"] = product.variation_variables.all(
        ).prefetch_related("values")
    elif product.mode == ProductMode.PACKAGE_PARENT:
        children = product.get_all_package_children().translated().order_by(
            "translations__name")
        context["package_children"] = cache_product_things(request, children)

    context["shop_product"] = shop_product
    context["attributes"] = product.attributes.filter(
        attribute__visibility_mode=AttributeVisibility.SHOW_ON_PRODUCT_PAGE)
    context["primary_image"] = shop_product.public_primary_image
    context["images"] = shop_product.public_images
    return context
Exemplo n.º 5
0
    def get_context_data(self, **kwargs):
        context = super(ProductDetailView, self).get_context_data(**kwargs)
        language = self.language = get_language()
        product = self.object
        context["category"] = self.shop_product.primary_category
        context["orderability_errors"] = list(
            self.shop_product.get_orderability_errors(
                supplier=None,
                quantity=1,
                customer=self.request.customer,
                ignore_minimum=True))
        context["variation_children"] = []
        if product.mode == ProductMode.SIMPLE_VARIATION_PARENT:
            context["variation_children"] = cache_product_things(
                self.request,
                sorted(product.variation_children.language(language).all(),
                       key=lambda p: get_string_sort_order(p.variation_name or
                                                           p.name)))
            context["orderable_variation_children"] = [
                p for p in context["variation_children"]
                if p.get_shop_instance(self.request.shop).is_orderable(
                    supplier=None, customer=self.request.customer, quantity=1)
            ]
        elif product.mode == ProductMode.VARIABLE_VARIATION_PARENT:
            context["variation_variables"] = product.variation_variables.all(
            ).prefetch_related("values")
        elif product.mode == ProductMode.PACKAGE_PARENT:
            children = (product.get_all_package_children().translated().
                        order_by("translations__name"))
            context["package_children"] = cache_product_things(
                self.request, children)

        context["shop_product"] = self.shop_product

        primary_image = None
        if self.shop_product.shop_primary_image_id:
            primary_image = self.shop_product.shop_primary_image
        elif product.primary_image_id:
            primary_image = self.shop_product.primary_image

        context["primary_image"] = primary_image
        context["images"] = self.shop_product.images.all()

        # TODO: Maybe add hook for ProductDetailView get_context_data?
        # dispatch_hook("get_context_data", view=self, context=context)

        return context