/feincms/views/cbv/views.py

http://github.com/feincms/feincms · Python · 96 lines · 58 code · 20 blank · 18 comment · 16 complexity · 2f3f8c52cdcb5ca9c3e8194fac2db196 MD5 · raw file

  1. from django.http import Http404
  2. from django.utils.cache import add_never_cache_headers
  3. from django.views.generic import TemplateView
  4. from feincms import settings
  5. from feincms.module.page.models import Page
  6. class Handler(TemplateView):
  7. """
  8. Class-based handler for FeinCMS page content
  9. """
  10. def get(self, request, *args, **kwargs):
  11. return self.handler(request, *args, **kwargs)
  12. def post(self, request, *args, **kwargs):
  13. return self.handler(request, *args, **kwargs)
  14. def handler(self, request, path=None, *args, **kwargs):
  15. self.page = Page.objects.for_request(request, raise404=True, best_match=True, setup=False)
  16. response = self.prepare()
  17. if response:
  18. return response
  19. response = self.render_to_response(self.get_context_data())
  20. return self.finalize(response)
  21. def get_template_names(self):
  22. if self.template_name is None:
  23. return [self.page.template.path]
  24. return [self.template_name]
  25. def get_context_data(self, **kwargs):
  26. context = self.request._feincms_extra_context
  27. context['feincms_page'] = self.page
  28. return context
  29. def prepare(self):
  30. """
  31. Prepare / pre-process content types. If this method returns anything,
  32. it is treated as a ``HttpResponse`` and handed back to the visitor.
  33. """
  34. response = self.page.setup_request(self.request)
  35. if response:
  36. return response
  37. http404 = None # store eventual Http404 exceptions for re-raising,
  38. # if no content type wants to handle the current self.request
  39. successful = False # did any content type successfully end processing?
  40. for content in self.page.content.all_of_type(tuple(self.page._feincms_content_types_with_process)):
  41. try:
  42. r = content.process(self.request, view=self)
  43. if r in (True, False):
  44. successful = r
  45. elif r:
  46. return r
  47. except Http404, e:
  48. http404 = e
  49. if not successful:
  50. if http404:
  51. # re-raise stored Http404 exception
  52. raise http404
  53. if not settings.FEINCMS_ALLOW_EXTRA_PATH and \
  54. self.request._feincms_extra_context['extra_path'] != '/':
  55. raise Http404
  56. def finalize(self, response):
  57. """
  58. Runs finalize() on content types having such a method, adds headers and
  59. returns the final response.
  60. """
  61. for content in self.page.content.all_of_type(tuple(self.page._feincms_content_types_with_finalize)):
  62. r = content.finalize(self.request, response)
  63. if r:
  64. return r
  65. self.page.finalize_response(self.request, response)
  66. # Add never cache headers in case frontend editing is active
  67. if hasattr(self.request, "session") and self.request.session.get('frontend_editing', False):
  68. add_never_cache_headers(response)
  69. return response
  70. @property
  71. def __name__(self):
  72. """
  73. Dummy property to make this handler behave like a normal function.
  74. This property is used by django-debug-toolbar
  75. """
  76. return self.__class__.__name__