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!
This commit is contained in:
Christopher Allan Webber 2013-02-26 13:38:11 -06:00
parent 397c22d139
commit 7de20e5234
2 changed files with 27 additions and 18 deletions

View File

@ -150,8 +150,10 @@ class MediaEntryMixin(object):
@property @property
def slug_or_id(self): 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): def url_for_self(self, urlgen, **extra_args):
""" """

View File

@ -125,24 +125,31 @@ def get_user_media_entry(controller):
if not user: if not user:
raise NotFound() raise NotFound()
media = MediaEntry.query.filter_by( media = None
slug=request.matchdict['media'],
state=u'processed', # might not be a slug, might be an id, but whatever
uploader=user.id).first() 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: if not media:
# no media via slug? Grab it via object id # Didn't find anything? Okay, 404.
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()
if not media:
# no media by that id? Okay, 404.
raise NotFound() raise NotFound()
return controller(request, media=media, *args, **kwargs) return controller(request, media=media, *args, **kwargs)