Commit 88152e6e authored by Laurent Bachelier's avatar Laurent Bachelier

Make it more pythonic

parent e077b0d4
......@@ -72,6 +72,14 @@ class File(IObject):
self.perms = {}
IObject.__init__(self, storage)
def to_class(self, cls):
"""
Convert the File object to another class.
That class should inherit from File.
This is useful for plugins.
"""
return cls(self.storage, self.path)
def __str__(self):
return self.path
......
......@@ -27,6 +27,7 @@ from paste.url import URL
from ass2m.plugin import Plugin
from ass2m.routes import View
from ass2m.server import ViewAction, FileApp
from ass2m.files import File
__all__ = ['GalleryPlugin']
......@@ -77,34 +78,25 @@ class DownloadThumbnailAction(ViewAction):
CONTENT_DISPOSITION.apply(self.ctx.res.headers, inline=True,
filename="%s_thumb_%s.%s" % (os.path.splitext(f.get_name())[0], size, thumbext))
class Media:
__file = None
def __init__(self, f):
self.__file = f
def get_name(self):
return self.__file.get_name()
def get_pretty_name(self):
return self.__file.get_pretty_name()
def get_mimetype(self):
return self.__file.get_mimetype()
class Media(File):
def get_url(self):
return URL(self.get_name())
def get_thumb_url(self):
if self.get_mimetype() == 'image/svg+xml':
if self.is_vector():
return self.get_url()
return self.get_url().setvars(view='thumbnail', thumb_size=200)
def is_vector(self):
return self.get_mimetype() == 'image/svg+xml'
def get_extra_classes(self):
if self.get_mimetype() == 'image/svg+xml':
return ' svg'
if self.is_vector():
return ' vector'
return ''
class MediaListAction(ViewAction):
IS_MEDIA = True
......@@ -122,7 +114,8 @@ class MediaListAction(ViewAction):
filename = f.get_name()
mimetype = f.get_mimetype()
if self.IS_MEDIA and mimetype is not None and mimetype.startswith('image'):
thumbs.append(Media(f))
m = f.to_class(Media)
thumbs.append(m)
continue
if filename in ('README', 'README.html', 'HEADER', 'HEADER.html'):
with open(f.get_realpath(), 'r') as fp:
......@@ -139,6 +132,7 @@ class MediaListAction(ViewAction):
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(
......
......@@ -207,7 +207,7 @@ img {
border: 0;
}
img.svg {
img.vector {
width: 200px;
height: 200px;
}
......
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