PageRenderTime 27ms CodeModel.GetById 16ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/feincms/views/cbv/views.py

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