Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Base64Converter for file/image widgets on ASCII fields
Add Base64 data converter for NamedImage and NamedFile widgets on ASCII fields with base64 encoded data and filename. Now the NamedImage and NamedFile widgets can be used with ``zope.schema.ASCII`` fields.
- Loading branch information
Showing
5 changed files
with
236 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,15 @@ | ||
from ZPublisher.HTTPRequest import FileUpload | ||
from plone.formwidget.namedfile.interfaces import INamedFileWidget | ||
from plone.namedfile.interfaces import INamedField, INamed | ||
from plone.formwidget.namedfile.interfaces import INamedImageWidget | ||
from plone.namedfile.file import NamedFile | ||
from plone.namedfile.file import NamedImage | ||
from plone.namedfile.interfaces import INamed | ||
from plone.namedfile.interfaces import INamedField | ||
from plone.namedfile.utils import safe_basename | ||
from z3c.form.converter import BaseDataConverter | ||
from zope.component import adapts | ||
from zope.schema.interfaces import IASCII | ||
import base64 | ||
|
||
|
||
class NamedDataConverter(BaseDataConverter): | ||
|
@@ -39,3 +45,67 @@ def toFieldValue(self, value): | |
|
||
else: | ||
return self.field._type(data=str(value)) | ||
|
||
|
||
def b64encode_file(filename, data): | ||
# encode filename and data using the standard alphabet, so that ";" can be | ||
# used as delimiter. | ||
if isinstance(filename, unicode): | ||
filename = filename.encode('utf-8') | ||
filenameb64 = base64.standard_b64encode(filename or '') | ||
datab64 = base64.standard_b64encode(data) | ||
return "filenameb64:{};datab64:{}".format( | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
davisagli
Member
|
||
filenameb64, datab64 | ||
).encode('ascii') | ||
|
||
|
||
def b64decode_file(value): | ||
filename, data = value.split(';') | ||
|
||
filename = filename.split(':')[1] | ||
filename = base64.standard_b64decode(filename) | ||
filename = filename.decode('utf-8') | ||
|
||
data = data.split(':')[1] | ||
data = base64.standard_b64decode(data) | ||
|
||
return filename, data | ||
|
||
|
||
class Base64Converter(BaseDataConverter): | ||
"""Converts between ASCII fields with base64 encoded data and a filename | ||
and INamedImage/INamedFile values. | ||
""" | ||
adapts(IASCII, INamedFileWidget) | ||
|
||
def toWidgetValue(self, value): | ||
|
||
if not isinstance(value, basestring): | ||
return None | ||
|
||
filename, data = b64decode_file(value) | ||
|
||
if INamedImageWidget.providedBy(self.widget): | ||
value = NamedImage(data=data, filename=filename) | ||
else: | ||
value = NamedFile(data=data, filename=filename) | ||
return value | ||
|
||
def toFieldValue(self, value): | ||
|
||
filename = None | ||
data = None | ||
|
||
if INamed.providedBy(value): | ||
filename = value.filename | ||
data = value.data | ||
|
||
elif isinstance(value, FileUpload): | ||
filename = safe_basename(value.filename) | ||
value.seek(0) | ||
data = value.read() | ||
|
||
if not data: | ||
return self.field.missing_value | ||
|
||
return b64encode_file(filename, data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@thet This breaks the Plone 4.3 tests for Python 2.6 because there is no "format()" in Python 2.6.