/
models.py
1118 lines (925 loc) · 43 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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- encoding: utf-8 -*-
# Copyright (c) 2008-2009, Joshua Sled <jsled@asynchronous.org>
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
#
# * The names of its contributors may not be used to endorse or promote
# products derived from this software without specific prior written
# permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import datetime
import itertools
import urllib
from decimal import Decimal, Context, ROUND_HALF_UP, InvalidOperation
from django.db import models
from django.contrib import auth
from timezones.fields import TimeZoneField
class StepFilter (object):
def __init__(self, conditions=None):
self._conditions = conditions or []
def eval(self, **kwargs):
for condition in self._conditions:
if not condition(**kwargs):
return False
return True
class StepType (object):
def __init__(self, id, label, input_filter, interesting_fields=None, next_steps=None):
self._id = id
self._label = label
self._input_filter = input_filter or StepFilter()
if not interesting_fields: interesting_fields = []
if not next_steps: next_steps = []
self._interesting_fields = interesting_fields
self._next_steps = next_steps
id = property(lambda s: s._id)
label = property(lambda s: s._label)
interesting_fields = property(lambda s: s._interesting_fields)
next_steps = property(lambda s: s._next_steps)
input_filter = property(lambda s: s._input_filter)
def is_terminal(self):
return len(self.next_steps) == 0
RecipeTypes = (
('e', 'Extract'),
('p', 'Partial Mash'),
('a', 'All Grain'),
)
DispenseTypes = (
('k', 'kegging'),
('b', 'bottling')
)
class UserProfile (models.Model):
user = models.ForeignKey(auth.models.User, unique=True)
pref_brew_type = models.CharField(max_length=1, choices=RecipeTypes, default='a')
pref_make_starter = models.BooleanField(default=False)
pref_secondary_ferm = models.BooleanField(default=False)
pref_dispensing_style = models.CharField(max_length=1, choices=DispenseTypes, default='b')
timezone = TimeZoneField(default='UTC')
def __getitem__(self, key):
return self.__dict__.get(key, None)
def filter_user_pref(preference_name, value=True):
def _foo(**kwargs):
user = kwargs['user']
profile = user.get_profile()
return profile[preference_name] == value
return _foo
def filter_recipe_type(types):
def _foo(**kwargs):
recipe = kwargs['recipe']
return recipe.type in types
return _foo
def filter_recipe_is_lager():
return lambda **kw: True
def filter_recipe_estimated_og_above(val):
return lambda **kw: True
no_filter = StepFilter()
# Other possible preferences/variants
# -----------------------------------
# practice fwh?
# typical sparge schedule: 1, 2, 3 steps?
# all_grain journal detail level:
# - low: dough -> sparge -> boil-start
# - medium: dough -> batch1-start -> batch1-end [-> batchN-start -> batchN-end] -> boil-start
# - high:
# to get a graph of step relations/dependencies:
#
# ./manage.py shell
# from app import models
# print 'digraph G {';
# for step in models.new_step_types:
# for next in step.next_steps:
# print '%s -> %s;' % (step.id.replace('-', '_'), next.replace('-', '_'))
# print '}'
# | dot -Tpng -o step-graph.png -
new_step_types = [ StepType('buy', 'buy ingredients', no_filter, ['time'], ['starter', 'strike', 'steep']),
StepType('starter', 'make starter', StepFilter([filter_user_pref('pref_make_starter')]), ['time', 'volume'], ['strike', 'steep']),
StepType('strike', 'strike water', StepFilter([filter_recipe_type(['a', 'p'])]), ['volume', 'temp'], ['dough', 'mash']),
StepType('dough', 'dough-in', no_filter, ['volume', 'temp'], ['mash', 'sparge']),
StepType('mash', 'mash', no_filter, ['time', 'temp'], ['recirc', 'vorlauf', 'sparge']),
StepType('recirc', 'recirculation', no_filter, [], ['vorlauf', 'sparge']),
StepType('vorlauf', 'vorlauf', no_filter, ['time'], ['sparge']),
StepType('sparge', 'sparge', no_filter, ['volume', 'temp'], ['batch1-start', 'boil-start']),
StepType('fwh', 'first wort hopping', no_filter, ['time'], ['batch1-start', 'boil-start']),
StepType('batch1-start', '1st runnings, start', no_filter, ['gravity'], ['batch1-end']),
StepType('batch1-end', '1st runnings, end', no_filter, ['gravity', 'volume'], ['batch2-start', 'boil-start']),
StepType('batch2-start', '2nd runnings, start', no_filter, ['gravity'], ['batch2-end']),
StepType('batch2-end', '2nd runnings, end', no_filter, ['gravity', 'volume'], ['batch3-start', 'boil-start']),
StepType('batch3-start', '3rd runnings, start', no_filter, ['gravity'], ['batch3-end']),
StepType('batch3-end', '3rd runnings, end', no_filter, ['gravity', 'volume'], ['boil-start']),
StepType('steep', 'steep', StepFilter([filter_recipe_type(['e', 'p'])]), ['time', 'volume', 'temp'], ['boil-start']),
StepType('boil-start', 'boil, start', StepFilter([filter_recipe_type(['e'])]), ['time'], ['boil-add', 'boil-end']),
StepType('boil-add', 'boil, addition', no_filter, ['time'], ['boil-add', 'boil-end']),
StepType('boil-end', 'boil, end', no_filter, ['time'], ['pitch']),
StepType('pitch', 'pitch', no_filter, ['time', 'gravity', 'volume', 'temp'], ['ferm1']),
StepType('ferm1', 'primary fermentation', no_filter, ['time', 'gravity', 'temp'], ['sample', 'ferm2', 'ferm-add', 'lager', 'keg', 'bottle', 'aging']),
StepType('ferm2', 'secondary fermentation', StepFilter([filter_user_pref('pref_secondary_ferm')]), ['time', 'gravity', 'temp'],
['sample', 'ferm-add', 'lager', 'keg', 'bottle', 'aging']),
StepType('sample', 'gravity sample', no_filter, ['gravity'],
['ferm-add', 'ferm2', 'sample','lager', 'keg', 'bottle', 'aging']),
StepType('ferm-add', 'addition', no_filter, ['time'], ['sample', 'ferm-add', 'lager', 'keg', 'bottle', 'aging']),
StepType('lager', 'lagering', StepFilter([filter_recipe_is_lager()]), ['time', 'temp'], ['sample', 'condition', 'keg', 'bottle', 'aging']),
StepType('condition', 'conditioning', no_filter, ['time', 'temp'], ['keg', 'bottle', 'aging']),
StepType('keg', 'kegged', StepFilter([filter_user_pref('pref_dispensing_style', 'k')]), ['time', 'temp', 'gravity'], ['consumed']),
StepType('bottle', 'bottled', StepFilter([filter_user_pref('pref_dispensing_style', 'b')]), ['time'], ['consumed']),
StepType('aging', 'aging', StepFilter([filter_recipe_estimated_og_above(Decimal('1.100'))]), [], ['keg', 'bottle']),
StepType('consumed', 'consumed', no_filter, [], []),
]
step_types_by_id = dict([(type.id, type) for type in new_step_types])
step_types_ui_choices = [(type.id, type.label) for type in new_step_types]
def flatten(*args):
rtn = []
for arg in args:
rtn.extend(arg)
return rtn
Weight_Units = [
('gr', 'grams'),
('kg', 'kilograms'),
('oz', 'ounces'),
('lb', 'pounds'),
('ct', 'count'),
]
Volume_Units = [
('fl', 'fluid ounces'),
('c', 'cups'),
('q', 'quarts'),
('gl', 'gallons'),
('ml', 'milliliters'),
('l', 'liters'),
('ct', 'count'),
('tsp', 'teaspoon'),
('tbsp', 'tablespoon'),
('pt', 'pint')
]
Temp_Units = [
('f', 'Farenheit'),
('c', 'Celsius')
]
All_Units = flatten(Weight_Units, Volume_Units)
class Style (models.Model):
name = models.CharField(max_length=100)
bjcp_code = models.CharField(max_length=8)
parent = models.ForeignKey('self', null=True)
def __str__(self):
return self.__unicode__()
def __unicode__(self):
return u'%(name)s %(bjcp_code)s' % self.__dict__
class Grain (models.Model):
name = models.CharField(max_length=200)
extract_min = models.SmallIntegerField(null=True)
extract_max = models.SmallIntegerField(null=True)
lovibond_min = models.SmallIntegerField(null=True)
lovibond_max = models.SmallIntegerField(null=True)
description = models.CharField(max_length=200)
group = models.CharField(max_length=8)
def __str__(self):
return self.__unicode__()
def __unicode__(self):
return u'%(name)s' % self.__dict__
class Hop (models.Model):
name = models.CharField(max_length=100)
aau_low = models.DecimalField(max_digits=3, decimal_places=1)
aau_high = models.DecimalField(max_digits=3, decimal_places=1)
def __str__(self):
return self.__unicode__()
def __unicode__(self):
return u'%(name)s' % self.__dict__
class Adjunct (models.Model):
name = models.CharField(max_length=100)
group = models.CharField(max_length=30)
def __unicode__(self):
return u'%s (%s)' % (self.name, self.group)
class YeastManufacturer (models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return u'%(name)s' % self.__dict__
class Yeast (models.Model):
Types = [
('ale', 'Ale'),
('belgian', 'Belgian'),
('brett', 'Brettanomyces'),
('lager', 'Lager'),
('lambic', 'Lambic'),
('wine', 'Wine'),
('mead', 'Mead'),
('cider', 'Cider'),
('weird', 'Speciality'),
]
manufacturer = models.ForeignKey(YeastManufacturer)
ident = models.CharField(max_length=10)
name = models.CharField(max_length=100)
description = models.CharField(max_length=1000, blank=True)
type = models.CharField(max_length=7, choices=Types)
# @fixme: attenuation; min/max temp; URL; class
#Flocc_Types = ['low', 'medium-low', 'medium', 'medium-high', 'high']
#flocculation = models.CharField(max_length=10, choices=Floc_Types)
#attenuation_low = models.IntegerField()
#attenuation_high = models.IntegerField()
#temp_low = models.IntegerField()
#temp_high = models.IntegerField()
#alc_tolerance = models.DecimalField(max_digits=3, decimal_places=1)
def __unicode__(self):
return u'%s %s: %s' % (self.manufacturer.name, self.ident, self.name)
class Recipe (models.Model):
author = models.ForeignKey(auth.models.User)
name = models.CharField(max_length=200)
insert_date = models.DateTimeField(default=datetime.datetime.now)
batch_size = models.DecimalField(max_digits=4, decimal_places=2)
batch_size_units = models.CharField(max_length=4, choices = Volume_Units, default='gl')
style = models.ForeignKey(Style, null=True)
derived_from_recipe = models.ForeignKey('self', null=True, blank=True)
type = models.CharField(max_length=1, choices=RecipeTypes, default='a')
source_url = models.URLField(max_length=300, blank=True, null=True, verify_exists=True)
notes = models.TextField(null=True, blank=True)
def __str__(self):
return self.__unicode__()
def __unicode__(self):
return u'%(name)s - %(style)s %(batch_size)s' % {'name': self.name,
'style': self.style,
'batch_size': self.batch_size}
def url(self):
return u'/recipe/%d/%s' % (self.id, self.name)
class RecipeGrain (models.Model):
recipe = models.ForeignKey(Recipe)
grain = models.ForeignKey(Grain)
amount_value = models.DecimalField(max_digits=4, decimal_places=2)
amount_units = models.CharField(max_length=2, choices=Weight_Units, default='lb')
class RecipeHop (models.Model):
recipe = models.ForeignKey(Recipe)
hop = models.ForeignKey(Hop)
amount_value = models.DecimalField(max_digits=4, decimal_places=2)
amount_units = models.CharField(max_length=2, choices=Weight_Units)
boil_time = models.SmallIntegerField()
class RecipeYeast (models.Model):
recipe = models.ForeignKey(Recipe)
yeast = models.ForeignKey(Yeast)
ideal = models.BooleanField(default=True)
class RecipeAdjunct (models.Model):
recipe = models.ForeignKey(Recipe)
adjunct = models.ForeignKey(Adjunct)
amount_value = models.DecimalField(max_digits=5, decimal_places=2)
amount_units = models.CharField(max_length=4, choices=All_Units)
boil_time = models.SmallIntegerField()
notes = models.CharField(max_length=300, null=True, blank=True)
class StarredRecipe (models.Model):
'''
A Recipe a User has specifically called out (to be turned into a Brew, or whatever).
'''
recipe = models.ForeignKey(Recipe)
user = models.ForeignKey(auth.models.User)
when = models.DateTimeField(default=datetime.datetime.now)
notes = models.CharField(max_length=1000, blank=True, default='')
class BrewManager (models.Manager):
def brews_with_future_steps(self, user):
brew_ids = [step.brew_id for step in Step.objects.future_steps_for_user(user)]
return Brew.objects.filter(id__in=brew_ids)
def brews_pre_brew(self, user):
# @fixme: if the brew date is in the future, too?
future_brews = Brew.objects.brews_with_future_steps(user)
def pre_brew(brew):
future_steps = brew.future_steps()
# @fixme, this should probably be step.is_pre_brew() or something.
substeps = [step for step in future_steps if step.type in ['buy', 'strike', 'steep']]
return len(substeps) > 0
pre_brews = [brew for brew in future_brews if pre_brew(brew)]
return pre_brews
class Brew (models.Model):
# recipe_name = models.CharField(max_length=500)
brew_date = models.DateTimeField('brew date', null=True, blank=True, default=datetime.datetime.now)
brewer = models.ForeignKey(auth.models.User)
notes = models.TextField(null=True, blank=True)
recipe = models.ForeignKey(Recipe, null=True)
last_update_date = models.DateTimeField(null=True, editable=False)
last_state = models.CharField(max_length=30, choices=step_types_ui_choices, null=True, editable=False)
is_done = models.BooleanField(editable=False, default=False)
def __str__(self):
return self.__unicode__()
def __unicode__(self):
return u'%s (%s)' % (self.recipe.name, self.brewer.username)
objects = BrewManager()
class Meta:
ordering = ['brew_date', 'last_update_date']
def update_from_steps(self, steps = None):
'''
Based on the type and timestamp of the latest step of `steps`, update the state of the Brew.
It's the caller's responsibility to save the updated object.
'''
# only allow non-future steps to update state.
past_steps = [step for step in steps if not step.in_future()] or []
if len(past_steps) > 0:
last_step = past_steps[-1]
self.last_update_date = last_step.date
self.last_state = last_step.type
self.is_done = step_types_by_id[last_step.type].is_terminal()
if not self.brew_date:
# @fixme; this could be better, taking the first actually-brewing-related step, rather than just index=0.
# @fixme: then, get "is_actually_brewing_related" into StepTypes model
self.brew_date = past_steps[0].date
else:
self.brew_date = None
self.last_update_date = None
self.last_state = None
self.is_done = False
def next_steps(self):
gennie = NextStepGenerator(self)
return gennie.get_next_steps()
def future_steps(self):
return [step for step in self.step_set.all() if step.in_future()]
def title(self):
if not self.recipe:
return "unnamed"
return self.recipe.name
class StepManager (models.Manager):
def future_steps_for_user(self, user):
now = datetime.datetime.now()
return Step.objects.filter(brew__brewer__exact=user, date__gt=now)
class Step (models.Model):
'''
Individual steps/events/readings/samples associated with the brew instance.
'''
brew = models.ForeignKey(Brew)
type = models.CharField(max_length=30, choices=step_types_ui_choices)
date = models.DateTimeField()
entry_date = models.DateTimeField(editable=False, default=datetime.datetime.now)
volume = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True)
volume_units = models.CharField(max_length=4, null=True, blank=True, choices = Volume_Units, default='gl')
temp = models.IntegerField(null=True, blank=True)
temp_units = models.CharField(max_length=1, null=True, blank=True, choices = Temp_Units, default='f')
# gravity = models.DecimalField(max_digits=5, decimal_places=3, null=True, blank=True)
gravity_read = models.DecimalField(max_digits=5, decimal_places=3, null=True, blank=True)
gravity_read_temp = models.IntegerField(null=True, blank=True)
gravity_read_temp_units = models.CharField(max_length=1, null=True, blank=True, choices = Temp_Units, default='f')
notes = models.CharField(max_length=500, blank=True)
def _get_gravity(self):
if not self.gravity_read or not self.gravity_read_temp:
return None
temp = self.gravity_read_temp
if self.gravity_read_temp_units == 'c':
temp = celsius_to_farenheit(self.gravity_read_temp)
return correct_gravity(self.gravity_read, temp)
def _set_gravity(self, gravity):
self.gravity_read = gravity
self.gravity_read_temp = 59
self.gravity_read_temp_units = 'f'
gravity = property(_get_gravity, _set_gravity)
def __str__(self):
return self.__unicode__()
def __unicode__(self):
return u'[%s:%s:%s] vol=%s, temp=%s, gravity=%s, notes [%s]' % (self.brew.recipe.name, self.date.strftime('%x %X'), self.type, self.volume, self.temp, self.gravity, self.notes)
def in_future(self):
# @fixme: inject datetime.datetime for testability
return self.date > datetime.datetime.now()
objects = StepManager()
class Meta:
ordering = ['date']
class NextStep (object):
def __init__(self, type, date, existing_step=None):
self.type = type
self.date = date
self.existing_step = existing_step
def __unicode__(self):
existing_part = u''
if self.existing_step:
existing_part = ' (existing id %d)' % (self.existing_step.id)
date_part = u''
if self.date:
date_part = u' @ %s' % (self.date)
return u'[%s%s%s]' % (self.type.id, date_part, existing_part)
def __str__(self):
return self.__unicode__()
class NextSteps (object):
def __init__(self, possible=None, maybe=None):
self.possible = possible or []
self.maybe = maybe or []
def __unicode__(self):
return u'possible: %s, maybe: %s' % ([str(x) for x in self.possible],
[str(x) for x in self.maybe])
def __str__(self):
return self.__unicode__()
class NextStepGenerator (object):
'''
Encapsulate the process of getting the next-step structure from a brew, user, prefs, &c.
'''
def __init__(self, brew, **kwargs):
self._brew = brew
self._user = kwargs.get('user', brew.brewer)
self._recipe = brew.recipe
def get_next_steps(self):
next_steps = NextSteps()
to_try = []
last_date = None
if self._brew.last_state:
last_step_type = step_types_by_id[self._brew.last_state]
try:
last_step = [s for s in self._brew.step_set.all() if s.type == self._brew.last_state][0]
except Exception,e:
print 'brew,last_state,steps',self._brew,self._brew.last_state,self._brew.step_set.all()
raise
# @fixme: assert(last_step is not None)
last_date = last_step.date
to_try.extend(last_step_type.next_steps)
else:
# @fixme: get these from StepTypes themselves
to_try.extend(['starter', 'strike', 'steep', 'boil-start'])
future_steps = [step for step in self._brew.step_set.all() if step.in_future()]
for typeid in to_try:
steptype = step_types_by_id[typeid]
appropriate_list = None
if steptype.input_filter.eval(brew=self._brew, user=self._user, recipe=self._recipe):
appropriate_list = next_steps.possible
else:
appropriate_list = next_steps.maybe
#
existing_step = None
next_step_date = last_date
matching_future_steps = [step for step in future_steps if step.type == typeid]
if len(matching_future_steps) > 0:
existing_step = matching_future_steps[0]
if not next_step_date or existing_step.date > next_step_date:
next_step_date = existing_step.date
#
next_step = NextStep(steptype, next_step_date, existing_step)
appropriate_list.append(next_step)
return next_steps
class ShoppingList (object):
'''
Find a user's pre-brews with a future "buy-ingredients" step, and consolidates the ingredients by type
Each ingredient type is a list of (Ingredient,[(RecipeIngredient,Brew)])
E.g., Grains -> (Centenniel, [ (5oz,Brew#42), (2oz,Brew#43), ...])
''' # '
def __init__(self, user=None, **kwargs):
self._grains = {}
self._hops = {}
self._adjuncts = {}
self._yeasts = {}
pre_brews = kwargs.get('pre_brews', None)
if not pre_brews:
now = datetime.datetime.now()
future_buy_steps = Step.objects.filter(brew__brewer__exact=user, date__gt=now, type='buy')
future_buy_brews = [step.brew.id for step in future_buy_steps]
pre_brews = Brew.objects.filter(id__in=future_buy_brews)
self._aggregate_brews(pre_brews)
def shopping_to_do(self):
to_buy_count = len(self._grains) + len(self._hops) + len(self._adjuncts) + len(self._yeasts)
return to_buy_count > 0
def _get_grains(self):
return [(grain,brews) for grain,brews in self._grains.iteritems()]
grains = property(_get_grains)
def _get_hops(self):
return [(hop,brews) for hop,brews in self._hops.iteritems()]
hops = property(_get_hops)
def _get_adjuncts(self):
return [(adjunct,brews) for adjunct,brews in self._adjuncts.iteritems()]
adjuncts = property(_get_adjuncts)
def _get_yeasts(self):
return [(yeast,brews) for yeast,brews in self._yeasts.iteritems()]
yeasts = property(_get_yeasts)
def _aggregate_brews(self, pre_brews):
for brew in pre_brews:
recipe = brew.recipe
if not recipe:
continue
for collection, recipe_item_getter, item_type_getter in \
[(self._grains, lambda: recipe.recipegrain_set.all(), lambda x: x.grain),
(self._hops, lambda: recipe.recipehop_set.all(), lambda x: x.hop),
(self._adjuncts, lambda: recipe.recipeadjunct_set.all(), lambda x: x.adjunct),
(self._yeasts, lambda: recipe.recipeyeast_set.all(), lambda x: x.yeast)]:
for recipe_item in recipe_item_getter():
item = item_type_getter(recipe_item)
collection.setdefault(item, []).append((recipe_item,brew))
class TimeConst:
SECOND = 1000
MINUTE = 60 * SECOND
HOUR = 60 * MINUTE
DAY = 24 * HOUR
WEEK = 7 * DAY
def celsius_to_farenheit(temp):
'''
>>> celsius_to_farenheit(0.0)
Decimal('32.00')
>>> celsius_to_farenheit(100.0)
Decimal('212.00')
>>> celsius_to_farenheit(-17.78)
Decimal('-0.004')
'''
temp = str(temp)
if temp.lstrip('-').find('.') == -1:
temp = temp + '.'
parts = temp.split('.')
prec = len(parts[0]) + len(parts[1])
context = Context(prec=prec)
_1_8 = context.create_decimal('1.8')
_32 = context.create_decimal('32.')
temp = context.create_decimal(temp)
return _1_8 * temp + _32
_c1 = Decimal('1.313454')
_c2 = Decimal('0.132674')
_c3 = Decimal('0.002057793')
_c4 = Decimal('0.000002627634')
_adj = Decimal('0.001')
def correct_gravity(gravity, temp_f):
'''
All temperatures are in expressed in degrees F.
correction = 1.313454 - 0.132674*F + 0.002057793*F*F - 0.000002627634*F*F*F
SG_corrected = SG + (correction * 0.001)
- http://www.primetab.com/formulas.html
- agrees with http://brewery.org/library/HydromCorr0992.html
>>> correct_gravity(1.042, 108)
Decimal('1.050')
>>> correct_gravity(1.050, 82)
Decimal('1.053')
>>> correct_gravity(1.0123103856, 59)
Decimal('1.012')
>>> correct_gravity(1.070, 41)
Decimal('1.069')
'''
gravity = Decimal(str(gravity))
F = Decimal(str(temp_f))
ctx = Context(prec=4, rounding=ROUND_HALF_UP)
gravity = gravity + (_c1 - _c2 * F + _c3 * F * F - _c4 * F * F * F) * _adj
return ctx.create_decimal(gravity)
def convert_volume(volume, from_units, to_units):
ctx = Context(prec=5)
volume = ctx.create_decimal(volume)
def simplify_volume(units):
factor = ctx.create_decimal('1')
if units == 'fl':
factor *= ctx.create_decimal('0.125')
units = 'c'
if units == 'c':
factor *= ctx.create_decimal('0.25')
units = 'q'
if units == 'q':
factor *= ctx.create_decimal('0.25')
units = 'gl'
if units == 'ml':
factor *= ctx.create_decimal('0.001')
units = 'l'
return (units, factor)
from_units,from_factor = simplify_volume(from_units)
to_units,to_factor = simplify_volume(to_units)
inv_to_factor = ctx.create_decimal('1') / to_factor
conversion_from_to = {
'l': {'l': ctx.create_decimal('1'),
'gl': ctx.create_decimal('0.264172052')},
'gl': {'l': ctx.create_decimal('3.78541178'),
'gl': ctx.create_decimal('1')},
}
return volume * from_factor * conversion_from_to[from_units][to_units] * inv_to_factor
def convert_weight(amount, from_units, to_units):
'''
>> convert_weight(1, 'lb', 'kg')
Decimal('0.4539237')
>>> convert_weight(1, 'kg', 'lb')
Decimal('2.20462500000')
>>> convert_weight(16, 'oz', 'lb')
Decimal('1.0000')
>>> convert_weight(32, 'oz', 'lb')
Decimal('2.0000')
>>> convert_weight(1500, 'gr', 'lb')
Decimal('3.30693750000')
>>> convert_weight(5, 'tsp', 'gr')
Decimal('15')
'''
ctx = Context(prec=6)
conversion_from_to = {
'gr': {'gr': ctx.create_decimal('1'),
'oz': ctx.create_decimal('0.0352739619')},
'oz': {'gr': ctx.create_decimal('28.35'),
'oz': ctx.create_decimal('1')},
}
def simplify_units(unit):
factor = ctx.create_decimal('1')
if unit == 'kg':
factor = factor * ctx.create_decimal('1000')
unit = 'gr'
if unit == 'lb':
factor = factor * ctx.create_decimal('16')
unit = 'oz'
if unit == 'tbsp':
factor = factor * ctx.create_decimal('3')
unit = 'tsp'
if unit == 'tsp':
factor = factor * ctx.create_decimal('3')
unit = 'gr'
if unit == 'pint':
factor = factor * ctx.create_decimal('910')
unit = 'gr'
return (unit,factor)
from_units,from_factor = simplify_units(from_units)
to_units,to_factor = simplify_units(to_units)
inv_to_factor = ctx.create_decimal('1') / to_factor
return amount * from_factor * conversion_from_to[from_units][to_units] * inv_to_factor
class BrewDerivations (object):
def __init__(self, brew):
self._brew = brew
def _get_sorted_steps(self, allowable_step_types_sorted):
allowable_types = dict([(type,idx)
for type,idx
in itertools.izip(allowable_step_types_sorted,itertools.count())])
steps = self._brew.step_set.all()
steps = [step for step in steps
if allowable_types.has_key(step.type)]
steps.sort(lambda a,b: allowable_types[a.type] - allowable_types[b.type])
return steps
efficiency_needed_steps = ['boil-start', 'sparge', 'pitch']
def _get_efficiency_steps(self):
related_steps = self._get_sorted_steps(BrewDerivations.efficiency_needed_steps)
related_steps = [step for step in related_steps if step.volume and step.gravity]
return related_steps
def can_not_derive_efficiency(self):
'''@return A list of Strings describing what needs to be provided, or [] if we can perform the op'''
rtn = []
required = self._get_efficiency_steps()
if len(required) == 0:
rtn.append('need one step of of type %s with gravity and volume' % (BrewDerivations.efficiency_needed_steps))
recipe_grains = self._brew.recipe.recipegrain_set.all()
if len(recipe_grains) == 0:
rtn.append('need grains on recipe')
return rtn
def efficiency(self):
related_steps = self._get_efficiency_steps()
if len(related_steps) == 0:
raise Exception('assertion violation')
best_step = related_steps[0]
ctx = Context(prec=3, rounding=ROUND_HALF_UP)
potential_points = ctx.create_decimal('0')
for recipe_grain in self._brew.recipe.recipegrain_set.all():
grain = recipe_grain.grain
min,max = tuple([ctx.create_decimal(str(x - 1000)) for x in [grain.extract_min,grain.extract_max]])
grain_potential_per_lb = (min + max) / ctx.create_decimal('2')
grain_in_lbs = convert_weight(recipe_grain.amount_value, recipe_grain.amount_units, 'lb')
recipe_grain_potential = grain_potential_per_lb * grain_in_lbs
potential_points += recipe_grain_potential
volume_in_gallons = convert_volume(best_step.volume, best_step.volume_units, 'gl')
grav = ((best_step.gravity - ctx.create_decimal('1')) * ctx.create_decimal('1000'))
obtained_points = grav * volume_in_gallons
efficiency = (obtained_points / potential_points) * ctx.create_decimal('100')
return efficiency
abv_start_steps = ['pitch', 'boil-end', 'boil-start']
abv_end_steps = ['consumed', 'aging', 'bottle', 'keg', 'condition', 'lager', 'ferm-add', 'sample', 'ferm2', 'ferm1']
def _get_abv_steps(self):
starting_steps = self._get_sorted_steps(BrewDerivations.abv_start_steps)
starting_steps = [step for step in starting_steps if step.gravity]
ending_steps = self._get_sorted_steps(BrewDerivations.abv_end_steps)
ending_steps = [step for step in ending_steps if step.gravity]
return starting_steps,ending_steps
def can_not_derive_abv(self):
starting_steps,ending_steps = self._get_abv_steps()
rtn = []
if len(starting_steps) == 0:
rtn.append('need a starting-gravity step of type %s with gravity reading' % (BrewDerivations.abv_start_steps))
if len(ending_steps) == 0:
rtn.append('need a ending-gravity step of type %s with gravity reading' % (BrewDerivations.abv_end_steps))
return rtn
def alcohol_by_volume(self):
starting_steps,ending_steps = self._get_abv_steps()
best_start = starting_steps[0]
best_end = ending_steps[0]
return (best_start.gravity - best_end.gravity) * Decimal('135')
def can_not_derive_aa(self):
return self.can_not_derive_abv()
def apparent_attenuation(self):
starting_steps,ending_steps = self._get_abv_steps()
best_start = starting_steps[0]
best_end = ending_steps[0]
fraction_attenuated = (best_end.gravity - Decimal('1.0')) / (best_start.gravity - Decimal('1.0'))
aa = (Decimal('1.0') - fraction_attenuated) * Decimal('100')
return aa
class PerHopIbu (object):
def __init__(self, recipe_hop, low_ibu, high_ibu, percentage=None):
self._recipe_hop = recipe_hop
self._low_ibu = low_ibu
self._high_ibu = high_ibu
self._percentage = percentage
recipe_hop = property(lambda s: s._recipe_hop)
low_ibu = property(lambda s: s._low_ibu)
high_ibu = property(lambda s: s._high_ibu)
def _set_pctg(self, x):
self._percentage = x
percentage = property(lambda s: s._percentage, _set_pctg)
class IbuDerivation (object):
def __init__(self, low_ibu, high_ibu, per_hop):
self._low_ibu = low_ibu
self._high_ibu = high_ibu
self._per_hop = per_hop or []
low = property(lambda s: s._low_ibu)
high = property(lambda s: s._high_ibu)
average = property(lambda s: (s._low_ibu + s._high_ibu) / Decimal('2'))
per_hop = property(lambda s: s._per_hop)
class PerGrainOg (object):
def __init__(self, recipe_grain, low_og, high_og, percentage=None):
self._recipe_grain = recipe_grain
self._low_og = low_og
self._high_og = high_og
self._percentage = percentage
recipe_grain = property(lambda s: s._recipe_grain)
low_og = property(lambda s: s._low_og)
high_og = property(lambda s: s._high_og)
def _set_pctg(self, x):
self._percentage = x
percentage = property(lambda s: s._percentage, _set_pctg)
class OgDerivation (object):
def __init__(self, low_og, high_og, per_grain):
self._low_og = low_og
self._high_og = high_og
self._per_grain = per_grain or []
low = property(lambda s: s._low_og)
high = property(lambda s: s._high_og)
average = property(lambda s: (s._low_og + s._high_og) / Decimal('2'))
per_grain = property(lambda s: s._per_grain)
class PerGrainSrm (object):
def __init__(self, recipe_grain, low_srm, high_srm, percentage=None):
self._recipe_grain = recipe_grain
self._low_srm = low_srm
self._high_srm = high_srm
self._percentage = percentage
recipe_grain = property(lambda s: s._recipe_grain)
low_srm = property(lambda s: s._low_srm)
high_srm = property(lambda s: s._high_srm)
def _set_pctg(self, x):
self._percentage = x
percentage = property(lambda s: s._percentage, _set_pctg)
class SrmDerivation (object):
def __init__(self, low_srm, high_srm, per_grain):
self._low_srm = low_srm
self._high_srm = high_srm
self._per_grain = per_grain or []
low = property(lambda s: s._low_srm)
high = property(lambda s: s._high_srm)
average = property(lambda s: (s._low_srm + s._high_srm) / Decimal('2'))
per_grain = property(lambda s: s._per_grain)
class RecipeDerivations (object):
def __init__(self, recipe):
self._recipe = recipe
default_efficiency = Decimal('0.75')
def _test_batch_size_deriv(self, reasons):
has_batch_size = False
has_non_zero_batch_size = False
try:
if self._recipe.batch_size is not None and self._recipe.batch_size_units is not None:
has_batch_size = True
if self._recipe.batch_size > 0:
has_non_zero_batch_size = True
except AttributeError,e:
pass
if not has_batch_size:
reasons.append('recipe must have batch size and units')
if not has_non_zero_batch_size:
reasons.append('recipe must have non-zero batch size')
def _test_grains_deriv(self, reasons):
has_grains = False
try:
if self._recipe.recipegrain_set and self._recipe.recipegrain_set.count() > 0:
has_grains = True
except AttributeError,e:
pass
if not has_grains:
reasons.append('recipe must have grains')
def _test_hops_deriv(self, reasons):
has_hops = False
try:
if self._recipe.recipehop_set.count() > 0:
has_hops = True
except AttributeError,e:
pass
if not has_hops:
reasons.append('recipe must have hops')
def can_not_derive_og(self):
'''@return a list of string reasons why we cannot compute an original gravity'''
reasons = []
self._test_batch_size_deriv(reasons)
self._test_grains_deriv(reasons)
return reasons
def compute_og(self, efficiency=None):
'''@return OgDerivation'''
efficiency = efficiency or RecipeDerivations.default_efficiency
batch_gallons = convert_volume(self._recipe.batch_size, self._recipe.batch_size_units, 'gl')
low_accum,high_accum = Decimal('0'),Decimal('0')
def convert_to_gravity(val, batch_gallons):