Rewrite to Django from scratch. Now it's much more user-friendly.
[cookbook.git] / cookbook / models.py
diff --git a/cookbook/models.py b/cookbook/models.py
new file mode 100644 (file)
index 0000000..3d75d57
--- /dev/null
@@ -0,0 +1,111 @@
+from django.db import models
+import markdown
+from taggit.managers import TaggableManager
+
+
+class UnitType (models.Model):
+    "Weight, length, count, time, etc."
+    name = models.CharField(max_length=40)
+
+    def __unicode__(self):
+        return u'{0.name}'.format(self)
+
+class UnitSystem (models.Model):
+    "SI, CGS, British Imperial, US, etc."
+    name = models.CharField(max_length=40)
+
+    def __unicode__(self):
+        return u'{0.name}'.format(self)
+
+class Unit (models.Model):
+    "Kilograms, pounds, liters, gallons, etc."
+    abbrev = models.CharField('abbreviation', max_length=6)
+    name = models.CharField(max_length=40)
+    type = models.ForeignKey(UnitType)
+    system = models.ForeignKey(UnitSystem)
+    si_scale = models.DecimalField(max_digits=30, decimal_places=15)
+    si_offset = models.DecimalField(max_digits=30, decimal_places=15)
+
+    def __unicode__(self):
+        return u'{0.abbrev}'.format(self)
+
+class Amount (models.Model):
+    "1 kg, 2-3 lb., 0.5 (0.3-0.6) gal., etc."
+    unit = models.ForeignKey(Unit)
+    value = models.DecimalField(max_digits=10, decimal_places=4)
+    min_value = models.DecimalField(
+        'minimum value', max_digits=10, decimal_places=4,
+        null=True, blank=True)
+    max_value = models.DecimalField(
+        'maximum value', max_digits=10, decimal_places=4,
+        null=True, blank=True)
+
+    def __unicode__(self):
+        if self.min_value is None and self.max_value is None:
+            value = self.value
+        elif self.min_value is None:
+            value = '{0.value}-{0.max_value}'.format(self)
+        elif self.max_value is None:
+            value = '{0.min_value}-{0.value}'.format(self)
+        else:
+            value = '{0.value} ({0.min_value}-{0.max_value})'.format(self)
+        return u'{0} {1.unit}'.format(value, self)
+
+class Recipe (models.Model):
+    name = models.CharField(max_length=200)
+    directions_markdown = models.TextField(
+        'directions', help_text='Markdown syntax')
+    directions = models.TextField('directions as HTML', blank=True, null=True)
+    # yield is a reserved word
+    x_yield = models.OneToOneField(
+        Amount, verbose_name='yield', db_column='yield', null=True, blank=True)
+    author = models.CharField(max_length=200, null=True, blank=True)
+    source = models.CharField(max_length=200, null=True, blank=True)
+    url = models.URLField('URL', null=True, blank=True)
+    tags = TaggableManager(blank=True)
+
+    class Meta:
+        ordering = ['name']
+
+    def __unicode__(self):
+        return u'{0.name}'.format(self)
+
+    def save(self):
+        # https://code.djangoproject.com/wiki/UsingMarkup
+        self.directions = markdown.markdown(self.directions_markdown)
+        super(Recipe, self).save()
+
+
+class IngredientBlock (models.Model):
+    name = models.CharField(max_length=200)
+    directions_markdown = models.TextField(
+        'directions', help_text='markdown syntax', blank=True, null=True)
+    directions = models.TextField('directions as HTML', blank=True, null=True)
+    recipe = models.ForeignKey(Recipe)
+
+    class Meta:
+        ordering = ['recipe', 'name']
+
+    def __unicode__(self):
+        return u'{0.name}'.format(self)
+
+    def save(self):
+        if self.directions_markdown:
+            self.directions = markdown.markdown(self.directions_markdown)
+        super(IngredientBlock, self).save()
+
+
+class Ingredient (models.Model):
+    "1 kg, 2 lb., 3.4 L, 0.5 gal., etc."
+    amount = models.OneToOneField(Amount, null=True, blank=True)
+    name = models.CharField(max_length=200)
+    note = models.CharField(max_length=200, null=True, blank=True)
+    block = models.ForeignKey(IngredientBlock)
+
+    def __unicode__(self):
+        fmt = '{0.name}'
+        if self.amount:
+            fmt = '{0.amount} ' + fmt
+        if self.note:
+            fmt += u', {0.note}'
+        return fmt.format(self)