PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/django/contrib/comments/views/utils.py

https://code.google.com/p/mango-py/
Python | 65 lines | 59 code | 2 blank | 4 comment | 0 complexity | 833a945ac9921c131a0a5739666db29c MD5 | raw file
 1"""
 2A few bits of helper functions for comment views.
 3"""
 4
 5import urllib
 6import textwrap
 7from django.http import HttpResponseRedirect
 8from django.core import urlresolvers
 9from django.shortcuts import render_to_response
10from django.template import RequestContext
11from django.core.exceptions import ObjectDoesNotExist
12from django.contrib import comments
13
14def 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        if '#' in next:
29            tmp = next.rsplit('#', 1)
30            next = tmp[0]
31            anchor = '#' + tmp[1]
32        else:
33            anchor = ''
34
35        joiner = ('?' in next) and '&' or '?'
36        next += joiner + urllib.urlencode(get_kwargs) + anchor
37    return HttpResponseRedirect(next)
38
39def confirmation_view(template, doc="Display a confirmation view."):
40    """
41    Confirmation view generator for the "comment was
42    posted/flagged/deleted/approved" views.
43    """
44    def confirmed(request):
45        comment = None
46        if 'c' in request.GET:
47            try:
48                comment = comments.get_model().objects.get(pk=request.GET['c'])
49            except (ObjectDoesNotExist, ValueError):
50                pass
51        return render_to_response(template,
52            {'comment': comment},
53            context_instance=RequestContext(request)
54        )
55
56    confirmed.__doc__ = textwrap.dedent("""\
57        %s
58
59        Templates: `%s``
60        Context:
61            comment
62                The posted comment
63        """ % (doc, template)
64    )
65    return confirmed