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