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