3d75d57a5acecbe360364d40cf908a369cadf7fa
[cookbook.git] / cookbook / models.py
1 from django.db import models
2 import markdown
3 from taggit.managers import TaggableManager
4
5
6 class UnitType (models.Model):
7     "Weight, length, count, time, etc."
8     name = models.CharField(max_length=40)
9
10     def __unicode__(self):
11         return u'{0.name}'.format(self)
12
13 class UnitSystem (models.Model):
14     "SI, CGS, British Imperial, US, etc."
15     name = models.CharField(max_length=40)
16
17     def __unicode__(self):
18         return u'{0.name}'.format(self)
19
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)
28
29     def __unicode__(self):
30         return u'{0.abbrev}'.format(self)
31
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)
42
43     def __unicode__(self):
44         if self.min_value is None and self.max_value is None:
45             value = self.value
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)
50         else:
51             value = '{0.value} ({0.min_value}-{0.max_value})'.format(self)
52         return u'{0} {1.unit}'.format(value, self)
53
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)
66
67     class Meta:
68         ordering = ['name']
69
70     def __unicode__(self):
71         return u'{0.name}'.format(self)
72
73     def save(self):
74         # https://code.djangoproject.com/wiki/UsingMarkup
75         self.directions = markdown.markdown(self.directions_markdown)
76         super(Recipe, self).save()
77
78
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)
85
86     class Meta:
87         ordering = ['recipe', 'name']
88
89     def __unicode__(self):
90         return u'{0.name}'.format(self)
91
92     def save(self):
93         if self.directions_markdown:
94             self.directions = markdown.markdown(self.directions_markdown)
95         super(IngredientBlock, self).save()
96
97
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)
104
105     def __unicode__(self):
106         fmt = '{0.name}'
107         if self.amount:
108             fmt = '{0.amount} ' + fmt
109         if self.note:
110             fmt += u', {0.note}'
111         return fmt.format(self)