Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
65 lines (54 loc) · 1.87 KB
/
Copy pathmodels.py
File metadata and controls
65 lines (54 loc) · 1.87 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
from django.db import models
from allocation.domain import model as domain_model
class Batch(models.Model):
reference = models.CharField(max_length=255)
sku = models.CharField(max_length=255)
qty = models.IntegerField()
eta = models.DateField(blank=True, null=True)
@staticmethod
def update_from_domain(batch: domain_model.Batch):
try:
b = Batch.objects.get(reference=batch.reference)
except Batch.DoesNotExist:
b = Batch(reference=batch.reference)
b.sku = batch.sku
b.qty = batch._purchased_quantity
b.eta = batch.eta
b.save()
b.allocation_set.set(
Allocation.from_domain(l, b)
for l in batch._allocations
)
def to_domain(self) -> domain_model.Batch:
b = domain_model.Batch(
ref=self.reference, sku=self.sku, qty=self.qty, eta=self.eta
)
b._allocations = set(
a.line.to_domain()
for a in self.allocation_set.all()
)
return b
class OrderLine(models.Model):
orderid = models.CharField(max_length=255)
sku = models.CharField(max_length=255)
qty = models.IntegerField()
def to_domain(self):
return domain_model.OrderLine(
orderid=self.orderid, sku=self.sku, qty=self.qty
)
@staticmethod
def from_domain(line):
l, _ = OrderLine.objects.get_or_create(
orderid=line.orderid, sku=line.sku, qty=line.qty
)
return l
class Allocation(models.Model):
batch = models.ForeignKey(Batch, on_delete=models.CASCADE)
line = models.ForeignKey(OrderLine, on_delete=models.CASCADE)
@staticmethod
def from_domain(domain_line, django_batch):
a, _ = Allocation.objects.get_or_create(
line=OrderLine.from_domain(domain_line),
batch=django_batch,
)
return a