diff options
author | Elrond <elrond+mediagoblin.org@samba-tng.org> | 2012-02-19 12:14:13 +0100 |
---|---|---|
committer | Elrond <elrond+mediagoblin.org@samba-tng.org> | 2012-03-07 21:32:20 +0100 |
commit | 814334f6eb95de5521796f798104e216706fe81f (patch) | |
tree | 0f326aa555604b2713f1ad6c8bf3b1bb3eb79be8 /mediagoblin/db/mixin.py | |
parent | 74743bed0a600db13ffb98e9bfe0bffff17720ee (diff) | |
download | mediagoblin-814334f6eb95de5521796f798104e216706fe81f.tar.lz mediagoblin-814334f6eb95de5521796f798104e216706fe81f.tar.xz mediagoblin-814334f6eb95de5521796f798104e216706fe81f.zip |
Generic generate_slug for mongo and sql.
Using the new check_media_slug_used it is possible to have
one generic generate_slug in the mixin class instead of in
each db class.
In the sql variant self.id is not always set: If the slug
alone would create a dupe the current code decides for "no
slug at all".
Diffstat (limited to 'mediagoblin/db/mixin.py')
-rw-r--r-- | mediagoblin/db/mixin.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/mediagoblin/db/mixin.py b/mediagoblin/db/mixin.py index 758f7e72..4f9e1b11 100644 --- a/mediagoblin/db/mixin.py +++ b/mediagoblin/db/mixin.py @@ -27,9 +27,11 @@ These functions now live here and get "mixed in" into the real objects. """ +from mediagoblin import mg_globals from mediagoblin.auth import lib as auth_lib from mediagoblin.tools import common, licenses from mediagoblin.tools.text import cleaned_markdown_conversion +from mediagoblin.tools.url import slugify class UserMixin(object): @@ -46,6 +48,22 @@ class UserMixin(object): class MediaEntryMixin(object): + def generate_slug(self): + # import this here due to a cyclic import issue + # (db.models -> db.mixin -> db.util -> db.models) + from mediagoblin.db.util import check_media_slug_used + + self.slug = slugify(self.title) + + duplicate = check_media_slug_used(mg_globals.database, + self.uploader, self.slug, self.id) + + if duplicate: + if self.id is not None: + self.slug = "%s-%s" % (self.id, self.slug) + else: + self.slug = None + @property def description_html(self): """ |