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