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.
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
16 from catalogue.forms import SearchForm
17 from suggest import forms
18 from suggest.models import Suggestion, PublishingSuggestion
21 # FIXME - shouldn't be in catalogue
22 from catalogue.views import LazyEncoder
25 class AjaxableFormView(object):
31 def __call__(self, request):
33 A view displaying a form, or JSON if `ajax' GET param is set.
35 ajax = request.GET.get('ajax', False)
36 if request.method == "POST":
37 form = self.formClass(request.POST)
40 response_data = {'success': True, 'message': _('Report was sent successfully.')}
42 response_data = {'success': False, 'errors': form.errors}
44 return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))
46 form = self.formClass()
49 template = self.ajax_template if ajax else self.template
50 return render_to_response(template, {
53 "response_data": response_data,
55 context_instance=RequestContext(request))
58 class PublishingSuggestionFormView(AjaxableFormView):
59 formClass = forms.PublishingSuggestForm
60 ajax_template = "publishing_suggest.html"
61 template = "publishing_suggest_full.html"
62 formname = "pubsuggest_form"
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']
73 suggestion = Suggestion(contact=contact,
74 description=description, ip=request.META['REMOTE_ADDR'])
75 if request.user.is_authenticated():
76 suggestion.user = request.user
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
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 '',
91 'description': description,
92 }, fail_silently=True)
94 if email_re.match(contact):
95 send_mail(u'[WolneLektury] ' + _(u'Thank you for your suggestion.'),
97 Thank you for your comment on WolneLektury.pl.
98 The suggestion has been referred to the project coordinator.""") +
102 """ + _(u'''Message sent automatically. Please do not reply.'''),
103 'no-reply@wolnelektury.pl', [contact], fail_silently=True)
105 response_data = {'success': True, 'message': _('Report was sent successfully.')}
107 response_data = {'success': False, 'errors': suggest_form.errors}
108 return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))