Esempio n. 1
0
File: album.py Progetto: dbaty/Lasco
def album_index(request):
    """An index page for an album """
    session = DBSession()
    gallery_name = request.matchdict['gallery_name']
    album_name = request.matchdict['album_name']
    page = int(request.GET.get('page', 1))
    try:
        gallery = session.query(Gallery).filter_by(name=gallery_name).one()
        album = session.query(Album).filter_by(gallery_id=gallery.id,
                                               name=album_name).one()
    except NoResultFound:
        raise HTTPNotFound(request.url)
    if not get_user_role(request, session, gallery, album):
        raise HTTPForbidden()
    pictures = sorted(album.pictures, key=lambda p: p.date)
    settings = request.registry.settings
    pictures = Batch(pictures,
                     batch_length=int(settings['lasco.pictures_per_page']),
                     current=page)
    api = TemplateAPI(request,
                      '%s - %s' % (gallery.title, album.title))
    url_of_page = '%s?page=%%s' % request.route_url(
        'album',
        gallery_name=gallery.name, album_name=album.name)
    if pictures.previous:
        api.previous_url = url_of_page % pictures.previous
    if pictures.next:
        api.next_url = url_of_page % pictures.next
    return {'api': api,
            'gallery': gallery,
            'album': album,
            'pictures': pictures,
            'url_of_page': url_of_page}
Esempio n. 2
0
def picture_in_album(request):
    session = DBSession()
    gallery_name = request.matchdict['gallery_name']
    album_name = request.matchdict['album_name']
    try:
        gallery = session.query(Gallery).filter_by(name=gallery_name).one()
        album = session.query(Album).filter_by(gallery_id=gallery.id,
                                               name=album_name).one()
    except NoResultFound:
        raise HTTPNotFound(request.url)
    role = get_user_role(request, session, gallery, album)
    if not role:
        raise HTTPForbidden()

    can_edit = role == ROLE_GALLERY_ADMIN
    pictures = sorted(album.pictures, key=lambda p: p.date)
    picture_index = None
    picture_id = int(request.matchdict['picture_id'])
    previous_id = next_id = None
    for i in range(len(pictures)):
        i_id = pictures[i].id
        if i_id == picture_id:
            picture_index = i
            break
        previous_id = i_id
    if picture_index is None:
        raise HTTPNotFound(request.url)
    picture = pictures[picture_index]
    if picture_index != len(pictures) - 1:
        next_id = pictures[picture_index + 1].id

    api = TemplateAPI(request,
                      '%s - %s' % (gallery.title, album.title))
    if previous_id:
        api.previous_url = request.route_url(
            'picture_in_album',
            gallery_name=gallery_name, album_name=album_name,
            picture_id=previous_id)
    if next_id:
        api.next_url = request.route_url(
            'picture_in_album',
            gallery_name=gallery_name, album_name=album_name,
            picture_id=next_id)
    return {'api': api,
            'gallery': gallery,
            'album': album,
            'picture': picture,
            'picture_index': picture_index,
            'previous_id': previous_id,
            'next_id': next_id,
            'can_edit': can_edit}
Esempio n. 3
0
def not_found(request):
    api = TemplateAPI(request, _(u'Page not found'))
    api.show_login_link = False
    request.response.status = 404
    return {'api': api,
            'resource_url': request.url}