# Path: wbc/process/models.py
# class Publication(Model):
#     process_step = models.ForeignKey('ProcessStep', related_name='publications', verbose_name="Verfahrensschritt")
#     description  = models.TextField(blank=True, verbose_name="Beschreibung")
#     place        = models.ForeignKey('Place', related_name='publications', verbose_name="Ort")
#     begin        = models.DateField(verbose_name="Beginn der Auslegungszeit")
#     end          = models.DateField(verbose_name="Ende der Auslegungszeit")
#     office       = models.TextField(blank=True, verbose_name="Auslegungsstelle")
#     office_hours = models.TextField(blank=True, verbose_name="Öffnungszeiten der Auslegungsstelle")
#     department   = models.ForeignKey(Department, verbose_name="Verantwortliche Behörde")
#     link         = models.URLField(blank=True)
# 
#     def get_absolute_url(self):
#         return reverse('place', kwargs={'pk': self.place.pk})
# 
#     def get_update_url(self):
#         return reverse('publication_update', kwargs={'pk': self.pk})
# 
#     def get_delete_url(self):
#         return reverse('publication_delete', kwargs={'pk': self.pk})
# 
#     def __str__(self):
#         return str(self.place) + ', ' + self.process_step.name
# 
#     def __unicode__(self):
#         return unicode(self.__str__())
# 
#     class Meta:
#         ordering            = ("-end",)
#         verbose_name        = u"Veröffentlichung"
#         verbose_name_plural = u"Veröffentlichungen"
# 
# Path: wbc/news/models.py
# class Subscriber(Model):
#     email = models.EmailField(unique=True)
#     entities = models.ManyToManyField(Entity, related_name='subscribers')
# 
#     def __unicode__(self):
#         return self.email
# 
#     class Meta:
#         verbose_name = "Abonnent"
#         verbose_name_plural = "Abonnenten"
# 
# class Newsletter(Model):
#     send = models.DateTimeField(verbose_name="Gesendet")
#     n = models.IntegerField(verbose_name="Anzahl Mails")
# 
#     def __unicode__(self):
#         return self.send.strftime("%d. %m. %Y, %H:%M:%S")
# 
#     class Meta:
#         verbose_name = "Newsletter"
#         verbose_name_plural = "Newsletter"
# 
# Path: wbc/news/lib.py
# def send_mail(to, template, context):
#     if type(to) != list:
#         to = [to,]
# 
#     t = loader.get_template(template)
#     c = Context(context)
# 
#     html_part = t.render(c)
#     text_part = strip_tags(html_part)
# 
#     subject,body = text_part.split('---')
# 
#     msg = EmailMultiAlternatives(subject.strip(),body.strip(),settings.EMAIL_FROM,to)
# 
#     return msg.send()


from django.conf import settings
from django.core.management.base import BaseCommand
from django.core.urlresolvers import reverse
from django.utils.timezone import now
from django.db.models import Max
from wbc.process.models import Publication
from wbc.news.models import Subscriber, Newsletter
from wbc.news.lib import send_mail
from django.utils.translation import activate
# -*- coding: utf-8 -*-
from __future__ import print_function




# temporary fix for Django 1.8
# http://stackoverflow.com/questions/29571606/django-not-able-to-render-context-when-in-shell

class Command(BaseCommand):
    help = u'Schickt die Newsletter-Mail für alle seit gestern eingetragenen Veröffentlichungen.'

    def handle(self, *args, **options):

        # temporary fix for Django 1.8
        activate('de')

        # get all publication since the last time a newsletter was send
        last = Newsletter.objects.all().aggregate(Max('send'))
        if last['send__max'] == None:
            # first newsletter ever, send all publications
            publications = Publication.objects.all()
        else:

# Path: wbc/process/models.py
# class Publication(Model):
#     process_step = models.ForeignKey('ProcessStep', related_name='publications', verbose_name="Verfahrensschritt")
#     description  = models.TextField(blank=True, verbose_name="Beschreibung")
#     place        = models.ForeignKey('Place', related_name='publications', verbose_name="Ort")
#     begin        = models.DateField(verbose_name="Beginn der Auslegungszeit")
#     end          = models.DateField(verbose_name="Ende der Auslegungszeit")
#     office       = models.TextField(blank=True, verbose_name="Auslegungsstelle")
#     office_hours = models.TextField(blank=True, verbose_name="Öffnungszeiten der Auslegungsstelle")
#     department   = models.ForeignKey(Department, verbose_name="Verantwortliche Behörde")
#     link         = models.URLField(blank=True)
# 
#     def get_absolute_url(self):
#         return reverse('place', kwargs={'pk': self.place.pk})
# 
#     def get_update_url(self):
#         return reverse('publication_update', kwargs={'pk': self.pk})
# 
#     def get_delete_url(self):
#         return reverse('publication_delete', kwargs={'pk': self.pk})
# 
#     def __str__(self):
#         return str(self.place) + ', ' + self.process_step.name
# 
#     def __unicode__(self):
#         return unicode(self.__str__())
# 
#     class Meta:
#         ordering            = ("-end",)
#         verbose_name        = u"Veröffentlichung"
#         verbose_name_plural = u"Veröffentlichungen"
# 
# Path: wbc/news/models.py
# class Subscriber(Model):
#     email = models.EmailField(unique=True)
#     entities = models.ManyToManyField(Entity, related_name='subscribers')
# 
#     def __unicode__(self):
#         return self.email
# 
#     class Meta:
#         verbose_name = "Abonnent"
#         verbose_name_plural = "Abonnenten"
# 
# class Newsletter(Model):
#     send = models.DateTimeField(verbose_name="Gesendet")
#     n = models.IntegerField(verbose_name="Anzahl Mails")
# 
#     def __unicode__(self):
#         return self.send.strftime("%d. %m. %Y, %H:%M:%S")
# 
#     class Meta:
#         verbose_name = "Newsletter"
#         verbose_name_plural = "Newsletter"
# 
# Path: wbc/news/lib.py
# def send_mail(to, template, context):
#     if type(to) != list:
#         to = [to,]
# 
#     t = loader.get_template(template)
#     c = Context(context)
# 
#     html_part = t.render(c)
#     text_part = strip_tags(html_part)
# 
#     subject,body = text_part.split('---')
# 
#     msg = EmailMultiAlternatives(subject.strip(),body.strip(),settings.EMAIL_FROM,to)
# 
#     return msg.send()


# Path: wbc/news/management/commands/daily-news.py
from django.conf import settings
from django.core.management.base import BaseCommand
from django.core.urlresolvers import reverse
from django.utils.timezone import now
from django.db.models import Max
from wbc.process.models import Publication
from wbc.news.models import Subscriber, Newsletter
from wbc.news.lib import send_mail
from django.utils.translation import activate

# -*- coding: utf-8 -*-
from __future__ import print_function




# temporary fix for Django 1.8
# http://stackoverflow.com/questions/29571606/django-not-able-to-render-context-when-in-shell

class Command(BaseCommand):
    help = u'Schickt die Newsletter-Mail für alle seit gestern eingetragenen Veröffentlichungen.'

    def handle(self, *args, **options):

        # temporary fix for Django 1.8
        activate('de')

        # get all publication since the last time a newsletter was send
        last = Newsletter.objects.all().aggregate(Max('send'))
        if last['send__max'] == None:
            # first newsletter ever, send all publications
            publications = Publication.objects.all()
        else:

            publications = Publication.objects.filter(created__range=[last['send__max'], now()]).all()