Add feather icons and use them in the global nav, add RSS
[styx.blog] / core / models.py
1 import re
2
3 from django.contrib.auth.models import User
4 from django.db import models
5 from django.urls import reverse
6 from django.utils.safestring import mark_safe
7
8 import commonmark
9
10 class Post(models.Model):
11     title = models.CharField(max_length=256, null=True)
12     slug = models.SlugField()
13     author = models.ForeignKey(User, on_delete=models.CASCADE)
14     publication_utc = models.DateTimeField(null=True, blank=True)
15     body_markdown = models.TextField(null=True)
16
17     def __str__(self):
18         return self.title
19
20     def get_absolute_url(self):
21         return reverse('post-detail', kwargs={ 'slug': self.slug })
22
23     @property
24     def body_html(self):
25         return mark_safe(commonmark.commonmark(self.body_markdown))
26
27     @property
28     def teaser_html(self):
29         paragraphs = re.split(r'\n(\s*\n)+', self.body_markdown)
30
31         if len(paragraphs) == 0:
32             return ''
33
34         teaser_markdown = paragraphs[0]
35
36         for p in paragraphs[1:]:
37             if len(teaser_markdown) > 512:
38                 break
39             teaser_markdown += '\n\n' + p
40
41         return mark_safe(commonmark.commonmark(teaser_markdown))