# -*- coding: utf-8 -*-
# Generated by Django 1.11.8 on 2017-12-03 22:29
from __future__ import unicode_literals

import random

from django.db import migrations

from blog.utils import saved, get_text


TAGS_TITLES = (
    'Advertising', 'Advice', 'Africa', 'Android', 'Anime',
    'Apple', 'Architecture', 'Art', 'Baking', 'Beauty', 'Bible',
    'Blog', 'Blogging', 'Book Reviews', 'Books', 'Branding',
    'Business', 'Canada', 'Cars', 'Cartoons', 'Celebrities',
    'Celebrity', 'Children', 'Christian', 'Christianity',
    'Comedy', 'Comics', 'Cooking', 'Cosmetics', 'Crafts',
    'Cuisine', 'Culinary', 'Culture', 'Dating', 'Design',
    'Diy', 'Dogs', 'Drawing', 'Economy', 'Education',
    'Entertainment', 'Environment', 'Events', 'Exercise',
    'Faith', 'Family', 'Fantasy', 'Porn'
)

TAGS_COUNT = len(TAGS_TITLES)


def add_data(apps, schema):
    Category = apps.get_model('blog.Category')
    BlogPost = apps.get_model('blog.BlogPost')
    TextBlock = apps.get_model('blog.TextBlock')
    Tag = apps.get_model('blog.Tag')

    # First - create some categories
    categories = saved(
        Category(title=title)
        for title in ('Fan', 'Porno', 'Other'))

    # Now create some posts
    posts = saved(
        BlogPost(
            title=get_text(2),
            lead=get_text(10),
            body=get_text(100),
            category=random.choice(categories)
        ) for x in range(5)
    )

    for x in range(3):
        TextBlock(
            title=get_text(1),
            body=get_text(50),
            blog_post=posts[-1]
        ).save()

    tags = saved(Tag(title=title) for title in TAGS_TITLES)

    for post in BlogPost.objects.all():
        post.tags.add(*random.sample(tags, random.randint(0, TAGS_COUNT-1)))


def remove_data(apps, schema):
    Category = apps.get_model('blog.Category')
    BlogPost = apps.get_model('blog.BlogPost')
    TextBlock = apps.get_model('blog.TextBlock')
    Tag = apps.get_model('blog.Tag')

    Tag.objects.all().delete()
    TextBlock.objects.all().delete()
    BlogPost.objects.all().delete()
    Category.objects.all().delete()


class Migration(migrations.Migration):

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

    operations = [
        migrations.RunPython(add_data, remove_data)
    ]
