-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathviews.py
92 lines (70 loc) · 2.57 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from bakery.views import BuildableDetailView
from wagtail.wagtailcore.views import serve
from wagtail.wagtailcore.middleware import SiteMiddleware
from wagtail.wagtailcore.models import Site
class BakeryView(BuildableDetailView):
"""
An abstract class that can be inherited to create a buildable view that can be
added to BAKERY_VIEWS setting. An inheriting class should define a bakery_model
property pointing to a Wagtail Page model.
Example:
# File: app/models.py
from wagtail.wagtailcore.pages import Page
class AuthorPage(Page):
bakery_views = ('app.bakery_views.AuthorPage',)
...
# File: app/bakery_views.py
from wagtail.wagtailbakery.views import BakeryView
from . import models
class AuthorPage(BakeryView):
bakery_model = models.AuthorPage
# File: project/settings.py:
BAKERY_VIEWS = (
'app.bakery_views.AuthorPage',
...
)
BUILD_DIR = os.path.join(PROJECT_ROOT, 'baked')
Build command:
python manage.py build app.bakery_views.AuthorPage
"""
bakery_model = None
def get_queryset(self):
"""
Defines get_queryset() for BuildableDetailView to return a
QuerySet containing all live Wagtail Page models
"""
return self.bakery_model.objects.live()
def get(self, request):
"""
Overrides DetailView's get() to return TemplateResponse from serve()
after passing request through Wagtail SiteMiddleware
"""
smw = SiteMiddleware()
smw.process_request(request)
response = serve(request, request.path)
return response
def get_content(self):
"""
Overrides BuildableMixin's get_content() to work with
both TemplateRespose and HttpResponse
"""
response = self.get(self.request)
if hasattr(response, 'render'):
response = response.render()
return response.content
def get_url(self, obj):
"""
Overrides BuildableDetailView's get_url() to return a url from the
Page model url_path property
"""
root_path = Site.get_site_root_paths()
if len(root_path):
root_path = root_path[0][1]
if obj.url_path == root_path:
return '/'
elif root_path in obj.url_path:
if obj.url_path.index(root_path) == 0:
return obj.url.replace(root_path, '/', 1)
return obj.url_path
class Meta:
abstract = True