Features: 1) Added custom manager for Product model
This commit is contained in:
parent
d948d51f91
commit
fe104c7ff5
3 changed files with 20 additions and 7 deletions
|
|
@ -62,3 +62,19 @@ class AddressManager(models.Manager):
|
||||||
user=kwargs.pop("user"),
|
user=kwargs.pop("user"),
|
||||||
defaults={"api_response": data, "location": location},
|
defaults={"api_response": data, "location": location},
|
||||||
)[0]
|
)[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,
|
||||||
|
)
|
||||||
|
|
|
||||||
|
|
@ -49,7 +49,7 @@ from mptt.models import MPTTModel
|
||||||
from core.abstract import NiceModel
|
from core.abstract import NiceModel
|
||||||
from core.choices import ORDER_PRODUCT_STATUS_CHOICES, ORDER_STATUS_CHOICES
|
from core.choices import ORDER_PRODUCT_STATUS_CHOICES, ORDER_STATUS_CHOICES
|
||||||
from core.errors import DisabledCommerceError, NotEnoughMoneyError
|
from core.errors import DisabledCommerceError, NotEnoughMoneyError
|
||||||
from core.managers import AddressManager
|
from core.managers import AddressManager, ProductManager
|
||||||
from core.utils import (
|
from core.utils import (
|
||||||
generate_human_readable_id,
|
generate_human_readable_id,
|
||||||
get_product_uuid_as_path,
|
get_product_uuid_as_path,
|
||||||
|
|
@ -681,6 +681,8 @@ class Product(ExportModelOperationsMixin("product"), NiceModel): # type: ignore
|
||||||
verbose_name=_("Slug"),
|
verbose_name=_("Slug"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
objects: ProductManager = ProductManager()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
verbose_name = _("product")
|
verbose_name = _("product")
|
||||||
verbose_name_plural = _("products")
|
verbose_name_plural = _("products")
|
||||||
|
|
|
||||||
|
|
@ -540,12 +540,7 @@ class ProductViewSet(EvibesViewSet):
|
||||||
qs = super().get_queryset()
|
qs = super().get_queryset()
|
||||||
if self.request.user.has_perm("core.view_product"):
|
if self.request.user.has_perm("core.view_product"):
|
||||||
return qs
|
return qs
|
||||||
return qs.filter(
|
return qs.available()
|
||||||
is_active=True,
|
|
||||||
brand__is_active=True,
|
|
||||||
category__is_active=True,
|
|
||||||
stocks__vendor__is_active=True,
|
|
||||||
)
|
|
||||||
|
|
||||||
def get_object(self):
|
def get_object(self):
|
||||||
queryset = self.filter_queryset(self.get_queryset())
|
queryset = self.filter_queryset(self.get_queryset())
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue