You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
4.3 KiB
106 lines
4.3 KiB
# -*- coding: utf-8 -*-
|
|
from django import forms
|
|
from django.utils.translation import ugettext_lazy as _
|
|
from django.utils import translation
|
|
from ckeditor.widgets import CKEditorWidget
|
|
from emencia.django.newsletter.models import Contact, ContactSettings, MailingList, Newsletter, Attachment
|
|
from city.models import City
|
|
from country.models import Country, Area
|
|
|
|
|
|
class ContactSettingsForm(forms.ModelForm):
|
|
city = forms.CharField(label=u'Город', widget=forms.HiddenInput() ,required=False)
|
|
periodic = forms.ChoiceField(choices=ContactSettings.PERIODIC_CHOICES,
|
|
label=_(u'Периодичность отправки'))
|
|
first_name = forms.CharField(label=_('first name'))
|
|
subscriber = forms.BooleanField(label=_('subscriber'), required=False)
|
|
valid = forms.BooleanField(label=_('valid email'), required=False)
|
|
tester = forms.BooleanField(label=_('contact tester'), required=False)
|
|
country = forms.MultipleChoiceField(choices=[(c.id,c.name) for c in list(set(Country.objects.language().all()))], required=False)
|
|
area = forms.MultipleChoiceField(choices=[(a.id, a.name) for a in list(set(Area.objects.language().all()))], required=False)
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super(ContactSettingsForm, self).__init__(*args, **kwargs)
|
|
instance = getattr(self, 'instance', None)
|
|
if instance:
|
|
lang = translation.get_language()
|
|
self.fields['city'].widget.attrs['data-init-text'] = [(item.id, item.name) for item in list(instance.city.filter(translations__language_code=lang))]
|
|
|
|
class Meta:
|
|
model = ContactSettings
|
|
fields = ('periodic', 'exponent_practicum', 'organiser_practicum', 'theme', 'area', 'country', 'city', )
|
|
|
|
def save(self, commit=True):
|
|
contactsettings = super(ContactSettingsForm, self).save(commit=False)
|
|
old_save_m2m = self.save_m2m
|
|
if commit:
|
|
contactsettings.save()
|
|
contact = contactsettings.contact
|
|
contact.first_name = self.cleaned_data['first_name']
|
|
contact.subscriber = self.cleaned_data['subscriber']
|
|
contact.valid = self.cleaned_data['valid']
|
|
contact.tester = self.cleaned_data['tester']
|
|
contact.save()
|
|
return contactsettings
|
|
|
|
def clean_periodic(self):
|
|
|
|
return int(self.cleaned_data['periodic'])
|
|
|
|
def clean_city(self):
|
|
cities = self.cleaned_data.get('city')
|
|
if cities:
|
|
res = []
|
|
for id in cities.split(','):
|
|
try:
|
|
res.append(int(id))
|
|
except ValueError:
|
|
continue
|
|
return City.objects.filter(id__in=res)
|
|
else:
|
|
return City.objects.none()
|
|
|
|
|
|
import xlrd
|
|
|
|
|
|
class MailingListForm(forms.ModelForm):
|
|
excel_file = forms.FileField(label='Импортировать подписчиков', required=False)
|
|
|
|
class Meta:
|
|
model = MailingList
|
|
fields = ('name', 'description')
|
|
|
|
def save(self, commit=True):
|
|
ml = super(MailingListForm, self).save(commit=True)
|
|
data = self.cleaned_data
|
|
f = data['excel_file']
|
|
if f:
|
|
contact_list = list(Contact.objects.all())
|
|
book = xlrd.open_workbook(file_contents=f.read())
|
|
sheet = book.sheet_by_index(0)
|
|
excel_emails = [sheet.row_values(row_number)[0] for row_number in range(1, sheet.nrows)]
|
|
valid_contacts = []
|
|
for contact in contact_list:
|
|
if contact.email in excel_emails:
|
|
valid_contacts.append(contact)
|
|
ml.subscribers = valid_contacts
|
|
ml.save()
|
|
return ml
|
|
|
|
|
|
class NewsletterForm(forms.ModelForm):
|
|
test_contacts = forms.ModelMultipleChoiceField(label=u'Тестовые контакты', required=False,
|
|
queryset=Contact.objects.filter(tester=True))
|
|
content = forms.CharField(label=_('content'), widget=CKEditorWidget(config_name='newsletters'))
|
|
|
|
class Meta:
|
|
model = Newsletter
|
|
fields = ('title', 'content', 'mailing_list', 'test_contacts', 'header_sender',
|
|
'header_reply', 'status', 'sending_date', 'slug')
|
|
|
|
|
|
class AttachmentForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Attachment
|
|
fields = ('title', 'file_attachment') |