#1223: notifications for suggestions
[wolnelektury.git] / apps / suggest / views.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.core.mail import send_mail, mail_managers
6 from django.core.urlresolvers import reverse
7 from django.core.validators import email_re
8 from django.http import HttpResponse, HttpResponseRedirect
9 from django.utils.translation import ugettext as _
10 from django.views.decorators import cache
11 from django.views.decorators.http import require_POST
12
13 from suggest import forms
14 from suggest.models import Suggestion
15
16 # FIXME - shouldn't be in catalogue
17 from catalogue.views import LazyEncoder
18
19
20 #@require_POST
21 @cache.never_cache
22 def report(request):
23     suggest_form = forms.SuggestForm(request.POST)
24     if suggest_form.is_valid():
25         contact = suggest_form.cleaned_data['contact']
26         description = suggest_form.cleaned_data['description']
27
28         suggestion = Suggestion(contact=contact,
29             description=description, ip=request.META['REMOTE_ADDR'])
30         if request.user.is_authenticated():
31             suggestion.user = request.user
32         suggestion.save()
33
34         mail_managers(u'Nowa sugestia na stronie WolneLektury.pl', u'''\
35 Zgłoszono nową sugestię w serwisie WolneLektury.pl.
36 %(url)s
37
38 Użytkownik: %(user)s
39 Kontakt: %(contact)s
40
41 %(description)s''' % {
42             'url': reverse('admin:suggest_suggestion_change', args=[suggestion.id]),
43             'user': str(request.user),
44             'contact': contact,
45             'description': description,
46             }, fail_silently=True)
47
48         if email_re.match(contact):
49             send_mail(u'[WolneLektury] ' + _(u'Thank you for your suggestion.'),
50                     _(u"""\
51 Thank you for your comment on WolneLektury.pl.
52 The suggestion has been referred to the project coordinator.""") +
53 u"""
54
55 -- 
56 """ + _(u'''Message sent automatically. Please do not reply.'''),
57                     'no-reply@wolnelektury.pl', [contact], fail_silently=True)
58
59         response_data = {'success': True, 'message': _('Report was sent successfully.')}
60     else:
61         response_data = {'success': False, 'errors': suggest_form.errors}
62     return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))