piwik
[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 from django.contrib.sites.models import Site
13
14 from suggest import forms
15 from suggest.models import Suggestion
16
17 # FIXME - shouldn't be in catalogue
18 from catalogue.views import LazyEncoder
19
20
21 #@require_POST
22 @cache.never_cache
23 def report(request):
24     suggest_form = forms.SuggestForm(request.POST)
25     if suggest_form.is_valid():
26         contact = suggest_form.cleaned_data['contact']
27         description = suggest_form.cleaned_data['description']
28
29         suggestion = Suggestion(contact=contact,
30             description=description, ip=request.META['REMOTE_ADDR'])
31         if request.user.is_authenticated():
32             suggestion.user = request.user
33         suggestion.save()
34
35         mail_managers(u'Nowa sugestia na stronie WolneLektury.pl', u'''\
36 Zgłoszono nową sugestię w serwisie WolneLektury.pl.
37 http://%(site)s%(url)s
38
39 Użytkownik: %(user)s
40 Kontakt: %(contact)s
41
42 %(description)s''' % {
43             'site': Site.objects.get_current().domain,
44             'url': reverse('admin:suggest_suggestion_change', args=[suggestion.id]),
45             'user': str(request.user) if request.user.is_authenticated() else '',
46             'contact': contact,
47             'description': description,
48             }, fail_silently=True)
49
50         if email_re.match(contact):
51             send_mail(u'[WolneLektury] ' + _(u'Thank you for your suggestion.'),
52                     _(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 """ + _(u'''Message sent automatically. Please do not reply.'''),
59                     'no-reply@wolnelektury.pl', [contact], fail_silently=True)
60
61         response_data = {'success': True, 'message': _('Report was sent successfully.')}
62     else:
63         response_data = {'success': False, 'errors': suggest_form.errors}
64     return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))