def categories_changed_handler(sender, **kwargs):
"""
?????????? ??????? ????????? ???-?? ??????? ?????????,
??????????? ? ????????? ? ?? ?????????????.
???????????? ??? ?????????? ????????? ??????? ? ?????? ?????????.
"""
categories = kwargs.get('categories')
include_self = kwargs.get('include_self', True)
if isinstance(categories, ShopCategory):
# ????????? ?????????
categories = ShopCategory.objects.filter(pk=categories.pk)
elif isinstance(categories, (int, str)):
# ?????? ??? ?????, ?????????? ID ?????????
categories = ShopCategory.objects.filter(pk=categories)
elif isinstance(categories, (list, tuple, set, ValuesListQuerySet)):
# ?????? ????? ??? ?????, ?????????? ID ?????????
categories = ShopCategory.objects.filter(pk__in=categories)
elif isinstance(categories, QuerySet) and categories.model is ShopCategory:
# QuerySet ?????????
pass
else:
raise TypeError('Invalid categories for signal "categories_changed"')
ancestors = categories.get_ancestors(
include_self=include_self
).filter(
is_visible=True
).order_by('tree_id', '-level').values_list('id', flat=True)
with transaction.atomic():
for category_id in ancestors:
ShopCategory.objects.filter(pk=category_id).update(
total_product_count=RawSQL(
'SELECT shop_shopcategory.product_count + '
'COALESCE(SUM(ssc.total_product_count), 0) '
'FROM shop_shopcategory AS ssc '
'WHERE ssc.parent_id = shop_shopcategory.id '
'AND ssc.is_visible = TRUE',
()
)
)
评论列表
文章目录