# Generated by Django 2.1.3 on 2018-11-02 14:39

from django.db import migrations

def add_units(apps, schema_editor):
    IngredientUnit = apps.get_model('recipes', 'IngredientUnit')
    ingredients = [
        ('teaspoon', 't'),
        ('tablespoon', 'T'),
        ('fluid ounce', 'fluid oz'),
        ('cup', 'c'),
        ('pint', 'pt'),
        ('quart', 'qt'),
        ('gallon', 'gal'),

        ('pound', 'lb'),
        ('ounce', 'oz'),
        ('gram', 'g'),

        ('count', 'ct'),
        ('dozen', 'dz'),
        ('bunch', 'bunch'),
        ('clove', 'clove'),
        ('inch', 'in'), # Ginger, etc.
        ('can', 'can'),
        ('stalk', 'stalk'),
        ('pinch', 'pinch'), 

        ('milliliter', 'ml')
        
    ]
    for name, short in ingredients:
        IngredientUnit(name=name, short=short).save()        

    IngredientUnitConversion = apps.get_model('recipes', 'IngredientUnitConversion')
    conversions = [
        ('gal', 'qt', 4),
        ('qt', 'pt', 2),
        ('pt', 'c', 2),
        ('c', 'fluid oz', 8),
        ('fluid oz', 'T', 2),
        ('T', 't', 3),

        ('lb', 'g', 453.592),
        ('lb', 'oz', 16),

        ('dz', 'ct', 12),
    ]
    for source, target, scale in conversions:
        IngredientUnitConversion(
            source=IngredientUnit.objects.get(short=source),
            target=IngredientUnit.objects.get(short=target),
            scale=scale
        ).save()
        IngredientUnitConversion(
            source=IngredientUnit.objects.get(short=target),
            target=IngredientUnit.objects.get(short=source),
            scale=1/scale
        ).save()

class Migration(migrations.Migration):

    dependencies = [
        ('recipes', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(add_units)
    ]

