fix newsletter field in registration form
[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     def save(self, request):
25         super(SuggestForm, self).save()
26         contact = self.cleaned_data['contact']
27         description = self.cleaned_data['description']
28
29         suggestion = Suggestion(contact=contact, description=description, ip=request.META['REMOTE_ADDR'])
30         if request.user.is_authenticated():
31             suggestion.user = request.user
32         suggestion.save()
33
34         mail_managers(u'Nowa sugestia na stronie WolneLektury.pl', u'''\
35 Zgłoszono nową sugestię w serwisie WolneLektury.pl.
36 http://%(site)s%(url)s
37
38 Użytkownik: %(user)s
39 Kontakt: %(contact)s
40
41 %(description)s''' % {
42             'site': Site.objects.get_current().domain,
43             'url': reverse('admin:suggest_suggestion_change', args=[suggestion.id]),
44             'user': str(request.user) if request.user.is_authenticated() else '',
45             'contact': contact,
46             'description': description,
47             }, fail_silently=True)
48
49         try:
50             validate_email(contact)
51         except ValidationError:
52             pass
53         else:
54             send_noreply_mail(
55                 ugettext(u'Thank you for your suggestion.'),
56                 ugettext(u"""\
57 Thank you for your comment on WolneLektury.pl.
58 The suggestion has been referred to the project coordinator."""),
59                 [contact], fail_silently=True)
60
61
62 class PublishingSuggestForm(NewsletterForm):
63     email_field = 'contact'
64     contact = forms.CharField(label=_('Contact'), max_length=120, required=False)
65     books = forms.CharField(label=_('books'), widget=forms.Textarea, required=True)
66     ebook = forms.BooleanField(label=_('ebook'), required=False, initial=True)
67     audiobook = forms.BooleanField(label=_('audiobook'), required=False)
68
69     def clean(self):
70         if not self.cleaned_data['ebook'] and not self.cleaned_data['audiobook']:
71             msg = ugettext(u"One of these options is required.")
72             self._errors['ebook'] = self.error_class([msg])
73             self._errors['audiobook'] = self.error_class([msg])
74         return super(PublishingSuggestForm, self).clean()
75
76     def save(self, request):
77         super(PublishingSuggestForm, self).save()
78         contact = self.cleaned_data['contact']
79         suggestion_text = self.cleaned_data['books'].strip(', \n\r')
80
81         books = suggestion_text if self.cleaned_data['ebook'] else ''
82         audiobooks = suggestion_text if self.cleaned_data['audiobook'] else ''
83
84         suggestion = PublishingSuggestion(
85             contact=contact, books=books,
86             audiobooks=audiobooks, ip=request.META['REMOTE_ADDR'])
87         if request.user.is_authenticated():
88             suggestion.user = request.user
89         suggestion.save()
90
91         if not suggestion.is_spam():
92             mail_managers(u'Konsultacja planu wydawniczego na WolneLektury.pl', u'''\
93     Zgłoszono nową sugestię nt. planu wydawniczego w serwisie WolneLektury.pl.
94     %(url)s
95
96     Użytkownik: %(user)s
97     Kontakt: %(contact)s
98
99     Książki:
100     %(books)s
101
102     Audiobooki:
103     %(audiobooks)s''' % {
104                 'url': request.build_absolute_uri(reverse('admin:suggest_suggestion_change', args=[suggestion.id])),
105                 'user': str(request.user) if request.user.is_authenticated() else '',
106                 'contact': contact,
107                 'books': books,
108                 'audiobooks': audiobooks,
109             }, fail_silently=True)
110
111             try:
112                 validate_email(contact)
113             except ValidationError:
114                 pass
115             else:
116                 send_noreply_mail(
117                     ugettext(u'Thank you for your suggestion.'),
118                     ugettext(u"""\
119 Thank you for your comment on WolneLektury.pl.
120 The suggestion has been referred to the project coordinator."""),
121                     [contact], fail_silently=True)