comparison libervia/pages/files/list/page_meta.py @ 1313:12aa95eeb409

pages (files/list): set `directory_affiliation` template data: Directory affiliation is checked by listing parent directory, then added to template_data
author Goffi <goffi@goffi.org>
date Sat, 01 Aug 2020 16:47:24 +0200
parents ee984eefc787
children d0575e9abf7d
comparison
equal deleted inserted replaced
1312:39a87d9099c4 1313:12aa95eeb409
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 2
3 import json 3 import json
4 import os 4 import os
5 from twisted.internet import defer 5 from pathlib import Path
6 from sat.core.i18n import _ 6 from sat.core.i18n import _
7 from sat.core.log import getLogger 7 from sat.core.log import getLogger
8 from sat.tools.common import uri 8 from sat.tools.common import uri
9 from sat.tools.common import utils
10 from sat_frontends.bridge.bridge_frontend import BridgeException 9 from sat_frontends.bridge.bridge_frontend import BridgeException
11 from libervia.server.constants import Const as C 10 from libervia.server.constants import Const as C
12 from libervia.server import session_iface 11 from libervia.server import session_iface
13 from libervia.server import pages_tools 12 from libervia.server import pages_tools
14 13
27 async def prepare_render(self, request): 26 async def prepare_render(self, request):
28 data = self.getRData(request) 27 data = self.getRData(request)
29 thumb_limit = data.get("thumb_limit", 300) 28 thumb_limit = data.get("thumb_limit", 300)
30 template_data = request.template_data 29 template_data = request.template_data
31 service, path_elts = data["service"], data["path"] 30 service, path_elts = data["service"], data["path"]
32 path = "/".join(path_elts) 31 path = Path('/', *path_elts)
33 profile = self.getProfile(request) or C.SERVICE_PROFILE 32 profile = self.getProfile(request) or C.SERVICE_PROFILE
34 33
35 try: 34 try:
36 files_data = await self.host.bridgeCall("FISList", service.full(), path, {}, profile) 35 files_data = await self.host.bridgeCall(
36 "FISList", service.full(), str(path), {}, profile)
37 except BridgeException as e: 37 except BridgeException as e:
38 if e.condition == 'item-not-found': 38 if e.condition == 'item-not-found':
39 log.debug( 39 log.debug(
40 f'"item-not-found" received for {path} at {service}, this may indicate ' 40 f'"item-not-found" received for {path} at {service}, this may indicate '
41 f'that the location is new') 41 f'that the location is new')
109 if comments_count and data.get("retrieve_comments", False): 109 if comments_count and data.get("retrieve_comments", False):
110 file_data["comments"] = await pages_tools.retrieveComments( 110 file_data["comments"] = await pages_tools.retrieveComments(
111 self, comments_service, comments_node, profile=profile 111 self, comments_service, comments_node, profile=profile
112 ) 112 )
113 113
114 # parent dir affiliation
115 # for the moment the only way to get affiliation of current directory is to check
116 # it from parent's list
117 # TODO: some caching? What if affiliation changes?
118 if path.parent != path:
119 try:
120 parent_files_data = await self.host.bridgeCall(
121 "FISList", service.full(), str(path.parent), {}, profile)
122 except BridgeException as e:
123 if e.condition == 'item-not-found':
124 pass
125 else:
126 raise e
127 else:
128 directory_data = next(f for f in parent_files_data if f['name'] == path.name)
129 template_data['directory_affiliation'] = directory_data.get('affiliation')
130
131
114 template_data["files_data"] = files_data 132 template_data["files_data"] = files_data
115 template_data["path"] = path 133 template_data["path"] = path
116 # we make the service and path accessible from scripts 134 # we make the service and path accessible from scripts
117 self.exposeToScripts(request, files_service=service.full(), files_path=path) 135 self.exposeToScripts(request, files_service=service.full(), files_path=str(path))
118 if path_elts: 136 if path_elts:
119 template_data["parent_url"] = self.getURL(service.full(), *path_elts[:-1]) 137 template_data["parent_url"] = self.getURL(service.full(), *path_elts[:-1])