commit tx
[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 from suggest.models import PublishingSuggestion, Suggestion
13
14
15 class SuggestForm(forms.Form):
16     contact = forms.CharField(label=_('Contact'), max_length=120, required=False)
17     description = forms.CharField(label=_('Description'), widget=forms.Textarea, required=True)
18
19     def save(self, request):
20         contact = self.cleaned_data['contact']
21         description = self.cleaned_data['description']
22
23         suggestion = Suggestion(contact=contact,
24             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         if email_re.match(contact):
45             send_mail(u'[WolneLektury] ' + _(u'Thank you for your suggestion.'),
46                     _(u"""\
47 Thank you for your comment on WolneLektury.pl.
48 The suggestion has been referred to the project coordinator.""") +
49 u"""
50
51 -- 
52 """ + _(u'''Message sent automatically. Please do not reply.'''),
53                     'no-reply@wolnelektury.pl', [contact], fail_silently=True)
54
55
56 class PublishingSuggestForm(forms.Form):
57     contact = forms.CharField(label=_('Contact'), max_length=120, required=False)
58     books = forms.CharField(label=_('books'), widget=forms.Textarea, required=False)
59     audiobooks = forms.CharField(label=_('audiobooks'), widget=forms.Textarea, required=False)
60
61     def clean(self, *args, **kwargs):
62         if not self.cleaned_data['books'] and not self.cleaned_data['audiobooks']:
63             msg = _(u"One of these fields is required.")
64             self._errors["books"] = self.error_class([msg])
65             self._errors["audiobooks"] = self.error_class([msg])
66         return super(PublishingSuggestForm, self).clean(*args, **kwargs)
67
68     def save(self, request):
69         contact = self.cleaned_data['contact']
70         books = self.cleaned_data['books']
71         audiobooks = self.cleaned_data['audiobooks']
72
73         suggestion = PublishingSuggestion(contact=contact, books=books,
74             audiobooks=audiobooks, ip=request.META['REMOTE_ADDR'])
75         if request.user.is_authenticated():
76             suggestion.user = request.user
77         suggestion.save()
78
79         mail_managers(u'Konsultacja planu wydawniczego na WolneLektury.pl', u'''\
80 Zgłoszono nową sugestię nt. planu wydawniczego w serwisie WolneLektury.pl.
81 %(url)s
82
83 Użytkownik: %(user)s
84 Kontakt: %(contact)s
85
86 Książki:
87 %(books)s
88
89 Audiobooki:
90 %(audiobooks)s''' % {
91             'url': request.build_absolute_uri(reverse('admin:suggest_suggestion_change', args=[suggestion.id])),
92             'user': str(request.user) if request.user.is_authenticated() else '',
93             'contact': contact,
94             'books': books,
95             'audiobooks': audiobooks,
96             }, fail_silently=True)
97
98         if email_re.match(contact):
99             send_mail(u'[WolneLektury] ' +
100                     ugettext(u'Thank you for your suggestion.'),
101                     ugettext(u"""\
102 Thank you for your comment on WolneLektury.pl.
103 The suggestion has been referred to the project coordinator.""") +
104 u"""
105
106 -- 
107 """ + ugettext(u'''Message sent automatically. Please do not reply.'''),
108                     'no-reply@wolnelektury.pl', [contact], fail_silently=True)