Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

151 lines (116 sloc) 4.943 kb
from functools import wraps
from django import http
from django.conf import settings
from django.shortcuts import get_object_or_404, render
from django.views.decorators.cache import never_cache
import jingo
from product_details.version_compare import Version
from session_csrf import anonymous_csrf_exempt
from tower import ugettext as _
import input
from input.decorators import cache_page, forward_mobile
from input.urlresolvers import reverse
from feedback.forms import PraiseForm, IssueForm, IdeaForm
from feedback.models import Opinion
from feedback.utils import detect_language, ua_parse
def enforce_ua(f):
View decorator enforcing feedback from the right (latest beta, latest
release) users only.
Can be disabled with settings.ENFORCE_USER_AGENT = False.
def wrapped(request, *args, **kwargs):
# Validate User-Agent request header.
ua = request.META.get('HTTP_USER_AGENT', None)
parsed = ua_parse(ua)
if not parsed: # Unknown UA.
if request.method == 'GET':
return http.HttpResponseRedirect(
return http.HttpResponseBadRequest(
_('User-Agent request header must be set.'))
if not settings.ENFORCE_USER_AGENT:
return f(request, ua=ua, *args, **kwargs)
this_ver = Version(parsed['version'])
ref_ver = Version(parsed['browser'].min_version)
# Check for outdated release.
if this_ver < ref_ver:
return http.HttpResponseRedirect(reverse(''))
# If we made it here, it's a valid version.
return f(request, ua=ua, *args, **kwargs)
return wrapped
def feedback(request, ua):
"""Page to receive feedback under happy/sad/idea categories"""
initial = dict(url=request.REQUEST.get('url'))
if request.method == 'POST':
typ = int(request.POST.get('_type'))
if typ ==
form = PraiseForm(request.POST, auto_id='happy-%s')
elif typ ==
form = IssueForm(request.POST, auto_id='sad-%s')
form = IdeaForm(request.POST, auto_id='idea-%s')
if form.is_valid():
request.session['previous_opinion'] = save_opinion_from_form(
request, typ, ua, form)
url = reverse('thanks')
return http.HttpResponseRedirect(url)
forms = {'happy': (form if typ == else
PraiseForm(auto_id='happy-%s', initial=initial)),
'sad': (form if typ == else
IssueForm(auto_id='sad-%s', initial=initial)),
'idea': (form if typ == else
forms = {'happy': PraiseForm(auto_id='happy-%s', initial=initial),
'sad': IssueForm(auto_id='sad-%s', initial=initial),
'idea': IdeaForm(auto_id='idea-%s')}
template = 'feedback/%sindex.html' % (
'mobile/' if request.mobile_site else '')
return render(request, template, {'forms': forms})
def download(request):
"""Encourage people to download a current version."""
template = 'feedback/%sdownload.html' % (
'mobile/' if request.mobile_site else '')
return jingo.render(request, template)
def thanks(request):
"""Thank you for your feedback."""
previous_opinion = request.session['previous_opinion']
except KeyError:
previous_opinion = None
template = 'feedback/%sthanks.html' % (
'mobile/' if request.mobile_site else '')
return jingo.render(request, template, {'opinion': previous_opinion})
def opinion_detail(request, id):
o = get_object_or_404(Opinion, pk=id, _type__in=input.OPINION_TYPES.keys())
if not request.mobile_site:
template = 'feedback/opinion.html'
template = 'feedback/mobile/opinion.html'
return jingo.render(request, template, {'opinion': o})
def save_opinion_from_form(request, type, ua, form):
"""Given a (valid) form and feedback type, save it to the DB."""
locale = detect_language(request)
if type not in input.OPINION_TYPES:
raise ValueError('Unknown type %s' % type)
opinion = Opinion(
url=form.cleaned_data.get('url', ''),
user_agent=ua, locale=locale,
return opinion
Jump to Line
Something went wrong with that request. Please try again.