fix(models, viewsets): filter inactive records in queries

ensure only active records are considered in `models.py` and `viewsets.py` by adding `is_active=True` filters. improves data integrity and prevents processing inactive entities.
This commit is contained in:
Egor Pavlovich Gorbunov 2026-02-16 19:43:09 +03:00
parent 1253496e30
commit 9d5b4fee90
2 changed files with 19 additions and 11 deletions

View file

@ -402,7 +402,8 @@ class Category(ExportModelOperationsMixin("category"), NiceModel, MPTTModel):
@cached_property @cached_property
def filterable_attributes(self) -> list[FilterableAttribute]: def filterable_attributes(self) -> list[FilterableAttribute]:
rows = ( rows = (
AttributeValue.objects.annotate(value_length=Length("value")) AttributeValue.objects.filter(is_active=True)
.annotate(value_length=Length("value"))
.filter( .filter(
product__is_active=True, product__is_active=True,
product__category=self, product__category=self,
@ -726,12 +727,17 @@ class Product(ExportModelOperationsMixin("product"), NiceModel):
@property @property
def price(self: Self) -> float: def price(self: Self) -> float:
stock = self.stocks.only("price").order_by("-price").first() stock = (
self.stocks.filter(is_active=True).only("price").order_by("-price").first()
)
return round(stock.price, 2) if stock else 0.0 return round(stock.price, 2) if stock else 0.0
@cached_property @cached_property
def quantity(self) -> int: def quantity(self) -> int:
return self.stocks.aggregate(total=Sum("quantity"))["total"] or 0 return (
self.stocks.filter(is_active=True).aggregate(total=Sum("quantity"))["total"]
or 0
)
@property @property
def total_orders(self): def total_orders(self):

View file

@ -383,16 +383,18 @@ class BrandViewSet(SchonViewSet):
return obj return obj
def get_queryset(self): def get_queryset(self):
queryset = Brand.objects.all() qs = super().get_queryset()
if self.request.user.has_perm("core.view_brand"): # ty:ignore[possibly-missing-attribute]
if self.request.user.has_perm("view_category"): # ty:ignore[possibly-missing-attribute] if self.request.user.has_perm("core.view_brand"): # ty:ignore[possibly-missing-attribute]
queryset = queryset.prefetch_related("categories") return qs.prefetch_related("categories")
else: return qs.prefetch_related(
queryset = queryset.prefetch_related(
Prefetch("categories", queryset=Category.objects.filter(is_active=True)) Prefetch("categories", queryset=Category.objects.filter(is_active=True))
) )
if self.request.user.has_perm("core.view_category"): # ty:ignore[possibly-missing-attribute]
return queryset return qs.filter(is_active=True).prefetch_related("categories")
return qs.filter(is_active=True).prefetch_related(
Prefetch("categories", queryset=Category.objects.filter(is_active=True))
)
# noinspection PyUnusedLocal # noinspection PyUnusedLocal
@action( @action(