author | Philippe Pepiot <philippe.pepiot@logilab.fr> |
Tue, 19 Mar 2019 13:17:47 +0100 | |
changeset 12519 | aff5d3498f68 |
permissions | -rw-r--r-- |
12519
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
1 |
import os |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
2 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
3 |
from cubicweb.server import hook |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
4 |
from cubicweb.predicates import is_instance |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
5 |
from cubicweb.entities import adapters |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
6 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
7 |
from cubicweb_file.entities import UnResizeable |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
8 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
9 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
10 |
class UpdateFileHook(hook.Hook): |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
11 |
"""a file has been updated, check data_format/data_encoding consistency |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
12 |
""" |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
13 |
__regid__ = 'updatefilehook' |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
14 |
__select__ = hook.Hook.__select__ & is_instance('File') |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
15 |
events = ('before_add_entity', 'before_update_entity',) |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
16 |
order = -1 # should be run before other hooks |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
17 |
category = 'hash' |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
18 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
19 |
def __call__(self): |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
20 |
edited = self.entity.cw_edited |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
21 |
if 'data' in edited: |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
22 |
self.entity.set_format_and_encoding() |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
23 |
maxsize = None |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
24 |
if maxsize and self.entity.data_format.startswith('image/'): |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
25 |
iimage = self.entity.cw_adapt_to('IImage') |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
26 |
try: |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
27 |
edited['data'] = iimage.resize(maxsize) |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
28 |
except UnResizeable: |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
29 |
# if the resize fails for some reason, do nothing |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
30 |
# (original image will be stored) |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
31 |
pass |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
32 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
33 |
# thumbnail cache invalidation |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
34 |
if 'update' in self.event and 'data' in edited: |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
35 |
thumb = self.entity.cw_adapt_to('IThumbnail') |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
36 |
if not thumb: |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
37 |
return |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
38 |
thumbpath = thumb.thumbnail_path() |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
39 |
if thumbpath: |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
40 |
try: |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
41 |
os.unlink(thumbpath) |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
42 |
except Exception as exc: |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
43 |
self.warning( |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
44 |
'could not invalidate thumbnail file `%s` ' |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
45 |
'(cause: %s)', |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
46 |
thumbpath, exc) |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
47 |
|
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 |
class FileIDownloadableAdapter(adapters.IDownloadableAdapter): |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
50 |
__select__ = is_instance('File') |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
51 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
52 |
# IDownloadable |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
53 |
def download_url(self, **kwargs): |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
54 |
# include filename in download url for nicer url |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
55 |
name = self._cw.url_quote(self.download_file_name()) |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
56 |
path = '%s/raw/%s' % (self.entity.rest_path(), name) |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
57 |
return self._cw.build_url(path, **kwargs) |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
58 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
59 |
def download_content_type(self): |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
60 |
return self.entity.data_format |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
61 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
62 |
def download_encoding(self): |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
63 |
return self.entity.data_encoding |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
64 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
65 |
def download_file_name(self): |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
66 |
return self.entity.data_name |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
67 |
|
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
68 |
def download_data(self): |
aff5d3498f68
[web/test] drop dependency on third party cubes
Philippe Pepiot <philippe.pepiot@logilab.fr>
parents:
diff
changeset
|
69 |
return self.entity.data.getvalue() |