36 lines
1.2 KiB
Python
36 lines
1.2 KiB
Python
from rest_framework import exceptions
|
|
from decimal import Decimal
|
|
from django.db import transaction
|
|
from apps.shop.models import Cart as CartModel
|
|
|
|
|
|
def add_to_cart(product_id: int, quantity: int, workspace, cuser_role) -> CartModel:
|
|
try:
|
|
|
|
try:
|
|
cart_obj = CartModel.objects.get(product_id=product_id, sys_ws=workspace, cuser_role=cuser_role)
|
|
except Exception as exp:
|
|
cart_obj = CartModel()
|
|
|
|
cart_obj.product_id = product_id
|
|
cart_obj.quantity = quantity
|
|
cart_obj.sys_ws = workspace
|
|
cart_obj.cuser_role = cuser_role
|
|
|
|
cart_obj.full_clean()
|
|
cart_obj.save()
|
|
|
|
return cart_obj
|
|
|
|
except Exception as exp:
|
|
message = f'Error Add to Cart: {exp}'
|
|
exceptions.ValidationError({'error': message})
|
|
|
|
|
|
def delete_cart_iteam(cart_id: int, cuser_role_uuid, workspace_uuid: str) -> CartModel:
|
|
try:
|
|
cart_obj = CartModel.objects.get(id=cart_id, cuser_role__uuid=cuser_role_uuid, sys_ws__uuid=workspace_uuid).delete()
|
|
return cart_obj
|
|
except Exception as exp:
|
|
message = f'Error Deleting Cart iteam: {exp}'
|
|
raise exceptions.ValidationError({'error': message}) |