Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
erp5
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Hardik Juneja
erp5
Commits
18958665
Commit
18958665
authored
Jun 03, 2016
by
Julien Muchembled
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
BT: review algorithm to chose extension when exporting a property to a separate file
parent
b6f3cb06
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
60 additions
and
73 deletions
+60
-73
product/ERP5/Document/BusinessTemplate.py
product/ERP5/Document/BusinessTemplate.py
+60
-73
No files found.
product/ERP5/Document/BusinessTemplate.py
View file @
18958665
...
@@ -102,8 +102,6 @@ try:
...
@@ -102,8 +102,6 @@ try:
except
TypeError
:
except
TypeError
:
pass
pass
cache_database
=
threading
.
local
()
cache_database
=
threading
.
local
()
from
mimetypes
import
guess_extension
from
mimetypes
import
MimeTypes
from
Products.MimetypesRegistry.common
import
MimeTypeException
from
Products.MimetypesRegistry.common
import
MimeTypeException
import
base64
import
base64
import
binascii
import
binascii
...
@@ -754,96 +752,85 @@ class ObjectTemplateItem(BaseTemplateItem):
...
@@ -754,96 +752,85 @@ class ObjectTemplateItem(BaseTemplateItem):
return
{
exported_property_type
:
extension
}
return
{
exported_property_type
:
extension
}
return
{}
return
{}
def
getMimetypesRegistryLookupResultOfContenType
(
self
,
content_type
):
def
_guessFilename
(
self
,
document
,
key
,
exported_property_type
):
"""Returns a lookup in mimetypes_registry based on the content_type"""
# Try to guess the extension based on the id of the document
mimetypes_registry
=
self
.
getPortalObject
().
mimetypes_registry
yield
key
# Try to guess the extension based on the reference of the document
if
hasattr
(
document
,
'getReference'
):
yield
document
.
getReference
()
elif
isinstance
(
document
,
ERP5BaseBroken
):
yield
getattr
(
document
,
"reference"
,
None
)
# Try to guess the extension based on the title of the document
yield
getattr
(
document
,
"title"
,
None
)
# Try to get type of image from its base64 encoding
if
exported_property_type
==
'data'
:
# XXX maybe decoding the whole file just for the extension is an overkill
data
=
str
(
document
.
__dict__
.
get
(
'data'
))
try
:
try
:
return
mimetypes_registry
.
lookup
(
content_type
)
decoded_data
=
base64
.
decodestring
(
data
)
except
MimeTypeException
:
except
binascii
.
Error
:
return
LOG
(
'BusinessTemplate '
,
0
,
'data is not base 64'
)
else
:
for
test
in
imghdr
.
tests
:
extension
=
test
(
decoded_data
,
None
)
if
extension
:
yield
'x.'
+
extension
def
guessExtensionOfDocument
(
self
,
document
,
key
,
exported_property_type
=
None
):
def
guessExtensionOfDocument
(
self
,
document
,
key
,
exported_property_type
=
None
):
"""Guesses and returns the extension of an ERP5 document.
"""Guesses and returns the extension of an ERP5 document.
The process followed is:
The process followed is:
1. Try to guess extension by content type
1. Try to guess extension by the id of the document
2. Try to guess extension by the id of the document
2. Try to guess extension by the title of the document
3. Try to guess extension by the title of the document
3. Try to guess extension by the reference of the document
4. Try to guess extension by the reference of the document
4. In the case of an image, try to guess extension by Base64 representation
5. In the case of an image, try to guess extension by Base64 representation
If there's a content type, we only return an extension that matches.
In case everything fails then:
In case everything fails then:
- '.bin' is returned for binary files
- '.bin' is returned for binary files
- '.txt' is returned for text
- '.txt' is returned for text
"""
"""
# Try to guess the extension based on the content_type of the document
# XXX Zope items like DTMLMethod would not implement getContentType method
# XXX Zope items like DTMLMethod would not
mime
=
None
# implement getContentType method
extension
=
None
binary
=
'not_identified'
content_type
=
None
if
hasattr
(
document
,
'getContentType'
):
if
hasattr
(
document
,
'getContentType'
):
content_type
=
document
.
getContentType
()
content_type
=
document
.
getContentType
()
elif
isinstance
(
document
,
ERP5BaseBroken
):
elif
isinstance
(
document
,
ERP5BaseBroken
):
content_type
=
getattr
(
document
,
"content_type"
,
None
)
content_type
=
getattr
(
document
,
"content_type"
,
None
)
else
:
content_type
=
None
# For stable export, people must have a MimeTypes Registry, so do not
# fallback on mimetypes. We prefer the mimetypes_registry because there
# are more extensions and we can have preferred extensions.
# See also https://bugs.python.org/issue1043134
mimetypes_registry
=
self
.
getPortalObject
()[
'mimetypes_registry'
]
if
content_type
:
if
content_type
:
lookup_result
=
self
.
getMimetypesRegistryLookupResultOfContenType
(
content_type
)
if
lookup_result
:
# return first registered Extension (if any)
if
lookup_result
[
0
].
extensions
:
extension
=
lookup_result
[
0
].
extensions
[
0
]
# If there is no extension return the first registered Glob (if any)
elif
lookup_result
[
0
].
globs
:
extension
=
str
(
lookup_result
[
0
].
globs
[
0
]).
replace
(
'*'
,
''
)
if
hasattr
(
lookup_result
[
0
],
'binary'
):
binary
=
lookup_result
[
0
].
binary
if
extension
:
if
not
extension
.
startswith
(
'.'
):
extension
=
'.'
+
extension
return
extension
# Try to guess the extension based on the id of the document
mime
=
MimeTypes
()
mime_type
=
mime
.
guess_type
(
key
)
if
mime_type
[
0
]:
extension
=
guess_extension
(
mime_type
[
0
])
return
extension
# Try to guess the extension based on the reference of the document
mime
=
MimeTypes
()
reference
=
None
if
hasattr
(
document
,
'getReference'
):
reference
=
document
.
getReference
()
elif
isinstance
(
document
,
ERP5BaseBroken
):
reference
=
getattr
(
document
,
"reference"
,
None
)
if
reference
:
mime_type
=
mime
.
guess_type
(
reference
)
if
mime_type
[
0
]:
extension
=
guess_extension
(
mime_type
[
0
])
return
extension
# Try to guess the extension based on the title of the document
title
=
getattr
(
document
,
"title"
,
None
)
if
title
:
mime_type
=
mime
.
guess_type
(
title
)
if
mime_type
[
0
]:
extension
=
guess_extension
(
mime_type
[
0
])
return
extension
# Try to get type of image from its base64 encoding
if
exported_property_type
==
'data'
:
# XXX maybe decoding the whole file just for the extension is an overkill
data
=
str
(
document
.
__dict__
.
get
(
'data'
))
try
:
try
:
decoded_data
=
base64
.
decodestring
(
data
)
mime
=
mimetypes_registry
.
lookup
(
content_type
)[
0
]
for
test
in
imghdr
.
tests
:
except
(
IndexError
,
MimeTypeException
):
extension
=
test
(
decoded_data
,
None
)
pass
if
extension
:
return
'.'
+
extension
for
key
in
self
.
_guessFilename
(
document
,
key
,
exported_property_type
):
except
binascii
.
Error
:
if
key
:
LOG
(
'BusinessTemplate '
,
0
,
'data is not base 64'
)
ext
=
os
.
path
.
splitext
(
key
)[
1
].
lower
()
if
ext
and
(
mimetypes_registry
.
lookupExtension
(
ext
[
1
:])
is
mime
if
mime
else
mimetypes_registry
.
lookupExtension
(
ext
[
1
:])):
return
ext
if
mime
:
# return first registered extension (if any)
if
mime
.
extensions
:
return
'.'
+
mime
.
extensions
[
0
]
for
ext
in
mime
.
globs
:
if
ext
[
0
]
==
"*"
and
ext
.
count
(
"."
)
==
1
:
return
ext
[
1
:].
encode
(
"utf-8"
)
# in case we could not read binary flag from mimetypes_registry then return
# in case we could not read binary flag from mimetypes_registry then return
# '.bin' for all the Portal Types where exported_property_type is data
# '.bin' for all the Portal Types where exported_property_type is data
# (File, Image, Spreadsheet). Otherwise, return .bin if binary was returned
# (File, Image, Spreadsheet). Otherwise, return .bin if binary was returned
# as 1.
# as 1.
if
(
binary
==
'not_identified'
and
exported_property_type
==
'data'
)
or
\
binary
=
getattr
(
mime
,
'binary'
,
None
)
binary
==
1
:
if
(
binary
is
None
and
exported_property_type
==
'data'
)
or
binary
:
return
'.bin'
return
'.bin'
# in all other cases return .txt
# in all other cases return .txt
return
'.txt'
return
'.txt'
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment