Commit 6aa4cac3 authored by Laurent Bachelier's avatar Laurent Bachelier

Restore the "plain list" view

And fix tests
parent c5b672cf
......@@ -30,6 +30,7 @@ from ass2m.files import File
from ass2m.routes import View
from ass2m.server import ViewAction, FileApp
from .cleanup import ICleaner
from .gallery import MediaListAction
__all__ = ['CorePlugin']
......@@ -204,37 +205,8 @@ class DownloadAction(ViewAction):
inline=True, filename=self.ctx.file.get_name())
class ListAction(ViewAction):
def get(self):
dirs = []
files = []
thumbs = []
self.ctx.template_vars['header_text'] = None
self.ctx.template_vars['readme_text'] = None
for f in self.ctx.iter_files():
if f.isdir():
dirs.append(f)
continue
if self.ctx.user.has_perms(f, f.PERM_READ):
filename = f.get_name()
mimetype = f.get_mimetype()
if mimetype is not None and mimetype.startswith('image'):
thumbs.append(f)
continue
if filename in ('README', 'README.html', 'HEADER', 'HEADER.html'):
with open(f.get_realpath(), 'r') as fp:
text = fp.read()
if not filename.endswith('.html'):
text = html_escape(text.decode('utf-8'))
self.ctx.template_vars['%s_text' % filename.split('.')[0].lower()] = text
continue
files.append(f)
self.ctx.template_vars['thumbs'] = thumbs
self.ctx.template_vars['dirs'] = dirs
self.ctx.template_vars['files'] = files
self.ctx.template_vars['scripts'].append('list.js')
self.ctx.res.body = self.ctx.render('list.html')
class ListAction(MediaListAction):
IS_MEDIA = False
class CoreCleaner(ICleaner):
......@@ -291,6 +263,6 @@ class CorePlugin(Plugin):
DownloadAction, 1)
self.register_web_view(
View(object_type='directory', name='list', verbose_name='Detailed list'),
ListAction, 2)
ListAction, 0)
self.register_hook('cleanup', CoreCleaner)
......@@ -76,8 +76,46 @@ class DownloadThumbnailAction(ViewAction):
filename="%s_thumb_%s.%s" % (os.path.splitext(f.get_name())[0], size, thumbext))
class MediaListAction(ViewAction):
IS_MEDIA = True
def get(self):
dirs = []
files = []
thumbs = []
self.ctx.template_vars['header_text'] = None
self.ctx.template_vars['readme_text'] = None
for f in self.ctx.iter_files():
if f.isdir():
dirs.append(f)
continue
if self.ctx.user.has_perms(f, f.PERM_READ):
filename = f.get_name()
mimetype = f.get_mimetype()
if self.IS_MEDIA and mimetype is not None and mimetype.startswith('image'):
thumbs.append(f)
continue
if filename in ('README', 'README.html', 'HEADER', 'HEADER.html'):
with open(f.get_realpath(), 'r') as fp:
text = fp.read()
if not filename.endswith('.html'):
text = html_escape(text.decode('utf-8'))
self.ctx.template_vars['%s_text' % os.path.splitext(filename)[0].lower()] = text
continue
files.append(f)
self.ctx.template_vars['thumbs'] = thumbs
self.ctx.template_vars['dirs'] = dirs
self.ctx.template_vars['files'] = files
self.ctx.template_vars['scripts'].append('list.js')
self.ctx.res.body = self.ctx.render('list.html')
class GalleryPlugin(Plugin):
def init(self):
self.register_web_view(
View(object_type='file', mimetype='image', name='thumbnail'),
DownloadThumbnailAction, -1)
self.register_web_view(
View(object_type='directory', name='medialist', verbose_name='Media list'),
MediaListAction, 2)
......@@ -40,8 +40,8 @@ class AssetsTest(TestCase):
self.app.get('/?action=asset&file=../../.ass2m/config', status=412)
def test_stylesheets(self):
res = self.app.get('/', status=200)
res = self.app.get('/?view=list', status=200)
assert '<link rel="stylesheet" type="text/css" href="/?action=asset&amp;file=main.css" />' in res.body
res = self.app.get('/test_data/?view=gallery', status=200)
res = self.app.get('/test_data/?view=medialist', status=200)
assert '<link rel="stylesheet" type="text/css" href="/?action=asset&amp;file=main.css" />' in res.body
......@@ -39,20 +39,24 @@ class GalleryTest(TestCase):
shutil.rmtree(self.root)
def test_listAndDefaultView(self):
# default view is the medialist
res1 = self.app.get('/images/')
# change it to list
f = self.storage.get_file('/images')
f.view = 'list'
f.save()
res = self.app.get('/images/')
assert '<title>Index of /images/</title>' in res.body
assert '<img' not in res.body
assert 'nothing' in res.body
res1 = self.app.get('/images/?view=gallery')
f = self.storage.get_file('/images')
f.view = 'gallery'
f.save()
res2 = self.app.get('/images/')
# force medialist
res2 = self.app.get('/images/?view=medialist')
for res in (res1, res2):
assert '<title>Gallery of /images/</title>' in res.body
assert '<title>Index of /images/</title>' in res.body
assert '<img' in res.body
assert 'nothing' in res.body
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment