From 7de20e52345949fa9d377f06ab0241d98063d9c9 Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Tue, 26 Feb 2013 13:38:11 -0600 Subject: [PATCH] Media URLs with ids in them are now like /u/cwebber/m/id:4112/ rather than /u/cwebber/m/4112/ This avoids some potential name collision issues. This commit sponsored by Asokan Pichai. Thank you! --- mediagoblin/db/mixin.py | 6 ++++-- mediagoblin/decorators.py | 39 +++++++++++++++++++++++---------------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/mediagoblin/db/mixin.py b/mediagoblin/db/mixin.py index b69e7d51..6789a970 100644 --- a/mediagoblin/db/mixin.py +++ b/mediagoblin/db/mixin.py @@ -150,8 +150,10 @@ class MediaEntryMixin(object): @property def slug_or_id(self): - return (self.slug or self.id) - + if self.slug: + return self.slug + else: + return u'id:%s' % self.id def url_for_self(self, urlgen, **extra_args): """ diff --git a/mediagoblin/decorators.py b/mediagoblin/decorators.py index 85883c1a..14b4f8fc 100644 --- a/mediagoblin/decorators.py +++ b/mediagoblin/decorators.py @@ -125,24 +125,31 @@ def get_user_media_entry(controller): if not user: raise NotFound() - media = MediaEntry.query.filter_by( - slug=request.matchdict['media'], - state=u'processed', - uploader=user.id).first() - - if not media: - # no media via slug? Grab it via object id - try: - media = MediaEntry.query.filter_by( - id = int(request.matchdict['media']), - state = u'processed', - uploader = user.id).first() - except ValueError: - # media "id" was no int - raise NotFound() + media = None + + # might not be a slug, might be an id, but whatever + media_slug = request.matchdict['media'] + + if u":" in request.matchdict['media']: + # okay, it's not actually a slug, it's some kind of identifier, + # probably id: + if media_slug.startswith(u'id:'): + try: + media = MediaEntry.query.filter_by( + id=int(media_slug[3:]), + state=u'processed', + uploader=user.id).first() + except ValueError: + raise NotFound() + else: + # no magical id: stuff? It's a slug! + media = MediaEntry.query.filter_by( + slug=request.matchdict['media'], + state=u'processed', + uploader=user.id).first() if not media: - # no media by that id? Okay, 404. + # Didn't find anything? Okay, 404. raise NotFound() return controller(request, media=media, *args, **kwargs) -- 2.25.1