Generate unique slugs for newly submitted images.
This commit is contained in:
parent
a8e2812b05
commit
0546833c6e
@ -18,8 +18,9 @@ import datetime, uuid
|
|||||||
|
|
||||||
from mongokit import Document, Set
|
from mongokit import Document, Set
|
||||||
|
|
||||||
|
from mediagoblin import util
|
||||||
from mediagoblin.auth import lib as auth_lib
|
from mediagoblin.auth import lib as auth_lib
|
||||||
|
from mediagoblin import globals as mediagoblin_globals
|
||||||
|
|
||||||
###################
|
###################
|
||||||
# Custom validators
|
# Custom validators
|
||||||
@ -66,6 +67,7 @@ class MediaEntry(Document):
|
|||||||
structure = {
|
structure = {
|
||||||
'uploader': User,
|
'uploader': User,
|
||||||
'title': unicode,
|
'title': unicode,
|
||||||
|
'slug':unicode,
|
||||||
'created': datetime.datetime,
|
'created': datetime.datetime,
|
||||||
'description': unicode,
|
'description': unicode,
|
||||||
'media_type': unicode,
|
'media_type': unicode,
|
||||||
@ -98,6 +100,13 @@ class MediaEntry(Document):
|
|||||||
def main_mediafile(self):
|
def main_mediafile(self):
|
||||||
pass
|
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]
|
REGISTER_MODELS = [MediaEntry, User]
|
||||||
|
|
||||||
|
@ -52,6 +52,9 @@ def submit_start(request):
|
|||||||
# it to generate the file path
|
# it to generate the file path
|
||||||
entry.save(validate=False)
|
entry.save(validate=False)
|
||||||
|
|
||||||
|
# Generate a slug from the title
|
||||||
|
entry.generate_slug()
|
||||||
|
|
||||||
# Now store generate the queueing related filename
|
# Now store generate the queueing related filename
|
||||||
queue_filepath = request.app.queue_store.get_unique_filepath(
|
queue_filepath = request.app.queue_store.get_unique_filepath(
|
||||||
['media_entries',
|
['media_entries',
|
||||||
|
@ -70,6 +70,14 @@ I hope you like unit tests JUST AS MUCH AS I DO!"""
|
|||||||
|
|
||||||
I hope you like unit tests JUST AS MUCH AS I DO!"""
|
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():
|
def test_locale_to_lower_upper():
|
||||||
"""
|
"""
|
||||||
|
@ -19,9 +19,10 @@ import gettext
|
|||||||
import pkg_resources
|
import pkg_resources
|
||||||
import smtplib
|
import smtplib
|
||||||
import sys
|
import sys
|
||||||
|
import re
|
||||||
import jinja2
|
import jinja2
|
||||||
import mongokit
|
import mongokit
|
||||||
|
import translitcodec
|
||||||
|
|
||||||
from mediagoblin import globals as mgoblin_globals
|
from mediagoblin import globals as mgoblin_globals
|
||||||
|
|
||||||
@ -107,6 +108,18 @@ def import_component(import_string):
|
|||||||
func = getattr(module, func_name)
|
func = getattr(module, func_name)
|
||||||
return func
|
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
|
### Special email test stuff begins HERE
|
||||||
|
@ -20,6 +20,7 @@ from webob import Response, exc
|
|||||||
import wtforms
|
import wtforms
|
||||||
from mongokit import ObjectId
|
from mongokit import ObjectId
|
||||||
from mediagoblin import models
|
from mediagoblin import models
|
||||||
|
import gettext
|
||||||
|
|
||||||
def root_view(request):
|
def root_view(request):
|
||||||
media_entries = request.db.MediaEntry.find(
|
media_entries = request.db.MediaEntry.find(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user