From f9520dcd20ff48c6ade836891461182289550760 Mon Sep 17 00:00:00 2001
From: Aaron Williamson <aaron@copiesofcopies.org>
Date: Thu, 12 May 2011 13:59:18 -0400
Subject: [PATCH 1/3] Patch to create slugs when new images are added.

---
 mediagoblin/models.py          |    4 ++++
 mediagoblin/submit/views.py    |    3 +++
 mediagoblin/tests/test_util.py |    7 +++++++
 mediagoblin/util.py            |   15 ++++++++++++++-
 setup.py                       |    1 +
 5 files changed, 29 insertions(+), 1 deletions(-)

diff --git a/mediagoblin/models.py b/mediagoblin/models.py
index 69b1f4f..96b2de7 100644
--- a/mediagoblin/models.py
+++ b/mediagoblin/models.py
@@ -18,6 +18,7 @@ import datetime, uuid
 
 from mongokit import Document, Set
 
+from mediagoblin import util
 from mediagoblin.auth import lib as auth_lib
 
 
@@ -66,6 +67,7 @@ class MediaEntry(Document):
     structure = {
         'uploader': User,
         'title': unicode,
+        'slug': unicode,
         'created': datetime.datetime,
         'description': unicode,
         'media_type': unicode,
@@ -98,6 +100,8 @@ class MediaEntry(Document):
     def main_mediafile(self):
         pass
 
+    def generate_slug(self):
+        self['slug'] = util.slugify(self['title'])
 
 REGISTER_MODELS = [MediaEntry, User]
 
diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py
index 5e262f1..4e20cc6 100644
--- a/mediagoblin/submit/views.py
+++ b/mediagoblin/submit/views.py
@@ -46,6 +46,9 @@ def submit_start(request):
             entry['media_type'] = u'image' # heh
             entry['uploader'] = request.user
 
+            # Generate a slug from the title
+            entry.generate_slug()
+
             # Save, just so we can get the entry id for the sake of using
             # it to generate the file path
             entry.save(validate=False)
diff --git a/mediagoblin/tests/test_util.py b/mediagoblin/tests/test_util.py
index 5bc31fd..861e8b6 100644
--- a/mediagoblin/tests/test_util.py
+++ b/mediagoblin/tests/test_util.py
@@ -69,3 +69,10 @@ I hope you like unit tests JUST AS MUCH AS I DO!"""
     assert mbox_message.get_payload(decode=True) == """HAYYY GUYS!
 
 I hope you like unit tests JUST AS MUCH AS I DO!"""
+
+def test_slugify():
+    assert util.slugify('a walk in the park') == 'a-walk-in-the-park'
+    assert util.slugify('A Walk in the Park') == 'a-walk-in-the-park'
+    assert util.slugify('a  walk in the park') == 'a-walk-in-the-park'
+    assert util.slugify('a walk in-the-park') == 'a-walk-in-the-park'
+    assert util.slugify('a w@lk in the park?') == 'a-w-lk-in-the-park'
diff --git a/mediagoblin/util.py b/mediagoblin/util.py
index 8695180..37bd532 100644
--- a/mediagoblin/util.py
+++ b/mediagoblin/util.py
@@ -17,9 +17,10 @@
 from email.MIMEText import MIMEText
 import smtplib
 import sys
-
+import re
 import jinja2
 import mongokit
+import translitcodec
 
 from mediagoblin import globals as mgoblin_globals
 
@@ -87,6 +88,18 @@ def import_component(import_string):
     func = getattr(module, func_name)
     return func
 
+_punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
+
+def slugify(text, delim=u'-'):
+    """
+    Generates an ASCII-only slug. Taken from http://flask.pocoo.org/snippets/5/
+    """
+    result = []
+    for word in _punct_re.split(text.lower()):
+        word = word.encode('translit/long')
+        if word:
+            result.append(word)
+    return unicode(delim.join(result))
 
 ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 ### Special email test stuff begins HERE
diff --git a/setup.py b/setup.py
index 7b483a5..283f9a2 100644
--- a/setup.py
+++ b/setup.py
@@ -38,6 +38,7 @@ setup(
         'jinja2',
         'sphinx',
         'PIL',
+        'translitcodec',
         ],
     test_suite='nose.collector',
 
-- 
1.7.4.4

