# Path: query/tasks.py
# @shared_task
# def write_newspaper_classification(classification_json):
#     with open(os.path.join(settings.PROJECT_PARENT, 'newspapers.txt'), 'wb') as out:
#         out.write(classification_json)


import json
from collections import defaultdict
from django.db import models
from django.db.models import signals
from django.contrib.auth.models import User
from .tasks import write_newspaper_classification

class Pillar(models.Model):
    """Model that allows to store a categorization of Newspapers along Pillars."""
    name = models.CharField(max_length=200, unique=True)

    def __unicode__(self):
        return self.name


class Newspaper(models.Model):
    """Model that stores the available Newspapers"""
    id = models.CharField(max_length=9, primary_key=True)
    title = models.CharField(max_length=500)
    start_date = models.DateField()
    end_date = models.DateField()
    editions = models.PositiveIntegerField()
    pillar = models.ForeignKey(Pillar, null=True)

    def __unicode__(self):
        return self.title


def update_newspaper_classification(sender, instance, created, **kwargs):
    """Updates the newspaper classification to be used both in Django as well as Celery"""
    classification = defaultdict(list)
    for newspaper in Newspaper.objects.all():
        if newspaper.pillar:
            classification[newspaper.pillar.id].append(newspaper.pk)
    classification_json = json.dumps(classification)
    write_newspaper_classification(classification_json)

# Path: query/tasks.py
# @shared_task
# def write_newspaper_classification(classification_json):
#     with open(os.path.join(settings.PROJECT_PARENT, 'newspapers.txt'), 'wb') as out:
#         out.write(classification_json)


# Path: query/models.py
import json
from collections import defaultdict
from django.db import models
from django.db.models import signals
from django.contrib.auth.models import User
from .tasks import write_newspaper_classification


class Pillar(models.Model):
    """Model that allows to store a categorization of Newspapers along Pillars."""
    name = models.CharField(max_length=200, unique=True)

    def __unicode__(self):
        return self.name


class Newspaper(models.Model):
    """Model that stores the available Newspapers"""
    id = models.CharField(max_length=9, primary_key=True)
    title = models.CharField(max_length=500)
    start_date = models.DateField()
    end_date = models.DateField()
    editions = models.PositiveIntegerField()
    pillar = models.ForeignKey(Pillar, null=True)

    def __unicode__(self):
        return self.title


def update_newspaper_classification(sender, instance, created, **kwargs):
    """Updates the newspaper classification to be used both in Django as well as Celery"""
    classification = defaultdict(list)
    for newspaper in Newspaper.objects.all():
        if newspaper.pillar:
            classification[newspaper.pillar.id].append(newspaper.pk)
    classification_json = json.dumps(classification)
    write_newspaper_classification(classification_json)

    write_newspaper_classification.delay(classification_json)