Mercurial > libervia-web
annotate libervia/pages/files/list/page_meta.py @ 1290:ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 19 Jun 2020 16:47:51 +0200 |
parents | c14ac2502011 |
children | 12aa95eeb409 |
rev | line source |
---|---|
1216 | 1 #!/usr/bin/env python3 |
1239 | 2 |
1290
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
3 import json |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
4 import os |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
5 from twisted.internet import defer |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
6 from sat.core.i18n import _ |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
7 from sat.core.log import getLogger |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
8 from sat.tools.common import uri |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
9 from sat.tools.common import utils |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
10 from sat_frontends.bridge.bridge_frontend import BridgeException |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 from libervia.server.constants import Const as C |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
12 from libervia.server import session_iface |
1069
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
13 from libervia.server import pages_tools |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
14 |
1145
29eb15062416
pages: set __name__ for imported pages
Goffi <goffi@goffi.org>
parents:
1124
diff
changeset
|
15 log = getLogger(__name__) |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 """files handling pages""" |
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
1216 | 18 name = "files_list" |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 access = C.PAGES_ACCESS_PROFILE |
1216 | 20 template = "file/overview.html" |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
22 |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 def parse_url(self, request): |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
24 self.getPathArgs(request, ["service", "*path"], min_args=1, service="jid", path="") |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
25 |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
1290
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
27 async def prepare_render(self, request): |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 data = self.getRData(request) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
29 thumb_limit = data.get("thumb_limit", 300) |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 template_data = request.template_data |
1216 | 31 service, path_elts = data["service"], data["path"] |
32 path = "/".join(path_elts) | |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 profile = self.getProfile(request) or C.SERVICE_PROFILE |
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
1290
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
35 try: |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
36 files_data = await self.host.bridgeCall("FISList", service.full(), path, {}, profile) |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
37 except BridgeException as e: |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
38 if e.condition == 'item-not-found': |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
39 log.debug( |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
40 f'"item-not-found" received for {path} at {service}, this may indicate ' |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
41 f'that the location is new') |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
42 files_data = [] |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
43 else: |
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
44 raise e |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 for file_data in files_data: |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
46 try: |
1216 | 47 extra_raw = file_data["extra"] |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
48 except KeyError: |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
49 pass |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
50 else: |
1216 | 51 file_data["extra"] = json.loads(extra_raw) if extra_raw else {} |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
52 dir_path = path_elts + [file_data["name"]] |
1216 | 53 if file_data["type"] == C.FILE_TYPE_DIRECTORY: |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 page = self |
1216 | 55 elif file_data["type"] == C.FILE_TYPE_FILE: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
56 page = self.getPageByName("files_view") |
1069
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
57 |
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
58 ## thumbnails ## |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
59 try: |
1216 | 60 thumbnails = file_data["extra"]["thumbnails"] |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
61 if not thumbnails: |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
62 raise KeyError |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
63 except KeyError: |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
64 pass |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
65 else: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
66 thumbnails.sort(key=lambda t: t["size"]) |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
67 thumb = thumbnails[0] |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
68 for thumb_data in thumbnails: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
69 if thumb_data["size"][0] > thumb_limit: |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
70 break |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
71 thumb = thumb_data |
1216 | 72 if "url" in thumb: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
73 file_data["thumb_url"] = thumb["url"] |
1216 | 74 elif "id" in thumb: |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
75 try: |
1290
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
76 thumb_path = await self.host.bridgeCall( |
1216 | 77 "bobGetFile", service.full(), thumb["id"], profile |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
78 ) |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
79 except Exception as e: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
80 log.warning( |
1216 | 81 _("Can't retrieve thumbnail: {reason}").format(reason=e) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
82 ) |
1067
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
83 else: |
808ec98de8b3
pages (files/list): retrieve thumbnail:
Goffi <goffi@goffi.org>
parents:
1064
diff
changeset
|
84 filename = os.path.basename(thumb_path) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
85 session_data = self.host.getSessionData( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
86 request, session_iface.ISATSession |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
87 ) |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
88 file_data["thumb_url"] = os.path.join( |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
89 session_data.cache_dir, filename |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
90 ) |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 else: |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
92 raise ValueError( |
1216 | 93 "unexpected file type: {file_type}".format(file_type=file_data["type"]) |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
94 ) |
1216 | 95 file_data["url"] = page.getURL(service.full(), *dir_path) |
1069
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
96 |
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
97 ## comments ## |
1216 | 98 comments_url = file_data.get("comments_url") |
1069
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
99 if comments_url: |
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
100 parsed_url = uri.parseXMPPUri(comments_url) |
1216 | 101 comments_service = file_data["comments_service"] = parsed_url["path"] |
102 comments_node = file_data["comments_node"] = parsed_url["node"] | |
1069
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
103 try: |
1216 | 104 comments_count = file_data["comments_count"] = int( |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
105 file_data["comments_count"] |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
106 ) |
1069
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
107 except KeyError: |
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
108 comments_count = None |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
109 if comments_count and data.get("retrieve_comments", False): |
1290
ee984eefc787
pages (files/list): return empty list of files when `item-not-found` is received
Goffi <goffi@goffi.org>
parents:
1289
diff
changeset
|
110 file_data["comments"] = await pages_tools.retrieveComments( |
1113
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
111 self, comments_service, comments_node, profile=profile |
cdd389ef97bc
server: code style reformatting using black
Goffi <goffi@goffi.org>
parents:
1069
diff
changeset
|
112 ) |
1069
2dab7692eae7
pages (files/list): comments handling + size parameters:
Goffi <goffi@goffi.org>
parents:
1067
diff
changeset
|
113 |
1216 | 114 template_data["files_data"] = files_data |
115 template_data["path"] = path | |
1289
c14ac2502011
pages (files/list): export `service` and `path` to scripts
Goffi <goffi@goffi.org>
parents:
1239
diff
changeset
|
116 # we make the service and path accessible from scripts |
c14ac2502011
pages (files/list): export `service` and `path` to scripts
Goffi <goffi@goffi.org>
parents:
1239
diff
changeset
|
117 self.exposeToScripts(request, files_service=service.full(), files_path=path) |
1064
abc5d545dbaa
pages (files): files sharing first draft:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 if path_elts: |
1216 | 119 template_data["parent_url"] = self.getURL(service.full(), *path_elts[:-1]) |