|
0
|
1 |
""" |
|
|
2 |
A few bits of helper functions for comment views. |
|
|
3 |
""" |
|
|
4 |
|
|
|
5 |
import urllib |
|
|
6 |
import textwrap |
|
|
7 |
from django.http import HttpResponseRedirect |
|
|
8 |
from django.core import urlresolvers |
|
|
9 |
from django.shortcuts import render_to_response |
|
|
10 |
from django.template import RequestContext |
|
|
11 |
from django.core.exceptions import ObjectDoesNotExist |
|
|
12 |
from django.contrib import comments |
|
|
13 |
|
|
|
14 |
def next_redirect(data, default, default_view, **get_kwargs): |
|
|
15 |
""" |
|
|
16 |
Handle the "where should I go next?" part of comment views. |
|
|
17 |
|
|
|
18 |
The next value could be a kwarg to the function (``default``), or a |
|
|
19 |
``?next=...`` GET arg, or the URL of a given view (``default_view``). See |
|
|
20 |
the view modules for examples. |
|
|
21 |
|
|
|
22 |
Returns an ``HttpResponseRedirect``. |
|
|
23 |
""" |
|
|
24 |
next = data.get("next", default) |
|
|
25 |
if next is None: |
|
|
26 |
next = urlresolvers.reverse(default_view) |
|
|
27 |
if get_kwargs: |
|
|
28 |
joiner = ('?' in next) and '&' or '?' |
|
|
29 |
next += joiner + urllib.urlencode(get_kwargs) |
|
|
30 |
return HttpResponseRedirect(next) |
|
|
31 |
|
|
|
32 |
def confirmation_view(template, doc="Display a confirmation view."): |
|
|
33 |
""" |
|
|
34 |
Confirmation view generator for the "comment was |
|
|
35 |
posted/flagged/deleted/approved" views. |
|
|
36 |
""" |
|
|
37 |
def confirmed(request): |
|
|
38 |
comment = None |
|
|
39 |
if 'c' in request.GET: |
|
|
40 |
try: |
|
|
41 |
comment = comments.get_model().objects.get(pk=request.GET['c']) |
|
29
|
42 |
except (ObjectDoesNotExist, ValueError): |
|
0
|
43 |
pass |
|
|
44 |
return render_to_response(template, |
|
|
45 |
{'comment': comment}, |
|
|
46 |
context_instance=RequestContext(request) |
|
|
47 |
) |
|
|
48 |
|
|
|
49 |
confirmed.__doc__ = textwrap.dedent("""\ |
|
|
50 |
%s |
|
|
51 |
|
|
|
52 |
Templates: `%s`` |
|
|
53 |
Context: |
|
|
54 |
comment |
|
|
55 |
The posted comment |
|
|
56 |
""" % (doc, template) |
|
|
57 |
) |
|
|
58 |
return confirmed |