Coverage for src/products/models/inventory.py: 95%

19 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-08-04 12:59 +0300

1from django.contrib.contenttypes.fields import GenericForeignKey 

2from django.contrib.contenttypes.models import ContentType 

3from django.core.validators import MinValueValidator 

4from django.db import models 

5 

6from src.products.constants import InventoryDefaults, InventoryFiledLengths 

7from src.products.mixins import NameFieldMixin 

8 

9 

10class Inventory(models.Model): 

11 """ 

12 This model manages the inventory levels and pricing for all products 

13 in the system. It uses Django's GenericForeignKey to work with different 

14 product types (Earwear, Neckwear, Fingerwear, Wristwear) without needing 

15 separate inventory tables for each category. 

16 

17 The model supports size variations, allowing the same product to have 

18 different stock levels and prices for different sizes (e.g., Small, Medium, Large). 

19 

20 Key features: 

21 - Quantity tracking for stock management 

22 - Price field with decimal precision for accurate pricing 

23 - Size relationship for product variations 

24 - Generic relationship to any product type 

25 - Validation to prevent negative quantities and prices 

26 """ 

27 

28 class Meta: 

29 ordering = ['id'] 

30 

31 quantity = models.PositiveIntegerField( 

32 default=InventoryDefaults.QUANTITY, 

33 ) 

34 

35 price = models.DecimalField( 

36 max_digits=InventoryFiledLengths.PRICE_MAX_DIGITS, 

37 decimal_places=InventoryFiledLengths.PRICE_DECIMAL_PLACES, 

38 validators=[MinValueValidator(0)], # Ensures price is not negative 

39 ) 

40 

41 size = models.ForeignKey( 

42 to='products.Size', 

43 on_delete=models.CASCADE, 

44 ) 

45 

46 # GenericForeignKey components for flexible product relationships 

47 # content_type stores which model the inventory is for (Earwear, Neckwear, etc.) 

48 content_type = models.ForeignKey( 

49 ContentType, 

50 on_delete=models.CASCADE, 

51 ) 

52 

53 # object_id stores the specific product's ID 

54 object_id = models.PositiveIntegerField() 

55 

56 # GenericForeignKey combines content_type and object_id 

57 # This allows the inventory to be associated with any product type 

58 # The 'product' name makes it easy to access the related product 

59 product = GenericForeignKey( 

60 'content_type', 

61 'object_id', 

62 ) 

63 

64 def __str__(self): 

65 return f'Quantity: {self.quantity} / Price: {self.price} / Size: {self.size.name}' 

66 

67 

68class Size(NameFieldMixin): 

69 """ 

70 Size model for product size variations. 

71 

72 This model inherits from NameFieldMixin to get a standardized name field. 

73 It's used to define size options for products (e.g., Small, Medium, Large). 

74 

75 The model is simple but essential for inventory management, allowing 

76 the same product to have different stock levels and prices for different sizes. 

77 """ 

78 

79 pass