|
1 | 1 | from django.db import models |
2 | 2 | from wagtail.models import Page |
3 | 3 | from wagtail.fields import RichTextField |
4 | | -from wagtail.admin.panels import FieldPanel |
| 4 | +from wagtail.admin.panels import FieldPanel, InlinePanel |
| 5 | +from wagtail.snippets.models import register_snippet |
| 6 | +from wagtail import hooks |
| 7 | +from modelcluster.fields import ParentalKey |
| 8 | +from modelcluster.contrib.taggit import ClusterTaggableManager |
| 9 | +from taggit.models import TaggedItemBase |
| 10 | + |
| 11 | + |
| 12 | +@register_snippet |
| 13 | +class Author(models.Model): |
| 14 | + """ |
| 15 | + Author snippet for blog posts. |
| 16 | +
|
| 17 | + Reusable snippet containing author information that can be referenced |
| 18 | + by blog posts. Based on Jekyll's _data/authors.yml structure. |
| 19 | +
|
| 20 | + Attributes |
| 21 | + ---------- |
| 22 | + name : CharField |
| 23 | + The author's full name as it appears in bylines |
| 24 | + slug : SlugField |
| 25 | + URL-safe version of the author's name, unique identifier |
| 26 | + bio : TextField |
| 27 | + Short biographical description of the author |
| 28 | + avatar : ImageField |
| 29 | + Profile photo of the author |
| 30 | + email : EmailField |
| 31 | + Author's contact email address |
| 32 | + website : URLField |
| 33 | + Personal or professional website URL |
| 34 | + github : CharField |
| 35 | + GitHub username (without @ symbol) |
| 36 | + linkedin : URLField |
| 37 | + LinkedIn profile URL |
| 38 | + mastodon : CharField |
| 39 | + Mastodon handle (without @ symbol) |
| 40 | + discord : CharField |
| 41 | + Discord username |
| 42 | + """ |
| 43 | + name = models.CharField(max_length=255) |
| 44 | + slug = models.SlugField(max_length=100, unique=True) |
| 45 | + bio = models.TextField(blank=True, help_text="Short bio of the author") |
| 46 | + avatar = models.ImageField(upload_to='authors/', blank=True, null=True) |
| 47 | + email = models.EmailField(blank=True) |
| 48 | + website = models.URLField(blank=True) |
| 49 | + github = models.CharField(max_length=100, blank=True) |
| 50 | + linkedin = models.URLField(blank=True) |
| 51 | + mastodon = models.CharField(max_length=100, blank=True) |
| 52 | + discord = models.CharField(max_length=100, blank=True) |
| 53 | + |
| 54 | + panels = [ |
| 55 | + FieldPanel('name'), |
| 56 | + FieldPanel('slug'), |
| 57 | + FieldPanel('bio'), |
| 58 | + FieldPanel('avatar'), |
| 59 | + FieldPanel('email'), |
| 60 | + FieldPanel('website'), |
| 61 | + FieldPanel('github'), |
| 62 | + FieldPanel('linkedin'), |
| 63 | + FieldPanel('mastodon'), |
| 64 | + FieldPanel('discord'), |
| 65 | + ] |
| 66 | + |
| 67 | + def __str__(self): |
| 68 | + return self.name |
| 69 | + |
| 70 | + class Meta: |
| 71 | + verbose_name = "Author" |
| 72 | + verbose_name_plural = "Authors" |
| 73 | + |
| 74 | + |
| 75 | +class BlogPageTag(TaggedItemBase): |
| 76 | + """ |
| 77 | + Tag model for blog posts. |
| 78 | +
|
| 79 | + Enables tagging functionality for blog posts using django-taggit. |
| 80 | + """ |
| 81 | + content_object = ParentalKey( |
| 82 | + 'BlogPage', |
| 83 | + related_name='tagged_items', |
| 84 | + on_delete=models.CASCADE |
| 85 | + ) |
5 | 86 |
|
6 | 87 |
|
7 | 88 | class BlogIndexPage(Page): |
8 | 89 | """ |
9 | 90 | Index page for blog posts. |
10 | | - |
11 | | - Wagtail page model for the main blog listing page. |
| 91 | +
|
| 92 | + Landing page that displays a listing of all blog posts and events. |
| 93 | + Provides context for rendering the blog index template. |
| 94 | +
|
| 95 | + Attributes |
| 96 | + ---------- |
| 97 | + intro : RichTextField |
| 98 | + Introduction text displayed at the top of the blog index |
12 | 99 | """ |
13 | 100 | intro = RichTextField(blank=True) |
14 | 101 |
|
15 | 102 | content_panels = Page.content_panels + [ |
16 | 103 | FieldPanel('intro'), |
17 | 104 | ] |
18 | 105 |
|
| 106 | + def get_context(self, request): |
| 107 | + """ |
| 108 | + Add blog posts to template context. |
| 109 | +
|
| 110 | + Returns |
| 111 | + ------- |
| 112 | + dict |
| 113 | + Template context including blog_posts queryset |
| 114 | + """ |
| 115 | + context = super().get_context(request) |
| 116 | + blog_posts = self.get_children().live().order_by('-first_published_at') |
| 117 | + context['blog_posts'] = blog_posts |
| 118 | + return context |
| 119 | + |
19 | 120 | class Meta: |
20 | 121 | verbose_name = "Blog Index Page" |
21 | 122 |
|
22 | 123 |
|
23 | 124 | class BlogPage(Page): |
24 | 125 | """ |
25 | 126 | Individual blog post page. |
26 | | - |
27 | | - Wagtail page model for individual blog posts with date, intro, and content. |
| 127 | +
|
| 128 | + Page model for individual blog posts with full Jekyll frontmatter compatibility. |
| 129 | + Supports authors, categories/tags, header images, and rich content. |
| 130 | +
|
| 131 | + Attributes |
| 132 | + ---------- |
| 133 | + date : DateField |
| 134 | + Publication date of the blog post |
| 135 | + last_modified : DateTimeField |
| 136 | + Last modification timestamp |
| 137 | + author : ForeignKey |
| 138 | + Reference to Author snippet |
| 139 | + excerpt : TextField |
| 140 | + Short description/summary of the post |
| 141 | + header_image : ImageField |
| 142 | + Featured image for the post |
| 143 | + header_image_alt : CharField |
| 144 | + Alt text for the header image |
| 145 | + body : RichTextField |
| 146 | + Main content of the blog post |
| 147 | + enable_comments : BooleanField |
| 148 | + Whether to show comments section |
| 149 | + tags : ClusterTaggableManager |
| 150 | + Categories/tags for the post |
28 | 151 | """ |
29 | | - date = models.DateField("Post date") |
30 | | - intro = models.CharField(max_length=250) |
| 152 | + date = models.DateField("Post date", help_text="Date when the post was published") |
| 153 | + last_modified = models.DateTimeField(auto_now=True) |
| 154 | + author = models.ForeignKey( |
| 155 | + 'Author', |
| 156 | + null=True, |
| 157 | + blank=True, |
| 158 | + on_delete=models.SET_NULL, |
| 159 | + related_name='blog_posts' |
| 160 | + ) |
| 161 | + excerpt = models.TextField( |
| 162 | + blank=True, |
| 163 | + max_length=500, |
| 164 | + help_text="Brief description of the post" |
| 165 | + ) |
| 166 | + header_image = models.ImageField( |
| 167 | + upload_to='blog/headers/', |
| 168 | + blank=True, |
| 169 | + null=True, |
| 170 | + help_text="Featured image for the post" |
| 171 | + ) |
| 172 | + header_image_alt = models.CharField( |
| 173 | + max_length=255, |
| 174 | + blank=True, |
| 175 | + help_text="Alt text for the header image" |
| 176 | + ) |
31 | 177 | body = RichTextField(blank=True) |
| 178 | + enable_comments = models.BooleanField( |
| 179 | + default=False, |
| 180 | + help_text="Show comments section on this post" |
| 181 | + ) |
| 182 | + tags = ClusterTaggableManager(through=BlogPageTag, blank=True) |
32 | 183 |
|
33 | 184 | content_panels = Page.content_panels + [ |
34 | 185 | FieldPanel('date'), |
35 | | - FieldPanel('intro'), |
| 186 | + FieldPanel('author'), |
| 187 | + FieldPanel('excerpt'), |
| 188 | + FieldPanel('header_image'), |
| 189 | + FieldPanel('header_image_alt'), |
36 | 190 | FieldPanel('body'), |
| 191 | + FieldPanel('enable_comments'), |
| 192 | + FieldPanel('tags'), |
37 | 193 | ] |
38 | 194 |
|
39 | 195 | class Meta: |
40 | 196 | verbose_name = "Blog Page" |
| 197 | + |
| 198 | + |
| 199 | +class EventPage(BlogPage): |
| 200 | + """ |
| 201 | + Event page extending blog post functionality. |
| 202 | +
|
| 203 | + Specialized blog page for events with additional event-specific fields. |
| 204 | + Inherits all blog post functionality while adding event metadata. |
| 205 | +
|
| 206 | + Attributes |
| 207 | + ---------- |
| 208 | + start_date : DateField |
| 209 | + Date when the event starts/started |
| 210 | + end_date : DateField |
| 211 | + Date when the event ends/ended (optional) |
| 212 | + location : CharField |
| 213 | + Where the event takes place |
| 214 | + event_type : CharField |
| 215 | + Type of event (workshop, webinar, conference, etc.) |
| 216 | + """ |
| 217 | + start_date = models.DateField( |
| 218 | + "Event start date", |
| 219 | + help_text="Date when the event starts" |
| 220 | + ) |
| 221 | + end_date = models.DateField( |
| 222 | + "Event end date", |
| 223 | + blank=True, |
| 224 | + null=True, |
| 225 | + help_text="Date when the event ends (optional)" |
| 226 | + ) |
| 227 | + location = models.CharField( |
| 228 | + max_length=255, |
| 229 | + blank=True, |
| 230 | + help_text="Where the event takes place" |
| 231 | + ) |
| 232 | + |
| 233 | + EVENT_TYPES = [ |
| 234 | + ('workshop', 'Workshop'), |
| 235 | + ('webinar', 'Webinar'), |
| 236 | + ('conference', 'Conference'), |
| 237 | + ('meetup', 'Meetup'), |
| 238 | + ('other', 'Other'), |
| 239 | + ] |
| 240 | + |
| 241 | + event_type = models.CharField( |
| 242 | + max_length=20, |
| 243 | + choices=EVENT_TYPES, |
| 244 | + default='other', |
| 245 | + help_text="Type of event" |
| 246 | + ) |
| 247 | + |
| 248 | + content_panels = Page.content_panels + [ |
| 249 | + FieldPanel('date'), |
| 250 | + FieldPanel('start_date'), |
| 251 | + FieldPanel('end_date'), |
| 252 | + FieldPanel('location'), |
| 253 | + FieldPanel('event_type'), |
| 254 | + FieldPanel('author'), |
| 255 | + FieldPanel('excerpt'), |
| 256 | + FieldPanel('header_image'), |
| 257 | + FieldPanel('header_image_alt'), |
| 258 | + FieldPanel('body'), |
| 259 | + FieldPanel('enable_comments'), |
| 260 | + FieldPanel('tags'), |
| 261 | + ] |
| 262 | + |
| 263 | + class Meta: |
| 264 | + verbose_name = "Event Page" |
0 commit comments