Exemple #1
0
    def make_package(self, package_def):
        if self.mode != ProductMode.NORMAL:
            raise ImpossibleProductModeException(_(
                "Product is currently not a normal product, can't turn into package"
            ),
                                                 code="abnormal")

        for child_product, quantity in six.iteritems(package_def):
            if child_product.pk == self.pk:
                raise ImpossibleProductModeException(
                    _("Package can't contain itself"), code="content")
            # :type child_product: Product
            if child_product.is_variation_parent():
                raise ImpossibleProductModeException(
                    _("Variation parents can not belong into a package"),
                    code="abnormal")
            if child_product.is_container():
                raise ImpossibleProductModeException(
                    _("Packages can't be nested"), code="multilevel")
            if quantity <= 0:
                raise ImpossibleProductModeException(
                    _("Quantity %s is invalid") % quantity, code="quantity")
            ProductPackageLink.objects.create(parent=self,
                                              child=child_product,
                                              quantity=quantity)
        self.verify_mode()
Exemple #2
0
    def _raise_if_cant_link_to_parent(self, parent, variables):
        """
        Validates relation possibility for `self.link_to_parent()`

        :param parent: parent product of self
        :type parent: Product
        :param variables:
        :type variables: dict|None
        """
        if parent.is_variation_child():
            raise ImpossibleProductModeException(_(
                "Multilevel parentage hierarchies aren't supported (parent is a child already)"
            ),
                                                 code="multilevel")
        if parent.mode == ProductMode.VARIABLE_VARIATION_PARENT and not variables:
            raise ImpossibleProductModeException(_(
                "Parent is a variable variation parent, yet variables were not passed"
            ),
                                                 code="no_variables")
        if parent.mode == ProductMode.SIMPLE_VARIATION_PARENT and variables:
            raise ImpossibleProductModeException(
                "Parent is a simple variation parent, yet variables were passed",
                code="extra_variables")
        if self.mode == ProductMode.SIMPLE_VARIATION_PARENT:
            raise ImpossibleProductModeException(_(
                "Multilevel parentage hierarchies aren't supported (this product is a simple variation parent)"
            ),
                                                 code="multilevel")
        if self.mode == ProductMode.VARIABLE_VARIATION_PARENT:
            raise ImpossibleProductModeException(_(
                "Multilevel parentage hierarchies aren't supported (this product is a variable variation parent)"
            ),
                                                 code="multilevel")