-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmodels.py
More file actions
78 lines (61 loc) · 2.44 KB
/
models.py
File metadata and controls
78 lines (61 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
from decimal import Decimal
from django.contrib.auth.models import User
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=50, unique=True)
description = models.CharField(max_length=200, blank=True, null=True)
price = models.DecimalField(max_digits=18, decimal_places=2, default=0.00)
retired = models.BooleanField(default=False, null=False)
category = models.ForeignKey(
"Category",
models.SET_NULL,
blank=True,
null=True,
)
class Tag(models.Model):
name = models.CharField(max_length=50, unique=True)
description = models.CharField(max_length=200, blank=True, null=True)
products = models.ManyToManyField(Product)
retired = models.BooleanField(default=False, null=False)
class Category(models.Model):
name = models.CharField(max_length=50, unique=True)
description = models.CharField(max_length=200, blank=True, null=True)
created_on = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
retired = models.BooleanField(default=False, null=False)
class Order(models.Model):
class Status(models.TextChoices):
CREATED = "Created", "Created"
PAID = "Paid", "Paid"
CANCELLED = "Cancelled", "Cancelled"
order_status = models.CharField(
max_length=10, choices=Status.choices, default=Status.CREATED
)
order_total = models.DecimalField(
max_digits=18, decimal_places=2, default=0.00
)
created_on = models.DateTimeField(auto_now_add=True)
last_updated = models.DateTimeField(auto_now=True)
owner = models.ForeignKey(
"auth.User", related_name="orders", on_delete=models.CASCADE
)
def calculate_total(self):
total = Decimal(0.00)
line_items = LineItem.objects.filter(order=self.pk).values()
if line_items:
for ol in line_items:
total += ol["sub_total"]
self.order_total = total
self.save()
class LineItem(models.Model):
order = models.ForeignKey(Order, on_delete=models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE)
quantity = models.IntegerField()
sub_total = models.DecimalField(
max_digits=18, decimal_places=2, default=0.00
)
def calculate_sub_total(self):
self.sub_total = self.quantity * self.product.price
self.save()
class Meta:
unique_together = [["order", "product"]]