# -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-04-04 18:59
from __future__ import unicode_literals

from django.db import migrations


def publish_news(apps, schema_editor):
    from articles.models import Article as Article
    from django.utils.translation import activate
    activate('es')

    articles = Article.objects.filter(is_draft=True, public=None)
    for article in articles.iterator():
        if article.is_published:
            for translation in article.translations.all():
                activate(translation.language_code)
                article.refresh_from_db()
                article.publish(translation.language_code)


def unpublish_news(apps, schema_editor):
    from articles.models import Article as Article
    Article.objects.filter(is_draft=False).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('articles', '0005_load_news_tags'),
    ]
    operations = [
        migrations.RunPython(publish_news, unpublish_news),
    ]
