diff options
author | luxagraf <sng@luxagraf.net> | 2019-05-05 11:40:42 -0500 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2019-05-05 11:40:42 -0500 |
commit | a2891841fbf1e5660693a1f9109f2e6810224a3b (patch) | |
tree | be0aae73fe8f5e6271a76e8c5c6dc35b00f347a6 /app/django_comments/views/utils.py | |
parent | bdc06f56d538358f1fa07d35afb5733c790e3ab2 (diff) |
Diffstat (limited to 'app/django_comments/views/utils.py')
-rw-r--r-- | app/django_comments/views/utils.py | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/app/django_comments/views/utils.py b/app/django_comments/views/utils.py new file mode 100644 index 0000000..a5f5c11 --- /dev/null +++ b/app/django_comments/views/utils.py @@ -0,0 +1,71 @@ +""" +A few bits of helper functions for comment views. +""" + +import textwrap + +try: + from urllib.parse import urlencode +except ImportError: # Python 2 + from urllib import urlencode + +from django.http import HttpResponseRedirect +from django.shortcuts import render, resolve_url +from django.core.exceptions import ObjectDoesNotExist +from django.utils.http import is_safe_url + +import django_comments + + +def next_redirect(request, fallback, **get_kwargs): + """ + Handle the "where should I go next?" part of comment views. + + The next value could be a + ``?next=...`` GET arg or the URL of a given view (``fallback``). See + the view modules for examples. + + Returns an ``HttpResponseRedirect``. + """ + next = request.POST.get('next') + if not is_safe_url(url=next, allowed_hosts={request.get_host()}): + next = resolve_url(fallback) + + if get_kwargs: + if '#' in next: + tmp = next.rsplit('#', 1) + next = tmp[0] + anchor = '#' + tmp[1] + else: + anchor = '' + + joiner = ('?' in next) and '&' or '?' + next += joiner + urlencode(get_kwargs) + anchor + return HttpResponseRedirect(next) + + +def confirmation_view(template, doc="Display a confirmation view."): + """ + Confirmation view generator for the "comment was + posted/flagged/deleted/approved" views. + """ + + def confirmed(request): + comment = None + if 'c' in request.GET: + try: + comment = django_comments.get_model().objects.get(pk=request.GET['c']) + except (ObjectDoesNotExist, ValueError): + pass + return render(request, template, {'comment': comment}) + + confirmed.__doc__ = textwrap.dedent("""\ + %s + + Templates: :template:`%s`` + Context: + comment + The posted comment + """ % (doc, template) + ) + return confirmed |