Fix for carousel management.
[wolnelektury.git] / src / suggest / forms.py
1 # This file is part of Wolnelektury, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
3 #
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 gettext_lazy as _
11 from django.utils.translation import gettext
12
13 from newsletter.forms import NewsletterForm
14 from suggest.models import PublishingSuggestion, Suggestion
15 from wolnelektury.utils import send_noreply_mail
16
17
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)
22
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.'''
26
27     def save(self, request):
28         super(SuggestForm, self).save()
29         contact = self.cleaned_data['contact']
30         description = self.cleaned_data['description']
31
32         suggestion = Suggestion(contact=contact, description=description, ip=request.META['REMOTE_ADDR'])
33         if request.user.is_authenticated:
34             suggestion.user = request.user
35         suggestion.save()
36
37         mail_managers('Nowa sugestia na stronie WolneLektury.pl', '''\
38 Zgłoszono nową sugestię w serwisie WolneLektury.pl.
39 http://%(site)s%(url)s
40
41 Użytkownik: %(user)s
42 Kontakt: %(contact)s
43
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 '',
48             'contact': contact,
49             'description': description,
50             }, fail_silently=True)
51
52         try:
53             validate_email(contact)
54         except ValidationError:
55             pass
56         else:
57             send_noreply_mail(
58                 gettext('Thank you for your suggestion.'),
59                 gettext("""\
60 Thank you for your comment on WolneLektury.pl.
61 The suggestion has been referred to the project coordinator."""),
62                 [contact], fail_silently=True)
63
64
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)
71
72     data_processing_part2 = SuggestForm.data_processing_part2
73
74     def clean(self):
75         if not self.cleaned_data['ebook'] and not self.cleaned_data['audiobook']:
76             msg = gettext("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()
80
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')
85
86         books = suggestion_text if self.cleaned_data['ebook'] else ''
87         audiobooks = suggestion_text if self.cleaned_data['audiobook'] else ''
88
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
94         suggestion.save()
95
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.
99     %(url)s
100
101     Użytkownik: %(user)s
102     Kontakt: %(contact)s
103
104     Książki:
105     %(books)s
106
107     Audiobooki:
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 '',
111                 'contact': contact,
112                 'books': books,
113                 'audiobooks': audiobooks,
114             }, fail_silently=True)
115
116             try:
117                 validate_email(contact)
118             except ValidationError:
119                 pass
120             else:
121                 send_noreply_mail(
122                     gettext('Thank you for your suggestion.'),
123                     gettext("""\
124 Thank you for your comment on WolneLektury.pl.
125 The suggestion has been referred to the project coordinator."""),
126                     [contact], fail_silently=True)