Files
crmApi/apps/shop/selectors/product.py
T
2026-10-09 11:05:10 +03:30

51 lines
2.2 KiB
Python

from apps.shop.models import Product as ProductModel
from rest_framework import exceptions
from apps.shop.ordering import product_ordering
def retrieve_product(product_id: int, workspace_uuid: str) -> ProductModel:
try:
product_obj = ProductModel.objects.get(id=product_id, sys_ws__uuid=workspace_uuid)
return product_obj
except Exception as exp:
message = f'Error Retrieve Product: {exp}'
raise exceptions.ValidationError({'error': message})
def list_product(workspace_uuid: str) -> ProductModel:
try:
product_category_obj = ProductModel.objects.filter(sys_ws__uuid=workspace_uuid)
return product_category_obj
except Exception as exp:
message = f'Error Fetching List Product: {exp}'
raise exceptions.ValidationError({'error': message})
def list_product_by_category(workspace_uuid: str, category_id: int) -> ProductModel:
try:
product_category_obj = ProductModel.objects.filter(sys_ws__uuid=workspace_uuid, category_id=category_id,
view_only=False).order_by(*product_ordering())
return product_category_obj
except Exception as exp:
message = f'Error Fetching List Product by category: {exp}'
raise exceptions.ValidationError({'error': message})
def list_view_only_product_by_category(workspace_uuid: str, category_id: int) -> ProductModel:
try:
view_only_product_category_obj = ProductModel.objects.filter(sys_ws__uuid=workspace_uuid, category_id=category_id,
view_only=True).order_by(*product_ordering())
return view_only_product_category_obj
except Exception as exp:
message = f'Error Fetching View Only List Product by category: {exp}'
raise exceptions.ValidationError({'error': message})
def count_product_in_category(workspace_uuid: str, category_id: int) -> int:
try:
count_product = ProductModel.objects.filter(sys_ws__uuid=workspace_uuid, category_id=category_id).count()
return count_product
except Exception as exp:
message = f'Error Couting Product in category: {exp}'
raise exceptions.ValidationError({'error': message})