Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
108 lines (86 loc) · 3.41 KB
/
Copy pathmodels.py
File metadata and controls
108 lines (86 loc) · 3.41 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from datetime import datetime
from django.db import models
from modelcluster.fields import ParentalKey
from modelcluster.contrib.taggit import ClusterTaggableManager
from taggit.models import TaggedItemBase
from wagtail.core import blocks
from wagtail.core.models import Page, Orderable
from wagtail.core.fields import RichTextField, StreamField
from wagtail.admin.edit_handlers import (FieldPanel, InlinePanel, MultiFieldPanel,
StreamFieldPanel, PageChooserPanel)
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtail.images.blocks import ImageChooserBlock
from wagtail.embeds.blocks import EmbedBlock
from wagtail.contrib.table_block.blocks import TableBlock
from wagtail.search import index
from .blocks import (TwoColumnBlock, CustomTableBlock,
ThumbnailBlock)
class BlogIndexPage(Page):
intro = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('intro', classname="full")
]
def get_context(self, request):
# update context to include only published posts and in reverse order
context = super().get_context(request)
blogpages = self.get_children().live().order_by('-first_published_at')
context['blogpages'] = blogpages
return context
class BlogTagIndexPage(Page):
def get_context(self, request):
# filter by tag
tag = request.GET.get('tag')
blogpages = BlogPage.objects.filter(tags__name=tag)
# update context
context = super().get_context(request)
context['blogpages'] = blogpages
return context
class BlogPageTag(TaggedItemBase):
content_object = ParentalKey(
'BlogPage',
related_name='tagged_items',
on_delete=models.CASCADE
)
# TODO create profile for authors
class BlogPage(Page):
date = models.DateField("Post date", default=datetime.today)
excerpt = models.CharField('Extracto', max_length=250, blank=True)
body = StreamField([
('heading', blocks.CharBlock(classname="full title")),
('paragraph', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
('two_columns', TwoColumnBlock()),
('embedded_video', EmbedBlock(icon="media")),
('table', TableBlock()),
('custom_table', CustomTableBlock()),
('thumbnail', ThumbnailBlock()),
], null=True, blank=True)
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
def main_image(self):
gallery_item = self.gallery_images.first()
if gallery_item:
return gallery_item.image
return None
search_fields = Page.search_fields + [
index.SearchField('excerpt'),
index.SearchField('body'),
]
content_panels = Page.content_panels + [
MultiFieldPanel([
FieldPanel('date'),
FieldPanel('tags'),
], heading="Blog info"),
FieldPanel('excerpt'),
StreamFieldPanel('body', classname="full"),
InlinePanel('gallery_images', label="Gallery Images"),
]
class BlogPageGalleryImage(Orderable):
page = ParentalKey(BlogPage, on_delete=models.CASCADE, related_name='gallery_images')
image = models.ForeignKey(
'wagtailimages.Image', on_delete=models.CASCADE, related_name='+'
)
caption = models.CharField(max_length=250, blank=True)
panel = [
ImageChooserPanel('image'),
FieldPanel('caption'),
]

