Django 1.9.
[wolnelektury.git] / src / suggest / forms.py
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.
4 #
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
13
14 from newsletter.forms import NewsletterForm
15 from suggest.models import PublishingSuggestion, Suggestion
16 from wolnelektury.utils import send_noreply_mail
17
18
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)
23
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.'''
27
28     def save(self, request):
29         super(SuggestForm, self).save()
30         contact = self.cleaned_data['contact']
31         description = self.cleaned_data['description']
32
33         suggestion = Suggestion(contact=contact, description=description, ip=request.META['REMOTE_ADDR'])
34         if request.user.is_authenticated():
35             suggestion.user = request.user
36         suggestion.save()
37
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
41
42 Użytkownik: %(user)s
43 Kontakt: %(contact)s
44
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 '',
49             'contact': contact,
50             'description': description,
51             }, fail_silently=True)
52
53         try:
54             validate_email(contact)
55         except ValidationError:
56             pass
57         else:
58             send_noreply_mail(
59                 ugettext(u'Thank you for your suggestion.'),
60                 ugettext(u"""\
61 Thank you for your comment on WolneLektury.pl.
62 The suggestion has been referred to the project coordinator."""),
63                 [contact], fail_silently=True)
64
65
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)
72
73     data_processing_part2 = SuggestForm.data_processing_part2
74
75     def clean(self):
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()
81
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')
86
87         books = suggestion_text if self.cleaned_data['ebook'] else ''
88         audiobooks = suggestion_text if self.cleaned_data['audiobook'] else ''
89
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
95         suggestion.save()
96
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.
100     %(url)s
101
102     Użytkownik: %(user)s
103     Kontakt: %(contact)s
104
105     Książki:
106     %(books)s
107
108     Audiobooki:
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 '',
112                 'contact': contact,
113                 'books': books,
114                 'audiobooks': audiobooks,
115             }, fail_silently=True)
116
117             try:
118                 validate_email(contact)
119             except ValidationError:
120                 pass
121             else:
122                 send_noreply_mail(
123                     ugettext(u'Thank you for your suggestion.'),
124                     ugettext(u"""\
125 Thank you for your comment on WolneLektury.pl.
126 The suggestion has been referred to the project coordinator."""),
127                     [contact], fail_silently=True)