Exemplo n.º 1
0
    def post_save_handler(cls, sender, instance, created, raw=True, **kwargs):
        """
        Post-save signal handler for creating/updating product related to archive item.

        Noteworthy:
            * Archive item data overwrites product data (no sync)
            * get_subtype() on archive is used with item id to generate slug for product.
            * Category for archive is automatically created if it doesn't exists (verbose_name_plural is used as title)
            * Category title can be changed via admin - and will not be overwritten. Category slug however cannot be changed.
            * Product is only active if archive item is published and sale=yes.
            * Only the price for 1 item is written - hence, quantity discount can be given and will not be overwritten.
        """
        if raw:
            return

        # Prevent that signal is invoked when we're just saving the product id on the archive item
        if not getattr(instance, '_updating_product_state', False):
            if instance.sale:
                if instance.product:
                    p = instance.product
                else:
                    p = Product()

                # Update product (TODO: weight)

                # Let archive item update product and save.
                p = instance.update_product(p)

                # Save product
                p.save()

                # Set product on archive item, if not already set
                if not instance.product:
                    instance.product = p
                    instance._updating_product_state = True
                    instance.save()
                    del instance._updating_product_state

                # Set price on product
                instance.update_price(p)

                # Save product attributes (e.g. Job/JSP no)
                instance.update_attributes(p)

                # Add categories for product
                instance.update_categories(p)

            elif not instance.sale and instance.product:
                p = instance.product
                p.active = False
                p.save()