1 from django.db import models
3 from taggit.managers import TaggableManager
6 class UnitType (models.Model):
7 "Weight, length, count, time, etc."
8 name = models.CharField(max_length=40)
10 def __unicode__(self):
11 return u'{0.name}'.format(self)
13 class UnitSystem (models.Model):
14 "SI, CGS, British Imperial, US, etc."
15 name = models.CharField(max_length=40)
17 def __unicode__(self):
18 return u'{0.name}'.format(self)
20 class Unit (models.Model):
21 "Kilograms, pounds, liters, gallons, etc."
22 abbrev = models.CharField('abbreviation', max_length=6)
23 name = models.CharField(max_length=40)
24 type = models.ForeignKey(UnitType)
25 system = models.ForeignKey(UnitSystem)
26 si_scale = models.DecimalField(max_digits=30, decimal_places=15)
27 si_offset = models.DecimalField(max_digits=30, decimal_places=15)
29 def __unicode__(self):
30 return u'{0.abbrev}'.format(self)
32 class Amount (models.Model):
33 "1 kg, 2-3 lb., 0.5 (0.3-0.6) gal., etc."
34 unit = models.ForeignKey(Unit)
35 value = models.DecimalField(max_digits=10, decimal_places=4)
36 min_value = models.DecimalField(
37 'minimum value', max_digits=10, decimal_places=4,
38 null=True, blank=True)
39 max_value = models.DecimalField(
40 'maximum value', max_digits=10, decimal_places=4,
41 null=True, blank=True)
43 def __unicode__(self):
44 if self.min_value is None and self.max_value is None:
46 elif self.min_value is None:
47 value = '{0.value}-{0.max_value}'.format(self)
48 elif self.max_value is None:
49 value = '{0.min_value}-{0.value}'.format(self)
51 value = '{0.value} ({0.min_value}-{0.max_value})'.format(self)
52 return u'{0} {1.unit}'.format(value, self)
54 class Recipe (models.Model):
55 name = models.CharField(max_length=200)
56 directions_markdown = models.TextField(
57 'directions', help_text='Markdown syntax')
58 directions = models.TextField('directions as HTML', blank=True, null=True)
59 # yield is a reserved word
60 x_yield = models.OneToOneField(
61 Amount, verbose_name='yield', db_column='yield', null=True, blank=True)
62 author = models.CharField(max_length=200, null=True, blank=True)
63 source = models.CharField(max_length=200, null=True, blank=True)
64 url = models.URLField('URL', null=True, blank=True)
65 tags = TaggableManager(blank=True)
70 def __unicode__(self):
71 return u'{0.name}'.format(self)
74 # https://code.djangoproject.com/wiki/UsingMarkup
75 self.directions = markdown.markdown(self.directions_markdown)
76 super(Recipe, self).save()
79 class IngredientBlock (models.Model):
80 name = models.CharField(max_length=200)
81 directions_markdown = models.TextField(
82 'directions', help_text='markdown syntax', blank=True, null=True)
83 directions = models.TextField('directions as HTML', blank=True, null=True)
84 recipe = models.ForeignKey(Recipe)
87 ordering = ['recipe', 'name']
89 def __unicode__(self):
90 return u'{0.name}'.format(self)
93 if self.directions_markdown:
94 self.directions = markdown.markdown(self.directions_markdown)
95 super(IngredientBlock, self).save()
98 class Ingredient (models.Model):
99 "1 kg, 2 lb., 3.4 L, 0.5 gal., etc."
100 amount = models.OneToOneField(Amount, null=True, blank=True)
101 name = models.CharField(max_length=200)
102 note = models.CharField(max_length=200, null=True, blank=True)
103 block = models.ForeignKey(IngredientBlock)
105 def __unicode__(self):
108 fmt = '{0.amount} ' + fmt
111 return fmt.format(self)