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