diff --git a/v1/ingredient/migrations/0009_auto_20180330_1055.py b/v1/ingredient/migrations/0009_auto_20180330_1055.py new file mode 100644 index 0000000..ead9c7d --- /dev/null +++ b/v1/ingredient/migrations/0009_auto_20180330_1055.py @@ -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'), + ), + ] diff --git a/v1/ingredient/migrations/0010_auto_20180330_1058.py b/v1/ingredient/migrations/0010_auto_20180330_1058.py new file mode 100644 index 0000000..317807d --- /dev/null +++ b/v1/ingredient/migrations/0010_auto_20180330_1058.py @@ -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), + ] diff --git a/v1/ingredient/models.py b/v1/ingredient/models.py index 0475db5..8489d26 100644 --- a/v1/ingredient/models.py +++ b/v1/ingredient/models.py @@ -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) diff --git a/v1/ingredient/serializers.py b/v1/ingredient/serializers.py index 2439d85..17f8646 100644 --- a/v1/ingredient/serializers.py +++ b/v1/ingredient/serializers.py @@ -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):