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