# Path: wbc/news/models.py
# class Validation(Model):
#     email = models.EmailField(unique=True)
#     entities = models.CharField(max_length=256, blank=True, null=True)
#     code = models.SlugField(max_length=32)
#     action = models.CharField(max_length=16)
# 
#     def save(self, *args, **kwargs):
#         c = string.ascii_lowercase + string.ascii_uppercase + string.digits
#         self.code = ''.join(random.sample(c*32, 32))
#         super(Validation, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return self.email
# 
#     class Meta:
#         verbose_name = "Validierung"
#         verbose_name_plural = "Validierung"
# 
# 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"
# 
# Path: wbc/news/models.py
# 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/region/models.py
# class Entity(Model):
#     name = models.CharField(max_length=256)
#     description = models.TextField(blank=True)
#     lat = models.FloatField(null=True, blank=True)
#     lon = models.FloatField(null=True, blank=True)
#     polygon = models.TextField(blank=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     class Meta:
#         verbose_name = 'Einheit'
#         verbose_name_plural = 'Einheiten'
# 
# class Muncipality(Entity):
# 
#     class Meta:
#         verbose_name = 'Gemeinde'
#         verbose_name_plural = 'Gemeinden'
# 
# class District(Entity):
#     muncipality = models.ForeignKey(Muncipality, related_name='districts')
# 
#     class Meta:
#         verbose_name = 'Bezirk'
#         verbose_name_plural = 'Bezirke'
# 
# Path: wbc/news/forms.py
# class SubscribeForm(forms.Form):
#     def __init__(self, *args, **kwargs):
#         if 'entities' in kwargs:
#             entities = kwargs.pop('entities')
# 
#         kwargs['label_suffix'] = ''
#         super(SubscribeForm, self).__init__(*args, **kwargs)
# 
#         for entity in entities:
#             field = forms.BooleanField(label=entity['name'],required=False);
#             self.fields[str(entity['id'])] = field
# 
#         self.fields['email'] = forms.EmailField(label='Email',
#                                                 widget=forms.TextInput(attrs={'class':'form-control'}))


from django.test import TestCase
from django.test import Client
from .models import Validation
from .models import Subscriber
from .models import Newsletter
from wbc.region.models import Entity, Muncipality, District
from .models import *
from .forms import SubscribeForm
from django.test.client import RequestFactory
from django.utils.timezone import now
from django.core.urlresolvers import reverse
        import json
    def test_validation_wrong_action(self):
        count = Subscriber.objects.all().count()
        self.assertEqual(count, 0)
        entity_json = self.getJson()
        validation = Validation.objects.create(email='hallo@example.com', action='wrong_action', entities=entity_json)
        validation.save()
        url = reverse('wbc.news.views.validate', args=[validation.code])
        try:
            Client().get(url)
        except:
            error_occured = True
        self.assertTrue(error_occured)

    def test_validation_does_not_exist(self):
        url = reverse('wbc.news.views.validate', args=['abc'])
        page = Client().get(url)
        self.assertEqual(page.status_code, 200)
        self.assertFalse('success' in page.context)
        self.assertEqual(Validation.objects.all().count(), 0)
        self.assertEqual(Subscriber.objects.all().count(), 0)


class NewsletterTestCase(TestCase):

    def create_newsletter(email='test@test.de'):
        n = Newsletter.objects.create(send=now(), n=5)
        return n

    def test_validation(self):
        n = self.create_newsletter()

# Path: wbc/news/models.py
# class Validation(Model):
#     email = models.EmailField(unique=True)
#     entities = models.CharField(max_length=256, blank=True, null=True)
#     code = models.SlugField(max_length=32)
#     action = models.CharField(max_length=16)
# 
#     def save(self, *args, **kwargs):
#         c = string.ascii_lowercase + string.ascii_uppercase + string.digits
#         self.code = ''.join(random.sample(c*32, 32))
#         super(Validation, self).save(*args, **kwargs)
# 
#     def __unicode__(self):
#         return self.email
# 
#     class Meta:
#         verbose_name = "Validierung"
#         verbose_name_plural = "Validierung"
# 
# 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"
# 
# Path: wbc/news/models.py
# 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/region/models.py
# class Entity(Model):
#     name = models.CharField(max_length=256)
#     description = models.TextField(blank=True)
#     lat = models.FloatField(null=True, blank=True)
#     lon = models.FloatField(null=True, blank=True)
#     polygon = models.TextField(blank=True)
# 
#     def __unicode__(self):
#         return self.name
# 
#     class Meta:
#         verbose_name = 'Einheit'
#         verbose_name_plural = 'Einheiten'
# 
# class Muncipality(Entity):
# 
#     class Meta:
#         verbose_name = 'Gemeinde'
#         verbose_name_plural = 'Gemeinden'
# 
# class District(Entity):
#     muncipality = models.ForeignKey(Muncipality, related_name='districts')
# 
#     class Meta:
#         verbose_name = 'Bezirk'
#         verbose_name_plural = 'Bezirke'
# 
# Path: wbc/news/forms.py
# class SubscribeForm(forms.Form):
#     def __init__(self, *args, **kwargs):
#         if 'entities' in kwargs:
#             entities = kwargs.pop('entities')
# 
#         kwargs['label_suffix'] = ''
#         super(SubscribeForm, self).__init__(*args, **kwargs)
# 
#         for entity in entities:
#             field = forms.BooleanField(label=entity['name'],required=False);
#             self.fields[str(entity['id'])] = field
# 
#         self.fields['email'] = forms.EmailField(label='Email',
#                                                 widget=forms.TextInput(attrs={'class':'form-control'}))


# Path: wbc/news/tests.py
from django.test import TestCase
from django.test import Client
from .models import Validation
from .models import Subscriber
from .models import Newsletter
from wbc.region.models import Entity, Muncipality, District
from .models import *
from .forms import SubscribeForm
from django.test.client import RequestFactory
from django.utils.timezone import now
from django.core.urlresolvers import reverse
        import json

    def test_validation_wrong_action(self):
        count = Subscriber.objects.all().count()
        self.assertEqual(count, 0)
        entity_json = self.getJson()
        validation = Validation.objects.create(email='hallo@example.com', action='wrong_action', entities=entity_json)
        validation.save()
        url = reverse('wbc.news.views.validate', args=[validation.code])
        try:
            Client().get(url)
        except:
            error_occured = True
        self.assertTrue(error_occured)

    def test_validation_does_not_exist(self):
        url = reverse('wbc.news.views.validate', args=['abc'])
        page = Client().get(url)
        self.assertEqual(page.status_code, 200)
        self.assertFalse('success' in page.context)
        self.assertEqual(Validation.objects.all().count(), 0)
        self.assertEqual(Subscriber.objects.all().count(), 0)


class NewsletterTestCase(TestCase):

    def create_newsletter(email='test@test.de'):
        n = Newsletter.objects.create(send=now(), n=5)
        return n

    def test_validation(self):
        n = self.create_newsletter()

        self.assertTrue(isinstance(n, Newsletter))