# -*- coding: utf-8 -*-
import time

from django.core.urlresolvers import reverse
from django.db import models
from django.template.defaultfilters import truncatechars
from django.utils.text import slugify
from django.utils import timezone
from django.conf import settings

from markitup.fields import MarkupField
from model_utils.models import TimeStampedModel
from taggit.managers import TaggableManager


class Entry(TimeStampedModel):
    """
    Represents a blog Entry.
    Uses TimeStampModel to provide created and modified fields
    """
    title = models.CharField(max_length=500)
    slug = models.SlugField(unique=True, editable=False)
    content = MarkupField()
    is_published = models.BooleanField(default=False)
    published_timestamp = models.DateTimeField(blank=True, null=True, editable=False)
    author = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, editable=True)
    tags = TaggableManager(blank=True)

    def __unicode__(self):
        return self.title

    class Meta:
        verbose_name_plural = "entries"

    def get_absolute_url(self):
        return reverse('andablog:entrydetail', args=[self.slug])

    def _slugify_title(self):
        """Slugify the Entry title, but ensure it's less than 50 characters.
        This method also ensures that a slug is unique by appending a timestamp
        to any duplicate slugs.

        """
        # Restrict slugs to 50 chars, but don't split mid-word
        self.slug = slugify(self.title)
        while len(self.slug) > 50:
            self.slug = '-'.join(self.slug.split('-')[:-1])

        # Is the same slug as another entry?
        if Entry.objects.filter(slug=self.slug).exclude(id=self.id).exists():
            # Append time to differentiate, but keep total length < 50 chars.
            timestamp = str(int(time.time()))
            slug = "{}-{}".format(self.slug, timestamp)
            if len(slug) > 50:
                slug = '-'.join([self.slug[:-len(timestamp)], timestamp])
            self.slug = slug

    def save(self, *args, **kwargs):
        self._slugify_title()

        # Time to publish?
        if not self.published_timestamp and self.is_published:
            self.published_timestamp = timezone.now()
        elif not self.is_published:
            self.published_timestamp = None

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


class EntryImage(TimeStampedModel):
    entry = models.ForeignKey(Entry)
    image = models.ImageField(blank=True, upload_to='andablog/images')

    @property
    def image_url(self):
        return self.get_absolute_url()

    def get_absolute_url(self):
        return self.image.url

    def __unicode__(self):
        return u"{entry} - {image}".format(
            entry=truncatechars(self.entry, 10),
            image=truncatechars(self.image.name, 10),
        )

@@ -1138,24 +1138,437 @@
 elf.slug%5D)%0A%0A
+    def _insert_timestamp(self, value, max_length=50):%0A        %22%22%22Appends a timestamp integer to the given value, yet ensuring the%0A        result is less than the specified max_length.%0A        %22%22%22%0A        timestamp = str(int(time.time()))%0A        value = %22%7B%7D-%7B%7D%22.format(value, timestamp)%0A        if len(value) %3E max_length:%0A            value = '-'.join(%5Bvalue%5B:-len(timestamp)%5D, timestamp%5D)%0A        return value%0A%0A
     def _slu
@@ -2203,231 +2203,68 @@
     
-timestamp = str(int(time.time()))%0A            slug = %22%7B%7D-%7B%7D%22.format(self.slug, timestamp)%0A            if len(slug) %3E 50:%0A                slug = '-'.join(%5Bself.slug%5B:-len(timestamp)%5D, timestamp%5D)%0A            self.slug = slug
+self.slug = self._insert_timestamp(self.slug, max_length=50)
 %0A%0A  

move the slug de-duplication code into its own method