from mongokit import Document, Set
+from mediagoblin import util
from mediagoblin.auth import lib as auth_lib
-
+from mediagoblin import globals as mediagoblin_globals
###################
# Custom validators
structure = {
'uploader': User,
'title': unicode,
+ 'slug':unicode,
'created': datetime.datetime,
'description': unicode,
'media_type': unicode,
def main_mediafile(self):
pass
+ def generate_slug(self):
+ self['slug'] = util.slugify(self['title'])
+
+ duplicate = mediagoblin_globals.database.media_entries.find_one({'slug': self['slug']})
+
+ if duplicate:
+ self['slug'] = "%s-%s" % (self['_id'], self['slug'])
REGISTER_MODELS = [MediaEntry, User]
# it to generate the file path
entry.save(validate=False)
+ # Generate a slug from the title
+ entry.generate_slug()
+
# Now store generate the queueing related filename
queue_filepath = request.app.queue_store.get_unique_filepath(
['media_entries',
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'
+ assert util.slugify(u'a walk in the par\u0107') == 'a-walk-in-the-parc'
+ assert util.slugify(u'\u00E0\u0042\u00E7\u010F\u00EB\u0066') == 'abcdef'
def test_locale_to_lower_upper():
"""
import pkg_resources
import smtplib
import sys
-
+import re
import jinja2
import mongokit
+import translitcodec
from mediagoblin import globals as mgoblin_globals
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
import wtforms
from mongokit import ObjectId
from mediagoblin import models
+import gettext
def root_view(request):
media_entries = request.db.MediaEntry.find(
'sphinx',
'PIL',
'Babel',
+ 'translitcodec',
],
test_suite='nose.collector',