forked from django-de/2010.djangocon.eu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
49 lines (38 loc) · 1.67 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
import datetime
from django.db import models
from django.db.models import permalink
from django.contrib.auth.models import User
from markdown import markdown
from smartypants import smartyPants
from utils.models import DateAwareModel
class PublicManager(models.Manager):
"""Returns published posts which are not in the future."""
def published(self):
return self.get_query_set().filter(draft=False, publish_date__lte=datetime.datetime.now)
class Post(DateAwareModel):
"""A blog post."""
author = models.ForeignKey(User, related_name='blog_posts', help_text='If left blank, will default to your user.')
title = models.CharField('Title', blank=False, max_length=140)
slug = models.SlugField('Slug', max_length=140, unique=True)
draft = models.BooleanField('Draft', default=False, help_text='If checked, will not appear in the public site.')
body_markdown = models.TextField('Body', blank=False, help_text = "Use markdown syntax")
body = models.TextField('Body', blank=True)
objects = PublicManager()
class Meta:
ordering = ('-publish_date',)
unique_together = ('publish_date', 'slug')
verbose_name = 'Blog Post'
get_latest_by = 'publish_date'
def __unicode__(self):
return self.title
def save(self, *args, **kwargs):
self.body = unicode(smartyPants(markdown(self.body_markdown)))
super(Post, self).save(*args, **kwargs)
@permalink
def get_absolute_url(self):
return('blog:blogpost_detail', (), {
'year': self.publish_date.year,
'month': self.publish_date.month,
'day': self.publish_date.day,
'slug': self.slug,
})