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
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)
20 def get_absolute_url(self):
21 return reverse('post-detail', kwargs={ 'slug': self.slug })
25 return mark_safe(commonmark.commonmark(self.body_markdown))
28 def teaser_html(self):
29 paragraphs = re.split(r'\n(\s*\n)+', self.body_markdown)
31 if len(paragraphs) == 0:
34 teaser_markdown = paragraphs[0]
36 for p in paragraphs[1:]:
37 if len(teaser_markdown) > 512:
39 teaser_markdown += '\n\n' + p
41 return mark_safe(commonmark.commonmark(teaser_markdown))