from datetime import timedelta

from django.utils import timezone
from django.db import models
from django.template.defaultfilters import slugify

from ckeditor.fields import RichTextField
from taggit.managers import TaggableManager

from blog.managers import PublishedManager


class Post(models.Model):
    verbose_name = 'blog post'
    # MANAGERS
    objects = models.Manager()
    published_objects = PublishedManager()

    # FIELDS
    title = models.CharField(max_length=200)
    slug = models.SlugField(max_length=40, unique=True)
    text = RichTextField(config_name='blog')
    tags = TaggableManager()
    STATUS_CHOICES = (('published', 'Published'),
                      ('draft', 'Draft'),)
    status = models.CharField(choices=STATUS_CHOICES,
                              default='published',
                              max_length=9)

    pub_date = models.DateTimeField('Date published', default=timezone.now,
                                    editable=False)
    date_created = models.DateTimeField(editable=False)
    last_modified = models.DateTimeField(editable=False)

    # METHODS
    def get_absolute_url(self):
        # TODO Sort of hardcoded url in model?
        date = (self.pub_date.strftime('%Y %b')).lower().split()
        return "/blog/%s/%s/%s" % (date[0],
                                   date[1],
                                   self.slug)

    def save(self, *args, **kwargs):
        status = self.status

        # If draft, set pub_date very far in the future
        if status == 'draft':
            self.pub_date = timezone.now() + timedelta(days=900000)

        if status == 'published' and self.pub_date > timezone.now():
        # ie if status changed from draft to published
        # (as draft is only way pub_date can be in future)
                self.pub_date = timezone.now()

        self.last_modified = timezone.now()
        if not self.id:
        # Newly created object, so set slug and date created
            self.date_created = timezone.now()
            self.slug = slugify(self.title)

        super(Post, self).save(*args, **kwargs)

    def __str__(self):
        return self.title

    class Meta:
        ordering = ['-pub_date']
        verbose_name = 'blog post'

@@ -25,16 +25,61 @@
 edelta%0A%0A
+from django.core.urlresolvers import reverse%0A
 from dja
@@ -1195,59 +1195,19 @@
     
-# TODO Sort of hardcoded url in model?%0A        date
+year, month
  = (
@@ -1274,35 +1274,55 @@
 urn 
-%22/blog/%25s/%25s/%25s%22 %25 (date%5B0%5D
+reverse('blog-detail', kwargs=%7B'pub_year': year
 ,%0A  
@@ -1358,15 +1358,37 @@
     
-date%5B1%5D
+           'pub_month': month
 ,%0A  
@@ -1411,32 +1411,51 @@
                 
+            'slug':
  self.slug)%0A%0A   
@@ -1448,16 +1448,17 @@
 elf.slug
+%7D
 )%0A%0A    d

Use reverse to get url