Generates Unique (& Unicode) slugs in Django Edit Add topics
Find a file
2012-08-06 14:29:25 +02:00
uuslug refactor 2012-08-06 14:29:25 +02:00
.gitignore update the ignore list 2011-10-26 13:29:03 -04:00
bootstrap.py Initial Release 2011-05-30 11:26:21 -04:00
buildout.cfg Up django version to 1.3.1 2011-10-26 15:34:25 -04:00
LICENSE Initial Release 2011-05-30 11:26:21 -04:00
pushpi.sh rename pypi utility script 2011-10-26 21:40:20 -04:00
README refactor 2012-08-06 14:29:25 +02:00
setup.py Cleaned up readme and comments and up the version 2011-10-27 14:58:32 -04:00

django-uuslug
================


This fork notes
------------------------------------------------------------------------------------------------
Here ' symbol in result of unidecode.unidecode() is deleted instead of replacing with -.
For example for Russian word "Компьютер" unidecode.unidecode() returns "komp'uter", 
then uuslug create "komp-uter" from this, but translitiration "komputer" seems more natural.


A Django slugify application that guarantees uniqueness and handles unicode.
UUSlug = (``U``nique + ``U``nicode Slug)

Patches welcome: https://github.com/un33k/django-uuslug

Usage
=====

A. Install django-uuslug:
    * _ Make sure you have python 2.6+ and can install from pypi
        1. easy_install django-uuslug
        2. pip install django-uuslug
        3. git clone http://github.com/un33k/django-uuslug
            a. cd django-uuslug
            b. run python setup.py
        4. wget https://github.com/un33k/django-uuslug/zipball/master
            a. unzip the downloaded file
            b. cd into django-uuslug-* directory
            c. run python setup.py

B. Test & contribute to django-uuslug: (for developers)
    * _ git clone http://github.com/un33k/django-uuslug
            a. cd into django-uuslug
            b. run python bootstrap.py
            c. run bin/buildout -vvvvv
            d. run bin/test


Unicode Test Example
=====================
from uuslug import slugify

s = "This is a test ---"
r = slugify(s)
self.assertEquals(r, "this-is-a-test")

s = 'C\'est déjà l\'été.'
r = slugify(s)
self.assertEquals(r, "c-est-deja-l-ete")

s = 'Nín hǎo. Wǒ shì zhōng guó rén'
r = slugify(s)
self.assertEquals(r, "nin-hao-wo-shi-zhong-guo-ren")

s = '影師嗎'
r = slugify(s)
self.assertEquals(r, "ying-shi-ma")


Uniqueness Test Example
=======================
Override your object's save method with something like this (models.py)

from django.db import models
from uuslug import uuslug

class CoolSlug(models.Model):
    name = models.CharField(max_length=100)
    slug = models.CharField(max_length=200)
    
    def __unicode__(self):
        return self.name
    
    def save(self, *args, **kwargs):
        self.slug = uuslug(self.name, instance=self)
        super(CoolSlug, self).save(*args, **kwargs)


You can specify the start number, e.g.: the second slug should start with "-2" instead of "-1":
    self.slug = uuslug(self.name, instance=self, start_no=2)


Test:
=====
 
name = "john"
c = CoolSlug.objects.create(name=name)
c.save()
self.assertEquals(c.slug, name) # slug = "john"
  
c1 = CoolSlug.objects.create(name=name)
c1.save()
self.assertEquals(c1.slug, name+"-1") # slug = "john-1"

c1 = CoolSlug.objects.create(name=name)
c1.save()
self.assertEquals(c1.slug, name+"-2") # slug = "john-2"

ToDo:
=====
clean up README
add more test and examples

Credits:
=========
This is a cleaned and packaged up version of snippet (369) from http://www.djangosnippets.org/
Improved to handle unique slugs and unicode chars and packaged for easy install.