示例#1
0
文件: views.py 项目: b7w/bviewer
def images_view(request, gallery_id, album_id):
    gallery = Gallery.objects.safe_get(id=gallery_id, user=request.user)
    if not gallery:
        raise Http404()

    controller = AlbumController(gallery, request.user, uid=album_id)
    if not controller.exists():
        return message_view(request, message='No such album')

    images = controller.get_images()
    storage = ImageStorage(gallery)
    path = request.GET.get('p', '')
    try:
        image_paths = storage.list(path, saved_images=images)
        folder = ImageFolder(path, image_paths)
    except FileError as e:
        logger.exception(e)
        return message_view(request, message=smart_text(e))
    return render(
        request, 'profile/images.html', {
            'gallery': gallery,
            'album': controller.get_object(),
            'folder': folder,
            'title': 'Select images',
        })
示例#2
0
def index_view(request):
    """
    Slideshow, create new for each session
    """
    gallery = get_gallery(request)
    if not gallery:
        return message_view(request, message=GALLERY_NOT_FOUND)

    controller = AlbumController(gallery,
                                 request.user,
                                 uid=gallery.top_album_id)
    if not controller.exists():
        return message_view(request, message='No such album')

    main = controller.get_object()
    link = reverse('actions-slideshow-get-or-create') + '?album={0}'.format(
        gallery.top_album_id)
    return render(
        request, 'slideshow/index.html', {
            'gallery': gallery,
            'main': main,
            'link': link,
            'back': dict(album_id=main.id,
                         home=gallery.top_album_id == main.id),
        })
示例#3
0
文件: views.py 项目: b7w/bviewer
def album_pre_cache(request, gallery_id, album_id):
    gallery = Gallery.objects.safe_get(id=gallery_id, user=request.user)
    if not gallery:
        raise Http404()

    controller = AlbumController(gallery, request.user, uid=album_id)
    if not controller.exists():
        return message_view(request, message='No such album')

    try:
        controller.pre_cache()
    except FileError as e:
        logger.exception(e)
        return message_view(request, message=smart_text(e))
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
示例#4
0
    def test_album_controller_from_obj(self):
        """
        Tests AlbumController.from_obj
        """
        gallery = self.data.gallery_b7w
        top_album = gallery.top_album

        controller = AlbumController(gallery, gallery.user, uid=top_album.id)
        self.assertEqual(controller.uid, top_album.id)
        self.assertEqual(controller.get_object(), top_album)

        controller = AlbumController.from_obj(top_album)
        self.assertEqual(controller.gallery, gallery)
        self.assertEqual(controller.user, self.data.user_b7w)
        self.assertEqual(controller.uid, top_album.id)
        self.assertEqual(controller.get_object(), top_album)
示例#5
0
def download_view(request, gid, uid):
    """
    Download archive
    """
    gallery = get_gallery(request)
    if not gallery:
        raise Http404(GALLERY_NOT_FOUND)

    controller = AlbumController(gallery, request.user, uid=gid)
    if not controller.exists():
        raise Http404(ALBUM_NOT_FOUND)

    if not controller.is_album():
        return message_view(request, message=NOT_ALBUM)

    if not controller.is_archiving_allowed():
        return message_view(request, message=NOT_ALLOW_ARCHIVING)

    image_paths = [i.path for i in controller.get_images()]
    z = ZipArchiveController(image_paths, gallery, name=uid)

    if z == 'NONE':
        raise Http404('No file found')

    main = controller.get_object()
    logger.info(smart_text('download archive "%s"'), main.title)
    name = smart_text('{0} - {1}.zip').format(main.time.strftime('%Y-%m-%d'),
                                              main.title)
    return download_response(z.archive, name=name)
示例#6
0
    def test_album_controller_params(self):
        data = TestData().load_users().load_galleries()
        gallery = data.gallery_b7w
        top_album = gallery.top_album

        controller = AlbumController(gallery, gallery.user, uid=top_album.id)
        self.assertTrue(controller.is_owner())
        self.assertTrue(controller.user_has_access())

        controller = AlbumController(gallery, None, uid=top_album.id)
        self.assertFalse(controller.is_owner())
        self.assertFalse(controller.user_has_access())
示例#7
0
    def test_album_controller_all_sub_albums(self):
        """
        Tests AlbumController.get_all_sub_albums
        """
        data = TestData().load_users().load_galleries()

        gallery = data.gallery_b7w
        top_album = gallery.top_album
        g1 = Album.objects.create(parent=top_album, gallery=gallery, title='1')
        g2 = Album.objects.create(parent=top_album, gallery=gallery, title='2')

        g11 = Album.objects.create(parent=g1, gallery=gallery, title='1.1')
        g12 = Album.objects.create(parent=g1, gallery=gallery, title='1.2')
        g13 = Album.objects.create(parent=g1, gallery=gallery, title='1.3')

        g21 = Album.objects.create(parent=g2, gallery=gallery, title='2.1')
        g22 = Album.objects.create(parent=g2, gallery=gallery, title='2.2')

        g111 = Album.objects.create(parent=g11, gallery=gallery, title='1.1.1')
        g112 = Album.objects.create(parent=g11, gallery=gallery, title='1.1.2')

        g121 = Album.objects.create(parent=g12, gallery=gallery, title='1.2.1')

        controller = AlbumController(gallery, gallery.user, uid=top_album.id)
        albums = controller.get_all_sub_albums()
        self.assertEqual(len(albums), 10)

        controller = AlbumController(gallery, gallery.user, uid=top_album.id)
        albums = controller.get_all_sub_albums(parents=False)
        self.assertEqual(len(albums), 6)
