Features: 1) Added custom manager for Product model

This commit is contained in:
Egor Pavlovich Gorbunov 2025-08-25 00:26:06 +03:00
parent d948d51f91
commit fe104c7ff5
3 changed files with 20 additions and 7 deletions

View file

@ -62,3 +62,19 @@ class AddressManager(models.Manager):
user=kwargs.pop("user"),
defaults={"api_response": data, "location": location},
)[0]
class ProductManager(models.Manager):
def available(self):
return self.filter(
is_active=True, brand__is_active=True, category__is_active=True, stocks__vendor__is_active=True
)
def available_in_stock(self):
return self.filter(
is_active=True,
brand__is_active=True,
category__is_active=True,
stocks__vendor__is_active=True,
stocks__quantity__gt=0,
)

View file

@ -49,7 +49,7 @@ from mptt.models import MPTTModel
from core.abstract import NiceModel
from core.choices import ORDER_PRODUCT_STATUS_CHOICES, ORDER_STATUS_CHOICES
from core.errors import DisabledCommerceError, NotEnoughMoneyError
from core.managers import AddressManager
from core.managers import AddressManager, ProductManager
from core.utils import (
generate_human_readable_id,
get_product_uuid_as_path,
@ -681,6 +681,8 @@ class Product(ExportModelOperationsMixin("product"), NiceModel): # type: ignore
verbose_name=_("Slug"),
)
objects: ProductManager = ProductManager()
class Meta:
verbose_name = _("product")
verbose_name_plural = _("products")

View file

@ -540,12 +540,7 @@ class ProductViewSet(EvibesViewSet):
qs = super().get_queryset()
if self.request.user.has_perm("core.view_product"):
return qs
return qs.filter(
is_active=True,
brand__is_active=True,
category__is_active=True,
stocks__vendor__is_active=True,
)
return qs.available()
def get_object(self):
queryset = self.filter_queryset(self.get_queryset())