1 # -*- coding: utf-8 -*-
2 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 from django import forms
6 from django.contrib.sites.models import Site
7 from django.core.exceptions import ValidationError
8 from django.core.mail import send_mail, mail_managers
9 from django.core.urlresolvers import reverse
10 from django.core.validators import validate_email
11 from django.utils.translation import ugettext_lazy as _
12 from django.utils.translation import ugettext
14 from newsletter.forms import NewsletterForm
15 from suggest.models import PublishingSuggestion, Suggestion
16 from wolnelektury.utils import send_noreply_mail
19 class SuggestForm(NewsletterForm):
20 email_field = 'contact'
21 contact = forms.CharField(label=_('Contact'), max_length=120, required=False)
22 description = forms.CharField(label=_('Description'), widget=forms.Textarea, required=True)
24 data_processing_part2 = u'''\
25 Dane są przetwarzane w zakresie niezbędnym do obsługi zgłoszenia. W przypadku wyrażenia dodatkowej zgody \
26 adres e-mail zostanie wykorzystany także w celu przesyłania newslettera Wolnych Lektur.'''
28 def save(self, request):
29 super(SuggestForm, self).save()
30 contact = self.cleaned_data['contact']
31 description = self.cleaned_data['description']
33 suggestion = Suggestion(contact=contact, description=description, ip=request.META['REMOTE_ADDR'])
34 if request.user.is_authenticated():
35 suggestion.user = request.user
38 mail_managers(u'Nowa sugestia na stronie WolneLektury.pl', u'''\
39 Zgłoszono nową sugestię w serwisie WolneLektury.pl.
40 http://%(site)s%(url)s
45 %(description)s''' % {
46 'site': Site.objects.get_current().domain,
47 'url': reverse('admin:suggest_suggestion_change', args=[suggestion.id]),
48 'user': str(request.user) if request.user.is_authenticated() else '',
50 'description': description,
51 }, fail_silently=True)
54 validate_email(contact)
55 except ValidationError:
59 ugettext(u'Thank you for your suggestion.'),
61 Thank you for your comment on WolneLektury.pl.
62 The suggestion has been referred to the project coordinator."""),
63 [contact], fail_silently=True)
66 class PublishingSuggestForm(NewsletterForm):
67 email_field = 'contact'
68 contact = forms.CharField(label=_('Contact'), max_length=120, required=False)
69 books = forms.CharField(label=_('books'), widget=forms.Textarea, required=True)
70 ebook = forms.BooleanField(label=_('ebook'), required=False, initial=True)
71 audiobook = forms.BooleanField(label=_('audiobook'), required=False)
73 data_processing_part2 = SuggestForm.data_processing_part2
76 if not self.cleaned_data['ebook'] and not self.cleaned_data['audiobook']:
77 msg = ugettext(u"One of these options is required.")
78 self._errors['ebook'] = self.error_class([msg])
79 self._errors['audiobook'] = self.error_class([msg])
80 return super(PublishingSuggestForm, self).clean()
82 def save(self, request):
83 super(PublishingSuggestForm, self).save()
84 contact = self.cleaned_data['contact']
85 suggestion_text = self.cleaned_data['books'].strip(', \n\r')
87 books = suggestion_text if self.cleaned_data['ebook'] else ''
88 audiobooks = suggestion_text if self.cleaned_data['audiobook'] else ''
90 suggestion = PublishingSuggestion(
91 contact=contact, books=books,
92 audiobooks=audiobooks, ip=request.META['REMOTE_ADDR'])
93 if request.user.is_authenticated():
94 suggestion.user = request.user
97 if not suggestion.is_spam():
98 mail_managers(u'Konsultacja planu wydawniczego na WolneLektury.pl', u'''\
99 Zgłoszono nową sugestię nt. planu wydawniczego w serwisie WolneLektury.pl.
109 %(audiobooks)s''' % {
110 'url': request.build_absolute_uri(reverse('admin:suggest_suggestion_change', args=[suggestion.id])),
111 'user': str(request.user) if request.user.is_authenticated() else '',
114 'audiobooks': audiobooks,
115 }, fail_silently=True)
118 validate_email(contact)
119 except ValidationError:
123 ugettext(u'Thank you for your suggestion.'),
125 Thank you for your comment on WolneLektury.pl.
126 The suggestion has been referred to the project coordinator."""),
127 [contact], fail_silently=True)