Mercurial > libervia-web
comparison libervia/pages/files/list/page_meta.py @ 1216:b2d067339de3
python 3 port:
/!\ Python 3.6+ is now needed to use libervia
/!\ instability may occur and features may not be working anymore, this will improve with time
/!\ TxJSONRPC dependency has been removed
The same procedure as in backend has been applied (check backend commit ab2696e34d29 logs
for details). Removed now deprecated code (Pyjamas compiled browser part, legacy blog,
JSON RPC related code).
Adapted code to work without `html` and `themes` dirs.
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 13 Aug 2019 19:12:31 +0200 |
parents | 29eb15062416 |
children | f511f8fbbf8a |
comparison
equal
deleted
inserted
replaced
1215:f14ab8a25e8b | 1216:b2d067339de3 |
---|---|
1 #!/usr/bin/env python2.7 | 1 #!/usr/bin/env python3 |
2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
3 | 3 |
4 from libervia.server.constants import Const as C | 4 from libervia.server.constants import Const as C |
5 from sat.core.i18n import _ | 5 from sat.core.i18n import _ |
6 from twisted.internet import defer | 6 from twisted.internet import defer |
12 import os | 12 import os |
13 | 13 |
14 log = getLogger(__name__) | 14 log = getLogger(__name__) |
15 """files handling pages""" | 15 """files handling pages""" |
16 | 16 |
17 name = u"files_list" | 17 name = "files_list" |
18 access = C.PAGES_ACCESS_PROFILE | 18 access = C.PAGES_ACCESS_PROFILE |
19 template = u"file/overview.html" | 19 template = "file/overview.html" |
20 | 20 |
21 | 21 |
22 def parse_url(self, request): | 22 def parse_url(self, request): |
23 self.getPathArgs(request, ["service", "*path"], min_args=1, service="jid", path="") | 23 self.getPathArgs(request, ["service", "*path"], min_args=1, service="jid", path="") |
24 | 24 |
26 @defer.inlineCallbacks | 26 @defer.inlineCallbacks |
27 def prepare_render(self, request): | 27 def prepare_render(self, request): |
28 data = self.getRData(request) | 28 data = self.getRData(request) |
29 thumb_limit = data.get("thumb_limit", 300) | 29 thumb_limit = data.get("thumb_limit", 300) |
30 template_data = request.template_data | 30 template_data = request.template_data |
31 service, path_elts = data[u"service"], data[u"path"] | 31 service, path_elts = data["service"], data["path"] |
32 path = u"/".join(path_elts) | 32 path = "/".join(path_elts) |
33 profile = self.getProfile(request) or C.SERVICE_PROFILE | 33 profile = self.getProfile(request) or C.SERVICE_PROFILE |
34 | 34 |
35 files_data = yield self.host.bridgeCall("FISList", service.full(), path, {}, profile) | 35 files_data = yield self.host.bridgeCall("FISList", service.full(), path, {}, profile) |
36 for file_data in files_data: | 36 for file_data in files_data: |
37 try: | 37 try: |
38 extra_raw = file_data[u"extra"] | 38 extra_raw = file_data["extra"] |
39 except KeyError: | 39 except KeyError: |
40 pass | 40 pass |
41 else: | 41 else: |
42 file_data[u"extra"] = json.loads(extra_raw) if extra_raw else {} | 42 file_data["extra"] = json.loads(extra_raw) if extra_raw else {} |
43 dir_path = path_elts + [file_data["name"]] | 43 dir_path = path_elts + [file_data["name"]] |
44 if file_data[u"type"] == C.FILE_TYPE_DIRECTORY: | 44 if file_data["type"] == C.FILE_TYPE_DIRECTORY: |
45 page = self | 45 page = self |
46 elif file_data[u"type"] == C.FILE_TYPE_FILE: | 46 elif file_data["type"] == C.FILE_TYPE_FILE: |
47 page = self.getPageByName("files_view") | 47 page = self.getPageByName("files_view") |
48 | 48 |
49 ## thumbnails ## | 49 ## thumbnails ## |
50 try: | 50 try: |
51 thumbnails = file_data[u"extra"]["thumbnails"] | 51 thumbnails = file_data["extra"]["thumbnails"] |
52 if not thumbnails: | 52 if not thumbnails: |
53 raise KeyError | 53 raise KeyError |
54 except KeyError: | 54 except KeyError: |
55 pass | 55 pass |
56 else: | 56 else: |
58 thumb = thumbnails[0] | 58 thumb = thumbnails[0] |
59 for thumb_data in thumbnails: | 59 for thumb_data in thumbnails: |
60 if thumb_data["size"][0] > thumb_limit: | 60 if thumb_data["size"][0] > thumb_limit: |
61 break | 61 break |
62 thumb = thumb_data | 62 thumb = thumb_data |
63 if u"url" in thumb: | 63 if "url" in thumb: |
64 file_data["thumb_url"] = thumb["url"] | 64 file_data["thumb_url"] = thumb["url"] |
65 elif u"id" in thumb: | 65 elif "id" in thumb: |
66 try: | 66 try: |
67 thumb_path = yield self.host.bridgeCall( | 67 thumb_path = yield self.host.bridgeCall( |
68 "bobGetFile", service.full(), thumb[u"id"], profile | 68 "bobGetFile", service.full(), thumb["id"], profile |
69 ) | 69 ) |
70 except Exception as e: | 70 except Exception as e: |
71 log.warning( | 71 log.warning( |
72 _(u"Can't retrieve thumbnail: {reason}").format(reason=e) | 72 _("Can't retrieve thumbnail: {reason}").format(reason=e) |
73 ) | 73 ) |
74 else: | 74 else: |
75 filename = os.path.basename(thumb_path) | 75 filename = os.path.basename(thumb_path) |
76 session_data = self.host.getSessionData( | 76 session_data = self.host.getSessionData( |
77 request, session_iface.ISATSession | 77 request, session_iface.ISATSession |
79 file_data["thumb_url"] = os.path.join( | 79 file_data["thumb_url"] = os.path.join( |
80 session_data.cache_dir, filename | 80 session_data.cache_dir, filename |
81 ) | 81 ) |
82 else: | 82 else: |
83 raise ValueError( | 83 raise ValueError( |
84 u"unexpected file type: {file_type}".format(file_type=file_data[u"type"]) | 84 "unexpected file type: {file_type}".format(file_type=file_data["type"]) |
85 ) | 85 ) |
86 file_data[u"url"] = page.getURL(service.full(), *dir_path) | 86 file_data["url"] = page.getURL(service.full(), *dir_path) |
87 | 87 |
88 ## comments ## | 88 ## comments ## |
89 comments_url = file_data.get(u"comments_url") | 89 comments_url = file_data.get("comments_url") |
90 if comments_url: | 90 if comments_url: |
91 parsed_url = uri.parseXMPPUri(comments_url) | 91 parsed_url = uri.parseXMPPUri(comments_url) |
92 comments_service = file_data[u"comments_service"] = parsed_url["path"] | 92 comments_service = file_data["comments_service"] = parsed_url["path"] |
93 comments_node = file_data[u"comments_node"] = parsed_url["node"] | 93 comments_node = file_data["comments_node"] = parsed_url["node"] |
94 try: | 94 try: |
95 comments_count = file_data[u"comments_count"] = int( | 95 comments_count = file_data["comments_count"] = int( |
96 file_data["comments_count"] | 96 file_data["comments_count"] |
97 ) | 97 ) |
98 except KeyError: | 98 except KeyError: |
99 comments_count = None | 99 comments_count = None |
100 if comments_count and data.get("retrieve_comments", False): | 100 if comments_count and data.get("retrieve_comments", False): |
101 file_data[u"comments"] = yield pages_tools.retrieveComments( | 101 file_data["comments"] = yield pages_tools.retrieveComments( |
102 self, comments_service, comments_node, profile=profile | 102 self, comments_service, comments_node, profile=profile |
103 ) | 103 ) |
104 | 104 |
105 template_data[u"files_data"] = files_data | 105 template_data["files_data"] = files_data |
106 template_data[u"path"] = path | 106 template_data["path"] = path |
107 if path_elts: | 107 if path_elts: |
108 template_data[u"parent_url"] = self.getURL(service.full(), *path_elts[:-1]) | 108 template_data["parent_url"] = self.getURL(service.full(), *path_elts[:-1]) |