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