Skip to content

Commit

Permalink
Feature/fractions (#9)
Browse files Browse the repository at this point in the history
* model changes and migrations to support fractions

* adding some comments and removing quantity

* updating models to allow floats
  • Loading branch information
RyanNoelk authored Apr 5, 2018
1 parent edcb28d commit 2b41bdc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
23 changes: 23 additions & 0 deletions v1/ingredient/migrations/0009_auto_20180330_1055.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 2.0.1 on 2018-03-30 10:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('ingredient', '0008_auto_20180129_1456'),
]

operations = [
migrations.AddField(
model_name='ingredient',
name='denominator',
field=models.FloatField(default=1, verbose_name='denominator'),
),
migrations.AddField(
model_name='ingredient',
name='numerator',
field=models.FloatField(default=1, verbose_name='numerator'),
),
]
26 changes: 26 additions & 0 deletions v1/ingredient/migrations/0010_auto_20180330_1058.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 2.0.1 on 2018-03-30 10:58

from django.db import migrations
from fractions import Fraction


def make_fraction(apps, schema_editor):
# We can't import the Ingredient model directly as it may be a newer
# version than this migration expects. We use the historical version.
Ingredient = apps.get_model('ingredient', 'Ingredient')
for i in Ingredient.objects.all():
fraction = Fraction(i.quantity).limit_denominator(100)
i.numerator = fraction.numerator
i.denominator = fraction.denominator
i.save()


class Migration(migrations.Migration):

dependencies = [
('ingredient', '0009_auto_20180330_1055'),
]

operations = [
migrations.RunPython(make_fraction),
]
11 changes: 9 additions & 2 deletions v1/ingredient/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ class Ingredient(models.Model):
Ingredients share a many to one relationship.
Meaning each Ingredient Group will have many Ingredients.
:title: = Title of the Ingredient (EX: Flour)
:quantity: = Title of the Ingredient (EX: 200, 15, 2)
:measurement: = Title of the Ingredient (EX: Liters, Cups, Grams, tablespoons)
:numerator: = Numerator of the quantity expressed as a fraction
:denominator: = Denominator of the quantity expressed as a fraction
:measurement: = Measurement of the Ingredient (EX: Liters, Cups, Grams, tablespoons)
:quantity: = Amount of the Ingredient Needed (EX: 200, 15, 2)
"""
# TODO: quantity is no longer used. (Apr. 5 2018)
# It is only here to allow for rollbacks to older version .
# It should be removed in future versions.
title = models.CharField(_('title'), max_length=250)
quantity = models.FloatField(_('quantity'), default=0)
numerator = models.FloatField(_('numerator'), default=1)
denominator = models.FloatField(_('denominator'), default=1)
measurement = models.CharField(_('measurement'), max_length=200, blank=True, null=True)
ingredient_group = models.ForeignKey(IngredientGroup, on_delete=models.CASCADE, related_name='ingredients', null=True)

Expand Down
8 changes: 7 additions & 1 deletion v1/ingredient/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ class IngredientSerializer(serializers.ModelSerializer):
""" Standard `rest_framework` ModelSerializer """
class Meta:
model = Ingredient
fields = ['id', 'quantity', 'measurement', 'title']
fields = [
'id',
'numerator',
'denominator',
'measurement',
'title'
]


class IngredientGroupSerializer(serializers.ModelSerializer):
Expand Down

0 comments on commit 2b41bdc

Please sign in to comment.