示例#8
0
文件: admin.py 项目: b7w/bviewer
 def save_model(self, request, obj, form, change):
     thumbnail_id = form.data['thumbnail_id']
     if thumbnail_id != 'None':
         obj.thumbnail_id = thumbnail_id
     else:
         obj.thumbnail = None
     controller = AlbumController.from_obj(obj)
     # allow archiving
     if change and 'allow_archiving' in form.changed_data:
         controller.set_archiving(obj.allow_archiving)
     if change and 'gallery' in form.changed_data:
         controller.set_gallery(obj.gallery)
     super(ProfileAlbumAdmin, self).save_model(request, obj, form, change)
示例#9
0
def index_view(request, gid):
    """
    Start to archive images, or if done redirect to download
    js - waite while done, after redirect to download
    """
    gallery = get_gallery(request)
    if not gallery:
        return message_view(request, message=GALLERY_NOT_FOUND)

    controller = AlbumController(gallery, request.user, uid=gid)
    if not controller.exists():
        return message_view(request, message=ALBUM_NOT_FOUND)

    if not controller.is_album():
        return message_view(request, message=NOT_ALBUM)

    if not controller.is_archiving_allowed():
        return message_view(request, message=NOT_ALLOW_ARCHIVING)

    image_paths = [i.path for i in controller.get_images()]
    z = ZipArchiveController(image_paths, gallery)

    # links for redirect to download, and check status
    redirect = reverse('archive.download', kwargs=dict(gid=gid, uid=z.uid))
    link = reverse('archive.status', kwargs=dict(gid=gid, uid=z.uid))
    main = controller.get_object()

    if z.status == 'DONE':
        return HttpResponseRedirect(redirect)

    z.add_job()
    return render(
        request, 'archive/download.html', {
            'gallery': gallery,
            'path': request.path,
            'link': link,
            'redirect': redirect,
            'album': main,
            'back': dict(album_id=main.id),
        })
示例#10
0
def status_view(request, gid, uid):
    """
    Check if archive exists and ready for download
    """
    gallery = get_gallery(request)
    if not gallery:
        raise Http404(GALLERY_NOT_FOUND)

    controller = AlbumController(gallery, request.user, gid)
    if not controller.exists():
        return HttpResponse(json.dumps(dict(error=ALBUM_NOT_FOUND)))

    if not controller.is_album():
        return HttpResponse(json.dumps(dict(error=NOT_ALBUM)))

    if not controller.is_archiving_allowed():
        return HttpResponse(json.dumps(dict(error=NOT_ALLOW_ARCHIVING)))

    image_paths = [i.path for i in controller.get_images()]
    z = ZipArchiveController(image_paths, gallery, name=uid)
    data = dict(status=z.status, album=gid, uid=uid, progress=z.progress)

    return HttpResponse(json.dumps(data))
示例#11
0
    def test_album_controller_no_access(self):
        gallery = self.data.gallery_b7w
        album_id = gallery.top_album.id

        cnt = AlbumController(gallery, gallery.user, album_id)
        self.assertEqual(4, len(cnt.get_albums()))
        self.assertEqual(7, len(cnt.get_all_sub_albums()))

        cnt = AlbumController(gallery, self.data.user_keks, album_id)
        self.assertEqual(2, len(cnt.get_albums()))
        self.assertEqual(3, len(cnt.get_all_sub_albums()))

        # test hidden
        cnt = AlbumController(gallery, self.data.user_keks, self.album7.id)
        self.assertTrue(cnt.exists())

        # test privet
        cnt = AlbumController(gallery, self.data.user_keks,
                              self.data.album2.id)
        self.assertFalse(cnt.exists())
示例#12
0
    def test_album_controller_has_access(self):
        gallery = self.data.gallery_b7w
        album_id = gallery.top_album.id

        Access.objects.create(user=self.data.user_keks,
                              gallery=gallery,
                              is_active=True)
        cnt = AlbumController(gallery, gallery.user, album_id)
        self.assertEqual(4, len(cnt.get_albums()))
        self.assertEqual(7, len(cnt.get_all_sub_albums()))

        cnt = AlbumController(gallery, self.data.user_keks, album_id)
        self.assertEqual(4, len(cnt.get_albums()))
        self.assertEqual(7, len(cnt.get_all_sub_albums()))

        # test hidden
        cnt = AlbumController(gallery, self.data.user_keks, self.album7.id)
        self.assertTrue(cnt.exists())

        # test privet
        cnt = AlbumController(gallery, self.data.user_keks,
                              self.data.album2.id)
        self.assertTrue(cnt.exists())