from __future__ import unicode_literals

from django.core.exceptions import ValidationError
from django.db import models
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _


class TimestampMixin(models.Model):
    """Mixin for date and timestamp. Inherits django's models.Model."""

    date_created = models.DateTimeField(auto_now_add=True)
    date_modified = models.DateTimeField(auto_now=True)

    class Meta:
        abstract = True


class SlugifyMixin():
    """
    Slugify specific field and pass as value to slug field in the model.

    This mixin helps in solving the problem of having case insensitive duplicates
    by creating a slug and ensuring uniqueness.
    Model field to be slugified should be passed as a string into a variable
    called slugify_field.
    Slug field in the model should be named slug.
    """

    def clean(self):
        if hasattr(self, 'slugify_field') and hasattr(self, 'slug'):
            slugify_field_value = getattr(self, self.slugify_field)
            self.slug = slugify(slugify_field_value)

            # If pk exists, object exists in db and is being edited by user.
            if self.__class__.objects.filter(slug=self.slug).exists() and not self.pk:
                raise ValidationError(_("Entry with {0} - {1} already exists.".format(
                                        self.slugify_field, slugify_field_value)))

    def save(self, *args, **kwargs):
        self.clean()
        return super().save(*args, **kwargs)

@@ -871,16 +871,201 @@
 %0A    %22%22%22
+%0A    def raise_validation_error(self, value):%0A        raise ValidationError(_(%22Entry with %7B0%7D - %7B1%7D already exists.%22.format(%0A                                self.slugify_field, value)))
 %0A%0A    de
@@ -1266,24 +1266,99 @@
 eld_value)%0A%0A
+            if self.__class__.objects.filter(slug=self.slug).exists():%0A    
             
@@ -1430,32 +1430,36 @@
             
+    
 if self.
 __class__.ob
@@ -1450,75 +1450,88 @@
 elf.
-__class__.objects.filter(slug=self.slug).exists() and not self.pk:%0A
+pk:%0A                    try:%0A                        self.validate_unique()%0A    
     
@@ -1534,37 +1534,38 @@
                 
-raise
+except
  ValidationError
@@ -1568,57 +1568,83 @@
 rror
-(_(%22Entry with %7B0%7D - %7B1%7D already exists.%22.format(
+:%0A                        self.raise_validation_error(slugify_field_value)%0A
 %0A   
@@ -1648,35 +1648,38 @@
                 
-   
+else:%0A
                 
@@ -1682,37 +1682,44 @@
         
- 
 self.
-slugify_field, 
+raise_validation_error(
 slugify_
@@ -1726,26 +1726,24 @@
 field_value)
-))
 %0A%0A    def sa

Add check to raise error if edited object has already existing slug name.