2008-08-26 06:14:22 +08:00
|
|
|
"""
|
|
|
|
A few bits of helper functions for comment views.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import textwrap
|
2012-07-20 21:36:52 +08:00
|
|
|
try:
|
|
|
|
from urllib.parse import urlencode
|
|
|
|
except ImportError: # Python 2
|
|
|
|
from urllib import urlencode
|
|
|
|
|
2008-08-26 06:14:22 +08:00
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.core import urlresolvers
|
|
|
|
from django.shortcuts import render_to_response
|
|
|
|
from django.template import RequestContext
|
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
|
from django.contrib import comments
|
|
|
|
|
|
|
|
def next_redirect(data, default, default_view, **get_kwargs):
|
|
|
|
"""
|
|
|
|
Handle the "where should I go next?" part of comment views.
|
|
|
|
|
|
|
|
The next value could be a kwarg to the function (``default``), or a
|
|
|
|
``?next=...`` GET arg, or the URL of a given view (``default_view``). See
|
|
|
|
the view modules for examples.
|
|
|
|
|
|
|
|
Returns an ``HttpResponseRedirect``.
|
|
|
|
"""
|
|
|
|
next = data.get("next", default)
|
|
|
|
if next is None:
|
|
|
|
next = urlresolvers.reverse(default_view)
|
|
|
|
if get_kwargs:
|
2011-03-03 21:03:53 +08:00
|
|
|
if '#' in next:
|
|
|
|
tmp = next.rsplit('#', 1)
|
|
|
|
next = tmp[0]
|
|
|
|
anchor = '#' + tmp[1]
|
|
|
|
else:
|
|
|
|
anchor = ''
|
|
|
|
|
2009-04-08 03:49:07 +08:00
|
|
|
joiner = ('?' in next) and '&' or '?'
|
2012-07-20 21:36:52 +08:00
|
|
|
next += joiner + urlencode(get_kwargs) + anchor
|
2008-08-26 06:14:22 +08:00
|
|
|
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 = comments.get_model().objects.get(pk=request.GET['c'])
|
2010-03-06 04:17:52 +08:00
|
|
|
except (ObjectDoesNotExist, ValueError):
|
2008-08-26 06:14:22 +08:00
|
|
|
pass
|
|
|
|
return render_to_response(template,
|
|
|
|
{'comment': comment},
|
|
|
|
context_instance=RequestContext(request)
|
|
|
|
)
|
|
|
|
|
|
|
|
confirmed.__doc__ = textwrap.dedent("""\
|
|
|
|
%s
|
|
|
|
|
2012-06-27 09:17:15 +08:00
|
|
|
Templates: :template:`%s``
|
2008-08-26 06:14:22 +08:00
|
|
|
Context:
|
|
|
|
comment
|
|
|
|
The posted comment
|
2008-08-28 06:55:10 +08:00
|
|
|
""" % (doc, template)
|
2008-08-26 06:14:22 +08:00
|
|
|
)
|
|
|
|
return confirmed
|