counters stub
[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 from django.shortcuts import render_to_response
14 from django.template import RequestContext
15
16 from catalogue.forms import SearchForm
17 from suggest import forms
18 from suggest.models import Suggestion, PublishingSuggestion
19
20
21 # FIXME - shouldn't be in catalogue
22 from catalogue.views import LazyEncoder
23
24
25 class AjaxableFormView(object):
26     formClass = None
27     template = None
28     ajax_template = None
29     formname = None
30
31     def __call__(self, request):
32         """
33             A view displaying a form, or JSON if `ajax' GET param is set.
34         """
35         ajax = request.GET.get('ajax', False)
36         if request.method == "POST":
37             form = self.formClass(request.POST)
38             if form.is_valid():
39                 form.save(request)
40                 response_data = {'success': True, 'message': _('Report was sent successfully.')}
41             else:
42                 response_data = {'success': False, 'errors': form.errors}
43             if ajax:
44                 return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
45         else:
46             form = self.formClass()
47             response_data = None
48
49         template = self.ajax_template if ajax else self.template
50         return render_to_response(template, {
51                 self.formname: form, 
52                 "form": SearchForm(),
53                 "response_data": response_data,
54             },
55             context_instance=RequestContext(request))
56
57
58 class PublishingSuggestionFormView(AjaxableFormView):
59     formClass = forms.PublishingSuggestForm
60     ajax_template = "publishing_suggest.html"
61     template = "publishing_suggest_full.html"
62     formname = "pubsuggest_form"
63
64
65 @require_POST
66 @cache.never_cache
67 def report(request):
68     suggest_form = forms.SuggestForm(request.POST)
69     if suggest_form.is_valid():
70         contact = suggest_form.cleaned_data['contact']
71         description = suggest_form.cleaned_data['description']
72
73         suggestion = Suggestion(contact=contact,
74             description=description, ip=request.META['REMOTE_ADDR'])
75         if request.user.is_authenticated():
76             suggestion.user = request.user
77         suggestion.save()
78
79         mail_managers(u'Nowa sugestia na stronie WolneLektury.pl', u'''\
80 Zgłoszono nową sugestię w serwisie WolneLektury.pl.
81 http://%(site)s%(url)s
82
83 Użytkownik: %(user)s
84 Kontakt: %(contact)s
85
86 %(description)s''' % {
87             'site': Site.objects.get_current().domain,
88             'url': reverse('admin:suggest_suggestion_change', args=[suggestion.id]),
89             'user': str(request.user) if request.user.is_authenticated() else '',
90             'contact': contact,
91             'description': description,
92             }, fail_silently=True)
93
94         if email_re.match(contact):
95             send_mail(u'[WolneLektury] ' + _(u'Thank you for your suggestion.'),
96                     _(u"""\
97 Thank you for your comment on WolneLektury.pl.
98 The suggestion has been referred to the project coordinator.""") +
99 u"""
100
101 -- 
102 """ + _(u'''Message sent automatically. Please do not reply.'''),
103                     'no-reply@wolnelektury.pl', [contact], fail_silently=True)
104
105         response_data = {'success': True, 'message': _('Report was sent successfully.')}
106     else:
107         response_data = {'success': False, 'errors': suggest_form.errors}
108     return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))