-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
268 lines (230 loc) · 10.1 KB
/
models.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
"""
Copyright 2021 Stones River Meadery (aaron@stonesrivermead.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from django.db import models
from datetime import datetime
from django.dispatch import receiver
from django.db.models.signals import post_save, m2m_changed
from django.utils.text import slugify
#TODO Refactor "fermenter" to generic "Vessel" and add "Vessel use" to be 'fermenter','aging','serving',etc.
#TODO add "Packaging" to identify how the batch was finished
# Create your models here.
class Unit(models.Model):
def __str__(self):
return self.name
TEMPERATURE = 0
CONCENTRATION = 1
WEIGHT = 2
PH = 3
TIME = 4
VOLUME = 5
CATEGORIES = (
(TEMPERATURE, ("Temperature")),
(CONCENTRATION, ("Concentration/Density")),
(WEIGHT, ("Weight/Mass")),
(PH, ("pH")),
(TIME, ("Timing")),
(VOLUME, ("Volume"))
)
identifier = models.CharField(max_length=10, help_text="Enter the unit identifier, i.e. 'mgL' or 'ph'")
label = models.CharField(max_length=25, null=True, help_text="Enter abbreviation label of the measured unit, i.e. 'mg/L'")
name = models.CharField(max_length=25, null=True, help_text="Descriptive Name of the measuring unit.")
category = models.SmallIntegerField(choices = CATEGORIES, null=False)
# TODO: Add Unit Categories in Objects
# TODO: Add User Roles/Permissions
class Vessel(models.Model):
STATUS_ACTIVE = 'In Use'
STATUS_READY = "Clean/Ready"
STATUS_DIRTY = "Needs Cleaning"
def __str__(self):
return self.name + " (" + str(self.max_size) + self.max_size_units.identifier + ")"
name = models.CharField(max_length=25)
max_size = models.IntegerField()
max_size_units = models.ForeignKey(Unit, related_name="fermenter_max_size_units", on_delete=models.SET("_del"))
used_size = models.IntegerField(blank=True, null=True)
used_size_units = models.ForeignKey(Unit, blank=True, null=True,related_name="fermenter_used_size_units", on_delete=models.SET("_del"))
status = models.CharField(max_length=15, default=STATUS_READY)
class RecipeFermentable(models.Model):
name = models.CharField(max_length=75)
version = models.IntegerField()
type = models.CharField(max_length=30) #TODO Refactor into it's own class/relation
class Recipe(models.Model):
name = models.CharField(max_length=75)
dateCreated = models.DateField()
dateUpdated = models.DateField()
version = models.IntegerField()
type = models.CharField(max_length=30)
brewer = models.CharField(max_length=30)
batchSize = models.FloatField() #in Liters
source = models.CharField(max_length=50) #Where did the recipe come from
pairing = models.CharField(max_length=250) # Textfield listing various foods. TODO: Refactor
notes = models.TextField()
estOG = models.FloatField()
estFG = models.FloatField()
estABV = models.FloatField()
class Fermenter(Vessel):
pass
class AgingTank(Vessel):
pass
class Barrel(Vessel):
serial = models.CharField(max_length=20) #Barcode / rfid / etc
toastLevel = models.CharField(max_length=25)
class BatchNoteType(models.Model):
def __str__(self):
return self.name
name = models.CharField(max_length=50)
class BatchTestType(models.Model):
def __str__(self):
return self.name
def save(self,*args,**kwargs):
if not self.shortid:
self.shortid = slugify(self.name)
super(BatchTestType,self).save(*args,**kwargs)
name = models.CharField(max_length = 25)
shortid = models.SlugField(unique=True)
class BatchStyle(models.Model):
def __str__(self):
return self.name
name = models.CharField(max_length=30)
class BatchCategory(models.Model):
def __str__(self):
if self.bjcp_code:
return self.name + " (" + self.bjcp_code + ")"
else:
return self.name
class Meta:
verbose_name_plural = "batch categories"
name = models.CharField(max_length=30)
style = models.ForeignKey(BatchStyle, on_delete=models.CASCADE)
bjcp_code = models.CharField(max_length=3)
class ActivityLog(models.Model):
datetime = models.DateTimeField()
text = models.TextField()
class Batch(models.Model):
def __str__(self):
return self.name
class Meta:
#ordering = ['-startdate']
verbose_name_plural = 'batches'
name = models.CharField(max_length=50)
startdate = models.DateTimeField(auto_now=True)
enddate = models.DateTimeField(null=True, blank=True)
size = models.IntegerField()
size_units = models.ForeignKey(Unit, on_delete=models.SET("_del"))
active = models.BooleanField(default=True)
# Using the 'related_name' on batches, applies that name on the other side for "Fermenter.batch"
fermenter = models.ManyToManyField(Fermenter, blank=True, related_name='batch')
startingGravity = models.FloatField()
estimatedEndGravity = models.FloatField()
category = models.ForeignKey(BatchCategory, on_delete=models.SET("_del"), blank=True, null=True)
activity = models.ManyToManyField(ActivityLog, related_name='batch')
recipe = models.ForeignKey(Recipe, on_delete=models.SET("_del"), null=True, blank=True)
# TODO Add additional objects
aging_vessel = None
packaging = None
def complete(self):
self.enddate = datetime.now()
self.active = False
# If a batch is saved on a fermenter that isn't currently active,
# set it active
@receiver(m2m_changed,sender=Batch.fermenter.through)
def setActiveFermenter(sender,instance,**kwargs):
fermenters = instance.fermenter.all()
for fermenter in fermenters:
if fermenter.status != Fermenter.STATUS_ACTIVE:
fermenter.status = fermenter.STATUS_ACTIVE
fermenter.save()
class BatchTest(models.Model):
def __str__(self):
fmt = "%m/%d/%y-%H:%M"
return self.datetime.strftime(fmt) + " " + self.type.name
datetime = models.DateTimeField(auto_now=False)
type = models.ForeignKey(BatchTestType, on_delete=models.SET("_del"))
value = models.FloatField()
description = models.CharField(max_length=250, blank=True)
units = models.ForeignKey(Unit, on_delete=models.SET("_del"))
batch = models.ForeignKey(Batch, blank=True, on_delete=models.CASCADE, related_name="tests")
# If a batch is saved with a Starting Gravity, add that test
@receiver(post_save,sender=Batch)
def addGravityTest(sender,instance,**kwarts):
print("Adding Gravity")
if instance.startingGravity:
gravTest = BatchTest()
testType = BatchTestType.objects.filter(shortid='specific-gravity')[0]
gravTest.type = testType
gravTest.value = instance.startingGravity
gravTest.description = "Auto created from new batch."
gravTest.datetime = datetime.now()
unit = Unit.objects.filter(name__contains="specific")[0]
gravTest.units = unit
gravTest.batch = instance
print("Saving gravity")
gravTest.save()
class BatchAdditionItem(models.Model):
# Sulfites, Acids, Hops, Nutrients, Fruit, etc
# Creating a model, rather than typing, so reports can be made on which batches used a specific item
def __str__(self):
if self.lotid:
return self.name + " (" + self.lotid + ")"
else:
return self.name
name = models.CharField(max_length=50, help_text="Name of this addition item.")
maker = models.CharField(max_length=50, blank=True, help_text="Name of the company who made this item.")
lotid = models.CharField(max_length=20, blank=True, help_text="The lot or batch id of this item. Useful when looking for batches made with bad Lot")
class BatchAddition(models.Model):
name = models.ForeignKey(BatchAdditionItem, on_delete=models.SET("_del"))
description = models.CharField(max_length=250, blank=True, help_text="Add a brief description of this Addition item and why")
units = models.ForeignKey(Unit, on_delete=models.SET("_del"))
amount = models.FloatField()
batch = models.ForeignKey(Batch, blank=True, on_delete=models.CASCADE, related_name="additions")
class BatchNote(models.Model):
def __str__(self):
fmt = "%m/%d/%y-%H:%M"
return self.date.strftime(fmt) + " " + self.text[:50]
text = models.TextField()
date = models.DateTimeField(auto_now_add=False)
notetype = models.ForeignKey(BatchNoteType,on_delete=models.SET("_del"))
batch = models.ForeignKey(Batch, on_delete=models.CASCADE,related_name="notes")
@receiver(post_save,sender=BatchNote)
@receiver(post_save,sender=BatchAddition)
@receiver(post_save,sender=BatchTest)
@receiver(post_save,sender=Batch)
def addActivity(sender,instance,created=False,**kwargs):
text = None
batch = None
date = datetime.now()
if sender.__name__ == "Batch":
batch = instance
if created:
text = "Batch Created"
else:
text = "Batch Modified"
if sender.__name__ == "BatchNote":
batch = instance.batch
if created:
text = "Added ["+instance.notetype.name+"] :: " + instance.text
if sender.__name__ == "BatchAddition":
batch = instance.batch
if created:
text = "Added [" + instance.name.name + "] :: " + str(instance.amount) + " " + instance.units.name
else:
text = "Updated [" + instance.name.name + "]"
if sender.__name__ == "BatchTest":
batch = instance.batch
if created:
text = "Added [" + instance.type.name + "] :: " + str(instance.value) + " " + instance.units.name
else:
text = "Updated [" + instance.type.name + "]"
if text:
log = ActivityLog(datetime=date,text=text)
log.save()
batch.activity.add(log)