Trying to give album url natural name

This commit is contained in:
tanshu 2015-06-14 17:22:58 +05:30
parent d586e8fe86
commit 82313b8ec8
2 changed files with 20 additions and 7 deletions

View File

@ -76,6 +76,12 @@ class Album(Base):
id = uuid.UUID(id)
return DBSession.query(cls).filter(cls.id == id).first()
@classmethod
def by_name(cls, name):
if not name:
return None
return DBSession.query(cls).filter(cls.name.ilike(name)).first()
@classmethod
def menu_item(cls):
return uuid.UUID('dad46805-f577-4e5b-8073-9b788e0173fc')

View File

@ -5,6 +5,7 @@ from pyramid.response import FileResponse, Response
from pyramid.security import authenticated_userid
from pyramid.view import view_config
import re
import transaction
from soter.models import DBSession
@ -85,8 +86,13 @@ def show_list(request):
@view_config(request_method='GET', route_name='api_album_id', renderer='json', request_param='pictures',
permission='Albums')
def show_pics(request):
album = Album.by_id(uuid.UUID(request.matchdict['id']))
info = album_info(uuid.UUID(request.matchdict['id']))
id = request.matchdict['id']
p = re.compile('^[A-Fa-f0-9]{8}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{4}-[A-Fa-f0-9]{12}$')
if p.match(id):
album = Album.by_id(uuid.UUID(id))
else:
album = Album.by_name(id)
info = album_info(album)
for item in album.pictures:
info['pictures'].append({'name': item.name, 'url': request.route_url('api_picture_id', id=item.id)})
return info
@ -94,10 +100,11 @@ def show_pics(request):
def album_info(id):
if id is None:
album = {'name': "", 'description': "", 'isPublic': False, 'pictures': []}
else:
return {'name': "", 'description': "", 'isPublic': False, 'pictures': []}
if not isinstance(id, Album):
album = Album.by_id(id)
album = {'id': album.id, 'name': album.name, 'description': album.description, 'isPublic': album.is_public,
'pictures': [], 'user': {'id': album.user_id, 'name': album.user.name},
'isFixture': album.is_fixture}
album = {'id': album.id, 'name': album.name, 'description': album.description, 'isPublic': album.is_public,
'pictures': [], 'user': {'id': album.user_id, 'name': album.user.name},
'isFixture': album.is_fixture}
return album