author | Philippe Pepiot <philippe.pepiot@logilab.fr> |
Tue, 19 Mar 2019 13:17:47 +0100 | |
changeset 12519 | aff5d3498f68 |
child 12567 | 26744ad37953 |
permissions | -rw-r--r-- |
12519
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
1 |
from six import text_type |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
2 |
from logilab.mtconverter import guess_mimetype_and_encoding |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
3 |
from cubicweb.entities import AnyEntity, fetch_config |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
4 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
5 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
6 |
class File(AnyEntity): |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
7 |
"""customized class for File entities""" |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
8 |
__regid__ = 'File' |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
9 |
fetch_attrs, cw_fetch_order = fetch_config(['data_name', 'title']) |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
10 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
11 |
def set_format_and_encoding(self): |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
12 |
"""try to set format and encoding according to known values (filename, |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
13 |
file content, format, encoding). |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
14 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
15 |
This method must be called in a before_[add|update]_entity hook else it |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
16 |
won't have any effect. |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
17 |
""" |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
18 |
assert 'data' in self.cw_edited, "missing mandatory attribute data" |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
19 |
if self.cw_edited.get('data'): |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
20 |
if (hasattr(self.data, 'filename') |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
21 |
and not self.cw_edited.get('data_name')): |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
22 |
self.cw_edited['data_name'] = self.data.filename |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
23 |
else: |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
24 |
self.cw_edited['data_format'] = None |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
25 |
self.cw_edited['data_encoding'] = None |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
26 |
self.cw_edited['data_name'] = None |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
27 |
return |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
28 |
if 'data_format' in self.cw_edited: |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
29 |
format = self.cw_edited.get('data_format') |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
30 |
else: |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
31 |
format = None |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
32 |
if 'data_encoding' in self.cw_edited: |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
33 |
encoding = self.cw_edited.get('data_encoding') |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
34 |
else: |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
35 |
encoding = None |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
36 |
if not (format and encoding): |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
37 |
format, encoding = guess_mimetype_and_encoding( |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
38 |
data=self.cw_edited.get('data'), |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
39 |
# use get and not get_value since data has changed, we only |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
40 |
# want to consider explicitly specified values, not old ones |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
41 |
filename=self.cw_edited.get('data_name'), |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
42 |
format=format, encoding=encoding, |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
43 |
fallbackencoding=self._cw.encoding) |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
44 |
if format: |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
45 |
self.cw_edited['data_format'] = text_type(format) |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
46 |
if encoding: |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
47 |
self.cw_edited['data_encoding'] = text_type(encoding) |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
48 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
49 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
50 |
class UnResizeable(Exception): |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
51 |
pass |