# Generated by Django 2.1.1 on 2018-10-06 12:46

from django.db import migrations


def add_images(apps, schema_editor):
    images = ['products/images/products/img_{}.jpg'.format(i) for i in range(1, 8)]

    image = apps.get_model('products', 'Image')
    for img in images:
        image.objects.create(img=img)


def add_markers(apps, schema_editor):
    markers = ['corner_new', 'corner_hot', 'None']

    marker = apps.get_model('products', 'ProductMarker')
    for mrk in markers:
        marker.objects.create(corner=mrk)


def add_categories(apps, schema_editor):
    categories = ['Category_' + str(i) for i in range(1, 15)]

    category = apps.get_model('products', 'Category')
    for cat in categories:
        category.objects.create(title=cat)


def add_products(apps, schema_editor):
    import random
    products = []
    product_markers = apps.get_model('products', 'ProductMarker').objects.all()
    categories = apps.get_model('products', 'Category').objects.all()
    images = apps.get_model('products', 'Image').objects.all()

    for i in range(1, 101):
        products.append(
            {
                'name': 'Product_' + str(i),
                'short_text': 'Product short text',
                'long_text': 'Product long text',
                'now_price': random.randint(1000, 1500),
                'old_price': random.randint(1500, 2000),
                'product_marker': random.choice(product_markers),
                'category': random.choice(categories),
                'image': random.choice(images)
            }
        )

    product = apps.get_model('products', 'Product')
    for prod in products:
        product.objects.create(
            name=prod['name'],
            short_text=prod['short_text'],
            long_text=prod['long_text'],
            now_price=prod['now_price'],
            old_price=prod['old_price'],
            product_marker=prod['product_marker'],
            category=prod['category'],
            image=prod['image']
        )

class Migration(migrations.Migration):

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

    operations = [
        migrations.RunPython(
            add_images,
            lambda x, y: (x, y)
        ),
        migrations.RunPython(
            add_markers,
            lambda x, y: (x, y)
        ),
        migrations.RunPython(
            add_categories,
            lambda x, y: (x, y)
        ),
        migrations.RunPython(
            add_products,
            lambda x, y: (x, y)
        )
    ]
