bug report form
[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.http import HttpResponse, HttpResponseRedirect
6 from django.views.decorators import cache
7 from django.views.decorators.http import require_POST
8 from django.utils.translation import ugettext as _
9
10 from suggest import forms
11 from suggest.models import Suggestion
12
13 # FIXME - shouldn't be in catalogue
14 from catalogue.views import LazyEncoder 
15
16
17 #@require_POST
18 @cache.never_cache
19 def report(request):
20     suggest_form = forms.SuggestForm(request.POST)
21     if suggest_form.is_valid():
22         author = suggest_form.cleaned_data['author']
23         email = suggest_form.cleaned_data['email']
24         title = suggest_form.cleaned_data['title']
25         description = suggest_form.cleaned_data['description']
26
27         suggestion = Suggestion(author=author, email=email, title=title,
28             description=description, ip=request.META['REMOTE_ADDR'])
29         if request.user.is_authenticated():
30             suggestion.user = request.user
31         suggestion.save()
32
33         response_data = {'success': True, 'message': _('Report was sent successfully.')}
34     else:
35         response_data = {'success': False, 'errors': suggest_form.errors}
36     print LazyEncoder(ensure_ascii=False).encode(response_data)
37     return HttpResponse(LazyEncoder(ensure_ascii=False).encode(response_data))