Coverage for src/orders/models.py: 94%

16 statements  

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

1from django.db import models 

2from django.contrib.auth import get_user_model 

3import uuid 

4from src.orders.choices import OrderStatusChoices 

5 

6UserModel = get_user_model() 

7 

8 

9class Order(models.Model): 

10 """ 

11 The Order model represents a single product order made by a user. 

12 

13 Key Features: 

14 - Each order is linked to a specific inventory item (size/variation) via a ForeignKey. 

15 - Supports grouping multiple products into a single order event using order_group (UUID). 

16 - Tracks order status (pending, completed, etc.). 

17 - Stores the quantity and creation timestamp for each order item. 

18 - Linked to the user who placed the order. 

19 

20 Relationships: 

21 - inventory: ForeignKey to Inventory, which in turn is linked to the actual product (Earwear, Neckwear, etc.). 

22 - user: ForeignKey to the user who placed the order. 

23 - order_group: UUID to group multiple order items from a single checkout. 

24 """ 

25 

26 class Meta: 

27 ordering = ['-created_at'] 

28 

29 order_group = models.UUIDField( 

30 default=uuid.uuid4, 

31 editable=False, 

32 help_text="Groups multiple order items from a single checkout event.", 

33 ) 

34 

35 status = models.CharField( 

36 max_length=OrderStatusChoices.max_length(), 

37 choices=OrderStatusChoices.choices, 

38 default=OrderStatusChoices.PENDING, 

39 help_text="Current status of the order (e.g., pending, completed).", 

40 ) 

41 

42 quantity = models.PositiveIntegerField( 

43 help_text="Number of units of the inventory item ordered." 

44 ) 

45 

46 created_at = models.DateTimeField( 

47 auto_now_add=True, help_text="Timestamp when the order was created." 

48 ) 

49 

50 inventory = models.ForeignKey( 

51 to='products.Inventory', 

52 on_delete=models.CASCADE, 

53 related_name='orders', 

54 help_text="The inventory item (size/variation) being ordered.", 

55 ) 

56 

57 user = models.ForeignKey( 

58 to=UserModel, 

59 on_delete=models.CASCADE, 

60 help_text="The user who placed the order.", 

61 ) 

62 

63 def __str__(self): 

64 return f"Order {self.id} by {self.user.username}"