# HG changeset patch # User Thierry Florac # Date 1505129339 -7200 # Node ID 3b2cb5046d8666e4e5191cfcf227f61f066c51ae # Parent 79afa48cc2aacdb2f965fbba3e694ab94ab84f4b Updated files and images widgets diff -r 79afa48cc2aa -r 3b2cb5046d86 src/pyams_file/widget/__init__.py --- a/src/pyams_file/widget/__init__.py Mon Sep 11 13:28:17 2017 +0200 +++ b/src/pyams_file/widget/__init__.py Mon Sep 11 13:28:59 2017 +0200 @@ -14,17 +14,21 @@ # import standard library +import os from cgi import FieldStorage # import interfaces from pyams_file.interfaces import IFileField, IFileWidget, IImageField, IImageWidget, \ - IThumbnailImageWidget, IThumbnailImageField, DELETED_FILE + IThumbnailImageWidget, IThumbnailImageField, DELETED_FILE, IThumbnail from pyams_form.interfaces.form import IFormLayer from z3c.form.interfaces import NOT_CHANGED, IFieldWidget, IDataConverter # import packages +from pyams_file.file import EXTENSIONS_THUMBNAILS from pyams_form.widget import widgettemplate_config from pyams_utils.adapter import adapter_config +from pyams_utils.size import get_human_size +from pyams_utils.url import absolute_url from z3c.form.browser.file import FileWidget as BaseFileWidget from z3c.form.converter import BaseDataConverter from z3c.form.util import toBytes @@ -54,6 +58,10 @@ return toBytes(value) +# +# File widgets +# + @widgettemplate_config(mode='input', template='templates/file-input.pt', widget=IFileWidget, layer=IFormLayer) @widgettemplate_config(mode='display', template='templates/file-display.pt', widget=IFileWidget, layer=IFormLayer) @implementer_only(IFileWidget) @@ -83,12 +91,28 @@ value = self.value return bool(value) + def get_human_size(self, value): + return get_human_size(value, self.request) + + def get_thumbnail(self): + thumbnails = IThumbnail(self.current_value, None) + if thumbnails is not None: + display = thumbnails.get_thumbnail('128x128', 'jpeg') + if display is not None: + return absolute_url(display, self.request) + name, ext = os.path.splitext(self.current_value.filename) + return '/--static--/pyams_skin/img/mimetypes/{0}'.format(EXTENSIONS_THUMBNAILS.get(ext, 'unknown.png')) + @adapter_config(context=(IFileField, IFormLayer), provides=IFieldWidget) def FileFieldWidget(field, request): return FieldWidget(field, FileWidget(request)) +# +# Image widgets +# + @widgettemplate_config(mode='input', template='templates/image-input.pt', widget=IImageWidget, layer=IFormLayer) @widgettemplate_config(mode='display', template='templates/image-display.pt', widget=IImageWidget, layer=IFormLayer) @implementer_only(IImageWidget) diff -r 79afa48cc2aa -r 3b2cb5046d86 src/pyams_file/widget/templates/file-display.pt --- a/src/pyams_file/widget/templates/file-display.pt Mon Sep 11 13:28:17 2017 +0200 +++ b/src/pyams_file/widget/templates/file-display.pt Mon Sep 11 13:28:59 2017 +0200 @@ -2,11 +2,15 @@
+
+ +