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
|
b
|
import datetime, uuid
|
18 | 18 | |
19 | 19 | from mongokit import Document, Set |
20 | 20 | |
| 21 | from mediagoblin import util |
21 | 22 | from mediagoblin.auth import lib as auth_lib |
22 | 23 | |
23 | 24 | |
… |
… |
class MediaEntry(Document):
|
66 | 67 | structure = { |
67 | 68 | 'uploader': User, |
68 | 69 | 'title': unicode, |
| 70 | 'slug': unicode, |
69 | 71 | 'created': datetime.datetime, |
70 | 72 | 'description': unicode, |
71 | 73 | 'media_type': unicode, |
… |
… |
class MediaEntry(Document):
|
98 | 100 | def main_mediafile(self): |
99 | 101 | pass |
100 | 102 | |
| 103 | def generate_slug(self): |
| 104 | self['slug'] = util.slugify(self['title']) |
101 | 105 | |
102 | 106 | REGISTER_MODELS = [MediaEntry, User] |
103 | 107 | |
diff --git a/mediagoblin/submit/views.py b/mediagoblin/submit/views.py
index 5e262f1..4e20cc6 100644
a
|
b
|
def submit_start(request):
|
46 | 46 | entry['media_type'] = u'image' # heh |
47 | 47 | entry['uploader'] = request.user |
48 | 48 | |
| 49 | # Generate a slug from the title |
| 50 | entry.generate_slug() |
| 51 | |
49 | 52 | # Save, just so we can get the entry id for the sake of using |
50 | 53 | # it to generate the file path |
51 | 54 | entry.save(validate=False) |
diff --git a/mediagoblin/tests/test_util.py b/mediagoblin/tests/test_util.py
index 5bc31fd..861e8b6 100644
a
|
b
|
I hope you like unit tests JUST AS MUCH AS I DO!"""
|
69 | 69 | assert mbox_message.get_payload(decode=True) == """HAYYY GUYS! |
70 | 70 | |
71 | 71 | I hope you like unit tests JUST AS MUCH AS I DO!""" |
| 72 | |
| 73 | def test_slugify(): |
| 74 | assert util.slugify('a walk in the park') == 'a-walk-in-the-park' |
| 75 | assert util.slugify('A Walk in the Park') == 'a-walk-in-the-park' |
| 76 | assert util.slugify('a walk in the park') == 'a-walk-in-the-park' |
| 77 | assert util.slugify('a walk in-the-park') == 'a-walk-in-the-park' |
| 78 | 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
|
b
|
|
17 | 17 | from email.MIMEText import MIMEText |
18 | 18 | import smtplib |
19 | 19 | import sys |
20 | | |
| 20 | import re |
21 | 21 | import jinja2 |
22 | 22 | import mongokit |
| 23 | import translitcodec |
23 | 24 | |
24 | 25 | from mediagoblin import globals as mgoblin_globals |
25 | 26 | |
… |
… |
def import_component(import_string):
|
87 | 88 | func = getattr(module, func_name) |
88 | 89 | return func |
89 | 90 | |
| 91 | _punct_re = re.compile(r'[\t !"#$%&\'()*\-/<=>?@\[\\\]^_`{|},.]+') |
| 92 | |
| 93 | def slugify(text, delim=u'-'): |
| 94 | """ |
| 95 | Generates an ASCII-only slug. Taken from http://flask.pocoo.org/snippets/5/ |
| 96 | """ |
| 97 | result = [] |
| 98 | for word in _punct_re.split(text.lower()): |
| 99 | word = word.encode('translit/long') |
| 100 | if word: |
| 101 | result.append(word) |
| 102 | return unicode(delim.join(result)) |
90 | 103 | |
91 | 104 | ### ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
92 | 105 | ### Special email test stuff begins HERE |
diff --git a/setup.py b/setup.py
index 7b483a5..283f9a2 100644
a
|
b
|
setup(
|
38 | 38 | 'jinja2', |
39 | 39 | 'sphinx', |
40 | 40 | 'PIL', |
| 41 | 'translitcodec', |
41 | 42 | ], |
42 | 43 | test_suite='nose.collector', |
43 | 44 | |