# -*- coding: utf-8 -*-
# Generated by Django 1.11.9 on 2018-03-05 17:26
from __future__ import unicode_literals

from django.db import migrations


def delete_repeated_news(apps, schema_editor):
    import json
    from django.conf import settings
    from django.utils.translation import activate
    ArticleTranslation = apps.get_model(
        'aldryn_newsblog', 'ArticleTranslation')

    from aldryn_newsblog.models import Article as RealArticle

    # open posts-lang.json
    with open(settings.BASE_DIR + '/gobcl_cms/utils/posts-lang.json') as news:
        json_news = json.loads(news.read())

    for news in json_news:
        title = news.get('titulo', '')
        language = news.get('lang', 'es')

        if language == 'es-CL':
            continue

        article_translation = ArticleTranslation.objects.filter(
            title=title,
        )

        if not article_translation.exists():
            continue

        article_translation = article_translation.filter(language_code='es')

        if not article_translation.exists():
            continue

        activate('es')
        RealArticle.objects.translated(title=title).delete()


class Migration(migrations.Migration):

    dependencies = [
        ('gobcl_cms', '0013_auto_20180301_1812'),
    ]

    operations = [
        migrations.RunPython(delete_repeated_news),
    ]
