merge migrations,
[wolnelektury.git] / apps / 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.core.mail import send_mail, mail_managers
7 from django.core.urlresolvers import reverse
8 from django.core.validators import email_re
9 from django.utils.translation import ugettext_lazy as _
10 from django.utils.translation import ugettext
11
12
13 from suggest.models import PublishingSuggestion
14
15
16 class SuggestForm(forms.Form):
17     contact = forms.CharField(label=_('Contact'), max_length=120, required=False)
18     description = forms.CharField(label=_('Description'), widget=forms.Textarea, required=True)
19
20
21 class PublishingSuggestForm(forms.Form):
22     contact = forms.CharField(label=_('Contact'), max_length=120, required=False)
23     books = forms.CharField(label=_('books'), widget=forms.Textarea, required=False)
24     audiobooks = forms.CharField(label=_('audiobooks'), widget=forms.Textarea, required=False)
25
26     def clean(self, *args, **kwargs):
27         if not self.cleaned_data['books'] and not self.cleaned_data['audiobooks']:
28             msg = _(u"One of these fields is required.")
29             self._errors["books"] = self.error_class([msg])
30             self._errors["audiobooks"] = self.error_class([msg])
31         return super(PublishingSuggestForm, self).clean(*args, **kwargs)
32
33     def save(self, request):
34         contact = self.cleaned_data['contact']
35         books = self.cleaned_data['books']
36         audiobooks = self.cleaned_data['audiobooks']
37
38         suggestion = PublishingSuggestion(contact=contact, books=books,
39             audiobooks=audiobooks, ip=request.META['REMOTE_ADDR'])
40         if request.user.is_authenticated():
41             suggestion.user = request.user
42         suggestion.save()
43
44         mail_managers(u'Konsultacja planu wydawniczego na WolneLektury.pl', u'''\
45 Zgłoszono nową sugestię nt. planu wydawniczego w serwisie WolneLektury.pl.
46 %(url)s
47
48 Użytkownik: %(user)s
49 Kontakt: %(contact)s
50
51 Książki:
52 %(books)s
53
54 Audiobooki:
55 %(audiobooks)s''' % {
56             'url': request.build_absolute_uri(reverse('admin:suggest_suggestion_change', args=[suggestion.id])),
57             'user': str(request.user) if request.user.is_authenticated() else '',
58             'contact': contact,
59             'books': books,
60             'audiobooks': audiobooks,
61             }, fail_silently=True)
62
63         if email_re.match(contact):
64             send_mail(u'[WolneLektury] ' +
65                     ugettext(u'Thank you for your suggestion.'),
66                     ugettext(u"""\
67 Thank you for your comment on WolneLektury.pl.
68 The suggestion has been referred to the project coordinator.""") +
69 u"""
70
71 -- 
72 """ + ugettext(u'''Message sent automatically. Please do not reply.'''),
73                     'no-reply@wolnelektury.pl', [contact], fail_silently=True)