# -*- coding: utf-8 -*- """Forms for emencia.django.newsletter""" from django import forms from django.utils.translation import ugettext_lazy as _ from emencia.django.newsletter.models import Contact, ContactSettings from emencia.django.newsletter.models import MailingList from theme.models import Theme from country.models import Country, Area class MailingListSubscriptionForm(forms.ModelForm): """Form for subscribing to a mailing list""" # Notes : This form will not check the uniquess of # the 'email' field, by defining it explictly and setting # it the Meta.exclude list, for allowing registration # to a mailing list even if the contact already exists. # Then the contact is always added to the subscribers field # of the mailing list because it will be cleaned with no # double. email = forms.EmailField(label=_('Email'), max_length=75) def save(self, mailing_list): data = self.cleaned_data contact, created = Contact.objects.get_or_create( email=data['email'], defaults={'first_name': data['first_name'], 'last_name': data['last_name']}) mailing_list.subscribers.add(contact) mailing_list.unsubscribers.remove(contact) class Meta: model = Contact fields = ('first_name', 'last_name') exclude = ('email',) class AllMailingListSubscriptionForm(MailingListSubscriptionForm): """Form for subscribing to all mailing list""" mailing_lists = forms.ModelMultipleChoiceField( queryset=MailingList.objects.all(), initial=[obj.id for obj in MailingList.objects.all()], label=_('Mailing lists'), widget=forms.CheckboxSelectMultiple()) def save(self, mailing_list): data = self.cleaned_data contact, created = Contact.objects.get_or_create( email=data['email'], defaults={'first_name': data['first_name'], 'last_name': data['last_name']}) for mailing_list in data['mailing_lists']: mailing_list.subscribers.add(contact) mailing_list.unsubscribers.remove(contact) class ContactForm(forms.ModelForm): email = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': _(u'Ваш e-mail')})) first_name = forms.CharField(required=True, widget=forms.TextInput(attrs={'placeholder': _(u'Ваше имя')})) class Meta: model = Contact fields = ('email', 'first_name', ) class ContactSettingsForm(forms.ModelForm): theme = forms.MultipleChoiceField(choices=[(str(item.id), item.name) for item in list(Theme.objects.language().all())], widget=forms.CheckboxSelectMultiple(attrs={'class': 'pr-checkbox'}), required=False) class Meta: model = ContactSettings fields = ('exponent_practicum', 'organiser_practicum', 'theme') def clean_theme(self): theme = self.cleaned_data.get('theme') if theme: return Theme.objects.filter(id__in=theme) else: return Theme.objects.none() class ContactFilterForm(forms.Form): email = forms.EmailField(label=_("Email"), max_length=255) theme = forms.ChoiceField(label=_("Тематика"), choices = [(t.id, t.name) for t in Theme.objects.language()]) country = forms.ChoiceField(label=_("Страна"), choices = [(c.id, c.name) for c in Country.objects.language().distinct()]) area = forms.ChoiceField(label=_("Страна"), choices = [(c.id, c.name) for c in Country.objects.language().distinct()])