def save(self):
        parent_product = self.parent_product
        current_products = set(parent_product.variation_children.all())
        selected_products, unlinked_products = self.get_selected_and_unlinked()

        with atomic():
            products_to_add = selected_products - current_products
            products_to_remove = current_products & unlinked_products
            for child_product in products_to_remove:
                child_product.unlink_from_parent()
            for child_product in products_to_add:
                try:
                    child_product.link_to_parent(parent_product)
                except ImpossibleProductModeException as ipme:
                    six.raise_from(
                        Problem(
                            _("Unable to link %(product)s: %(error)s") % {
                                "product": child_product,
                                "error": ipme
                            }), ipme)

        message_parts = []
        if products_to_add:
            message_parts.append(_("New: %d") % len(products_to_add))
        if products_to_remove:
            message_parts.append(_("Removed: %d") % len(products_to_remove))
        if message_parts and self.request:
            messages.success(self.request, ", ".join(message_parts))
Example #2
0
    def save(self):
        parent_product = self.parent_product
        current_products = set(
            parent_product.get_package_child_to_quantity_map())
        selected_products, removed_products, selected_quantities = self.get_selected_and_removed(
        )

        with atomic():
            try:
                clear_existing_package(parent_product)
                parent_product.make_package(package_def=selected_quantities)
            except ImpossibleProductModeException as ipme:
                six.raise_from(
                    Problem(
                        _("Unable to make package %(product)s: %(error)s") % {
                            "product": parent_product,
                            "error": ipme
                        }), ipme)

        products_to_add = selected_products - current_products
        products_to_remove = current_products & removed_products

        message_parts = []
        if products_to_add:
            message_parts.append(_("New: %d") % len(products_to_add))
        if products_to_remove:
            message_parts.append(_("Removed: %d") % len(products_to_remove))
        if message_parts and self.request:
            messages.success(self.request, ", ".join(message_parts))
 def _save_combo(self, combo):
     """
     :param combo: Combo description dict, from `get_all_available_combinations`
     :type combo: dict
     """
     new_product = self.cleaned_data.get("r_%s" % combo["hash"])
     new_product_pk = getattr(new_product, "pk", None)
     old_product_pk = combo["result_product_pk"]
     if old_product_pk == new_product_pk:  # Nothing to do
         return
     if old_product_pk:  # Unlink old one
         try:
             old_product = Product.objects.get(
                 variation_parent=self.parent_product, pk=old_product_pk)
             old_product.unlink_from_parent()
         except ObjectDoesNotExist:
             pass
     if new_product:
         try:
             new_product.link_to_parent(parent=self.parent_product,
                                        combination_hash=combo["hash"])
         except ImpossibleProductModeException as ipme:
             six.raise_from(
                 Problem(
                     _("Unable to link %(product)s: %(error)s") % {
                         "product": new_product,
                         "error": ipme
                     }), ipme)