Coverage for src/wishlists/services.py: 94%
36 statements
« prev ^ index » next coverage.py v7.9.2, created at 2025-08-04 12:59 +0300
« prev ^ index » next coverage.py v7.9.2, created at 2025-08-04 12:59 +0300
1from rest_framework.exceptions import ValidationError, NotFound
3from src.wishlists.models import Wishlist
4from src.common.services import UserIdentificationService
5from src.wishlists.constants import WishlistErrorMessages
8class WishlistService:
9 @staticmethod
10 def get_user_identifier(request):
11 return UserIdentificationService.get_user_identifier(request)
13 @staticmethod
14 def get_product_object(content_type, object_id):
15 """
16 Retrieve a product object using ContentType framework.
18 This method validates that the requested product exists in the database
19 and returns the actual product object for further operations.
20 """
21 try:
22 return content_type.get_object_for_this_type(pk=object_id)
24 except content_type.model_class().DoesNotExist:
25 raise NotFound(WishlistErrorMessages.PRODUCT_NOT_FOUND)
27 @staticmethod
28 def check_item_exists(user_filters, content_type, object_id):
29 """
30 Check if a wishlist item already exists for the user.
32 This method prevents duplicate items in the wishlist by checking
33 if the user already has the specific
34 product in their wishlist.
35 """
36 return Wishlist.objects.filter(
37 content_type=content_type,
38 object_id=object_id,
39 **user_filters,
40 ).exists()
42 @staticmethod
43 def create_wishlist_item(user_filters, content_type, object_id):
44 """
45 This method creates a new wishlist item after validating that:
46 1. The product exists in the database
47 2. The user doesn't already have this item in their wishlist
48 """
49 # Check if item already exists to prevent duplicates
50 if WishlistService.check_item_exists(
51 user_filters,
52 content_type,
53 object_id,
54 ):
55 raise ValidationError(
56 {
57 'detail': WishlistErrorMessages.ITEM_ALREADY_EXISTS,
58 }
59 )
61 # Validate that the product exists
62 WishlistService.get_product_object(content_type, object_id)
64 # Create the wishlist item
65 created_item = Wishlist.objects.create(
66 content_type=content_type,
67 object_id=object_id,
68 **user_filters,
69 )
71 return created_item
73 @staticmethod
74 def get_wishlist_item(user_filters, content_type, object_id):
75 """
76 This method finds and returns a specific wishlist item for the user
77 and the specified product.
78 """
79 try:
80 item = Wishlist.objects.get(
81 content_type=content_type,
82 object_id=object_id,
83 **user_filters,
84 )
85 return item
87 except Wishlist.DoesNotExist:
88 raise NotFound(WishlistErrorMessages.ITEM_NOT_FOUND)
90 @staticmethod
91 def delete_wishlist_item(user_filters, content_type, object_id):
92 """
93 This method removes a specific wishlist item from the user's wishlist.
94 It first validates that the item exists before deletion.
95 """
96 # Get the wishlist item (this will raise NotFound if it doesn't exist)
97 wishlist_item = WishlistService.get_wishlist_item(
98 user_filters,
99 content_type,
100 object_id,
101 )
103 # Delete the item
104 wishlist_item.delete()
106 return True