-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
143 lines (110 loc) · 3.82 KB
/
models.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from datetime import date
from django.conf import settings
from django.db import models
from django.utils import timezone
from wagtail.wagtailcore.blocks import CharBlock, TextBlock, URLBlock
from wagtail.wagtailcore.models import Page, Site
from wagtail.wagtailcore.fields import StreamField, RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel, StreamFieldPanel
from wagtail.wagtailimages.blocks import ImageChooserBlock
from wagtail.wagtailsearch import index
from wagtail.wagtailsnippets.models import register_snippet
from modelcluster.fields import ParentalKey
from modelcluster.tags import ClusterTaggableManager
from taggit.models import TaggedItemBase
from .blocks import CodeBlock
class WPPageTag(TaggedItemBase):
"""
Base class for storing tags for Pages and Posts.
"""
content_object = ParentalKey('wagtailpress.WPPage', related_name='tagged_items')
@register_snippet
class Config(models.Model):
"""
Class for storing configuration values for WagtailPress.
"""
site = models.ForeignKey(Site, null=True, blank=True, related_name='site', on_delete=models.SET_NULL)
name = models.CharField(max_length=255, blank=True)
value = models.CharField(max_length=255, blank=True)
def __str__(self):
return '(' + str(self.site) + ') ' + self.name + ': ' + self.value
@register_snippet
class Category(models.Model):
"""
Base class for storing categories for Posts.
"""
class Meta:
verbose_name_plural = "Categories"
name = models.CharField(max_length=255, blank=True)
panels = [
FieldPanel('name'),
]
def __str__(self):
return self.name
class WPPage(Page):
"""
This class will hold pages, similar to WordPress' post_type of 'page'.
Posts will inherit from this class, adding additional fields needed.
"""
class Meta:
verbose_name = "Page"
content = StreamField([
('heading', CharBlock(classname="full title")),
('paragraph', TextBlock()),
('image', ImageChooserBlock()),
('url', URLBlock()),
('code', CodeBlock()),
])
tags = ClusterTaggableManager(through=WPPageTag, blank=True)
modified = models.DateTimeField("Page Modified", null=True)
search_fields = Page.search_fields + (
index.SearchField('title'),
index.SearchField('content'),
)
content_panels = Page.content_panels + [
FieldPanel('tags'),
StreamFieldPanel('content'),
]
def save(self, *args, **kwargs):
self.modified = timezone.now()
super(WPPage, self).save(*args, **kwargs)
def __str__(self):
return 'ID %s: %s' % (str(self.pk), self.title)
class WPPost(WPPage):
"""
This class will hold blog posts, similar to WordPress' post_type of 'post'.
"""
class Meta:
verbose_name = "Post"
date = models.DateField("Post Date", default=date.today)
author = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, blank=True, related_name='author', on_delete=models.SET_NULL)
categories = models.ManyToManyField(
Category,
blank=True,
)
excerpt = models.CharField(
max_length=250,
blank=True,
)
search_fields = WPPage.search_fields + (
index.SearchField('title'),
index.SearchField('excerpt'),
index.SearchField('content'),
)
content_panels = Page.content_panels + [
FieldPanel('date'),
FieldPanel('author'),
FieldPanel('categories'),
FieldPanel('excerpt'),
FieldPanel('tags'),
StreamFieldPanel('content'),
]
def __str__(self):
return 'ID %s: %s' % (str(self.pk), self.title)
"""
class BlogIndexPage(Page):
excerpt = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('excerpt', classname="full")
]
"""