From 814334f6eb95de5521796f798104e216706fe81f Mon Sep 17 00:00:00 2001 From: Elrond Date: Sun, 19 Feb 2012 12:14:13 +0100 Subject: [PATCH] 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". --- mediagoblin/db/mixin.py | 18 ++++++++++++++++++ mediagoblin/db/mongo/models.py | 11 ----------- 2 files changed, 18 insertions(+), 11 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): """ diff --git a/mediagoblin/db/mongo/models.py b/mediagoblin/db/mongo/models.py index c86adbb6..2e35a2b8 100644 --- a/mediagoblin/db/mongo/models.py +++ b/mediagoblin/db/mongo/models.py @@ -18,11 +18,9 @@ import datetime from mongokit import Document -from mediagoblin import mg_globals from mediagoblin.db.mongo import migrations from mediagoblin.db.mongo.util import ASCENDING, DESCENDING, ObjectId from mediagoblin.tools.pagination import Pagination -from mediagoblin.tools import url from mediagoblin.db.mixin import UserMixin, MediaEntryMixin, MediaCommentMixin @@ -232,15 +230,6 @@ class MediaEntry(Document, MediaEntryMixin): return self.db.MediaComment.find({ 'media_entry': self._id}).sort('created', order) - def generate_slug(self): - self.slug = url.slugify(self.title) - - duplicate = mg_globals.database.media_entries.find_one( - {'slug': self.slug}) - - if duplicate: - self.slug = "%s-%s" % (self._id, self.slug) - def url_to_prev(self, urlgen): """ Provide a url to the previous entry from this user, if there is one -- 2.25.1