Ticket #36: slug-patch.txt

File slug-patch.txt, 4.1 KB (added by Aaron Williamson, 13 years ago)

slug-patch.txt

Line 
1diff --git a/mediagoblin/models.py b/mediagoblin/models.py
2index 69b1f4f..a858ffe 100644
3--- a/mediagoblin/models.py
4+++ b/mediagoblin/models.py
5@@ -18,8 +18,9 @@ import datetime, uuid
6
7 from mongokit import Document, Set
8
9+from mediagoblin import util
10 from mediagoblin.auth import lib as auth_lib
11-
12+from mediagoblin import globals as mediagoblin_globals
13
14 ###################
15 # Custom validators
16@@ -66,6 +67,7 @@ class MediaEntry(Document):
17 structure = {
18 'uploader': User,
19 'title': unicode,
20+ 'slug': unicode,
21 'created': datetime.datetime,
22 'description': unicode,
23 'media_type': unicode,
24@@ -98,6 +100,15 @@ class MediaEntry(Document):
25 def main_mediafile(self):
26 pass
27
28+ def generate_slug(self):
29+ """ Generate a unique slug based on the image's title """
30+
31+ self['slug'] = util.slugify(self['title'])
32+
33+ duplicate = mediagoblin_globals.database.media_entries.find_one({'slug': self['slug']})
34+
35+ if duplicate:
36+ self['slug'] = "%s-%s" % (self['_id'], self['slug'])
37
38 REGISTER_MODELS = [MediaEntry, User]
39
40diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py
41index 5e262f1..eda77b0 100644
42--- a/mediagoblin/submit/views.py
43+++ b/mediagoblin/submit/views.py
44@@ -50,6 +50,9 @@ def submit_start(request):
45 # it to generate the file path
46 entry.save(validate=False)
47
48+ # Generate a slug from the title
49+ entry.generate_slug()
50+
51 # Now store generate the queueing related filename
52 queue_filepath = request.app.queue_store.get_unique_filepath(
53 ['media_entries',
54diff --git a/mediagoblin/tests/test_util.py b/mediagoblin/tests/test_util.py
55index ff40a67..7b00a07 100644
56--- a/mediagoblin/tests/test_util.py
57+++ b/mediagoblin/tests/test_util.py
58@@ -70,6 +70,14 @@ I hope you like unit tests JUST AS MUCH AS I DO!"""
59
60 I hope you like unit tests JUST AS MUCH AS I DO!"""
61
62+def test_slugify():
63+ assert util.slugify('a walk in the park') == 'a-walk-in-the-park'
64+ assert util.slugify('A Walk in the Park') == 'a-walk-in-the-park'
65+ assert util.slugify('a walk in the park') == 'a-walk-in-the-park'
66+ assert util.slugify('a walk in-the-park') == 'a-walk-in-the-park'
67+ assert util.slugify('a w@lk in the park?') == 'a-w-lk-in-the-park'
68+ assert util.slugify(u'a walk in the par\u0107') == 'a-walk-in-the-parc'
69+ assert util.slugify(u'\u00E0\u0042\u00E7\u010F\u00EB\u0066') == 'abcdef'
70
71 def test_locale_to_lower_upper():
72 """
73diff --git a/mediagoblin/util.py b/mediagoblin/util.py
74index ac977bd..31ff4f6 100644
75--- a/mediagoblin/util.py
76+++ b/mediagoblin/util.py
77@@ -17,9 +17,10 @@
78 from email.MIMEText import MIMEText
79 import smtplib
80 import sys
81-
82+import re
83 import jinja2
84 import mongokit
85+import translitcodec
86
87 from mediagoblin import globals as mgoblin_globals
88
89@@ -97,6 +98,18 @@ def import_component(import_string):
90 func = getattr(module, func_name)
91 return func
92
93+_punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+')
94+
95+def slugify(text, delim=u'-'):
96+ """
97+ Generates an ASCII-only slug. Taken from http://flask.pocoo.org/snippets/5/
98+ """
99+ result = []
100+ for word in _punct_re.split(text.lower()):
101+ word = word.encode('translit/long')
102+ if word:
103+ result.append(word)
104+ return unicode(delim.join(result))
105
106 ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107 ### Special email test stuff begins HERE
108diff --git a/mediagoblin/views.py b/mediagoblin/views.py
109index 95d0be7..7f925bb 100644
110--- a/mediagoblin/views.py
111+++ b/mediagoblin/views.py
112@@ -20,6 +20,7 @@ from webob import Response, exc
113 import wtforms
114 from mongokit import ObjectId
115 from mediagoblin import models
116+import gettext
117
118 def root_view(request):
119 media_entries = request.db.MediaEntry.find(
120diff --git a/setup.py b/setup.py
121index 7d38e52..1fb2414 100644
122--- a/setup.py
123+++ b/setup.py
124@@ -38,6 +38,7 @@ setup(
125 'jinja2',
126 'sphinx',
127 'PIL',
128+ 'translitcodec',
129 'Babel',
130 ],
131 test_suite='nose.collector',