# Path: wbc/region/models.py
# 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'}))
# 
# class UnsubscribeForm(forms.Form):
#     email = forms.EmailField(label='Email-Adresse',
#                              widget=forms.TextInput(attrs={'class':'form-control'}))
# 
# 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"
# 
# 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/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 json import loads, dumps
from django.conf import settings
from django.shortcuts import render
from django.core.urlresolvers import reverse
from wbc.region.models import District
from .forms import SubscribeForm, UnsubscribeForm
from .models import Validation, Subscriber
from .lib import send_mail
            })
    else:
        form = SubscribeForm(entities=entities)

    return render(request, 'news/subscribe.html', {
        'form': form,
        'unsubscribe_link': settings.SITE_URL + unsubscribe_path,
        'entities': entities
    })


def unsubscribe(request, email=None):
    if request.method == 'POST':
        form = UnsubscribeForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data.pop('email')
            try:
                subscriber = Subscriber.objects.get(email=email)

                try:
                    v = Validation.objects.get(email=email)
                except Validation.DoesNotExist:
                    v = Validation(email=email)

                v.action = 'unsubscribe'
                v.save()

                validate_path = reverse(
                    'wbc.news.views.validate', args=['.']).strip('.')


# Path: wbc/region/models.py
# 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'}))
# 
# class UnsubscribeForm(forms.Form):
#     email = forms.EmailField(label='Email-Adresse',
#                              widget=forms.TextInput(attrs={'class':'form-control'}))
# 
# 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"
# 
# 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/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/views.py
from json import loads, dumps
from django.conf import settings
from django.shortcuts import render
from django.core.urlresolvers import reverse
from wbc.region.models import District
from .forms import SubscribeForm, UnsubscribeForm
from .models import Validation, Subscriber
from .lib import send_mail

            })
    else:
        form = SubscribeForm(entities=entities)

    return render(request, 'news/subscribe.html', {
        'form': form,
        'unsubscribe_link': settings.SITE_URL + unsubscribe_path,
        'entities': entities
    })


def unsubscribe(request, email=None):
    if request.method == 'POST':
        form = UnsubscribeForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data.pop('email')
            try:
                subscriber = Subscriber.objects.get(email=email)

                try:
                    v = Validation.objects.get(email=email)
                except Validation.DoesNotExist:
                    v = Validation(email=email)

                v.action = 'unsubscribe'
                v.save()

                validate_path = reverse(
                    'wbc.news.views.validate', args=['.']).strip('.')


                send_mail(email, 'news/mail/unsubscribe.html', {