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

/feincms/views/__init__.py

http://github.com/feincms/feincms
Python | 69 lines | 50 code | 10 blank | 9 comment | 6 complexity | 6e34855cd38e4e406c4ef9341782277d MD5 | raw file
 1from __future__ import absolute_import, unicode_literals
 2
 3import logging
 4
 5from django.apps import apps
 6from django.http import Http404
 7from django.utils.functional import cached_property
 8
 9from feincms import settings
10from feincms.module.mixins import ContentView
11
12
13logger = logging.getLogger(__name__)
14
15
16class Handler(ContentView):
17    page_model_path = None
18    context_object_name = "feincms_page"
19
20    @cached_property
21    def page_model(self):
22        model = self.page_model_path or settings.FEINCMS_DEFAULT_PAGE_MODEL
23        return apps.get_model(*model.split("."))
24
25    def get_object(self):
26        path = None
27        if self.args:
28            path = self.args[0]
29        return self.page_model._default_manager.for_request(
30            self.request, raise404=True, best_match=True, path=path
31        )
32
33    def dispatch(self, request, *args, **kwargs):
34        try:
35            return super(Handler, self).dispatch(request, *args, **kwargs)
36        except Http404 as e:
37            if settings.FEINCMS_CMS_404_PAGE is not None:
38                logger.info(
39                    "Http404 raised for '%s', attempting redirect to"
40                    " FEINCMS_CMS_404_PAGE",
41                    args[0],
42                )
43                try:
44                    # Fudge environment so that we end up resolving the right
45                    # page.
46                    # Note: request.path is used by the page redirect processor
47                    # to determine if the redirect can be taken, must be == to
48                    # page url.
49                    # Also clear out the _feincms_page attribute which caches
50                    # page lookups (and would just re-raise a 404).
51                    request.path = request.path_info = settings.FEINCMS_CMS_404_PAGE
52                    if hasattr(request, "_feincms_page"):
53                        delattr(request, "_feincms_page")
54                    response = super(Handler, self).dispatch(
55                        request, settings.FEINCMS_CMS_404_PAGE, **kwargs
56                    )
57                    # Only set status if we actually have a page. If we get for
58                    # example a redirect, overwriting would yield a blank page
59                    if response.status_code == 200:
60                        response.status_code = 404
61                    return response
62                except Http404:
63                    logger.error(
64                        "Http404 raised while resolving" " FEINCMS_CMS_404_PAGE=%s",
65                        settings.FEINCMS_CMS_404_PAGE,
66                    )
67                    raise e
68            else:
69                raise