diff libervia/pages/files/list/page_meta.py @ 1316:d0575e9abf7d

pages (files/list): use new `FISAffiliationsGet` to get `directory_affiliation`
author Goffi <goffi@goffi.org>
date Sat, 01 Aug 2020 16:47:24 +0200
parents 12aa95eeb409
children 009542289bc9
line wrap: on
line diff
--- a/libervia/pages/files/list/page_meta.py	Sat Aug 01 16:47:24 2020 +0200
+++ b/libervia/pages/files/list/page_meta.py	Sat Aug 01 16:47:24 2020 +0200
@@ -30,6 +30,9 @@
     service, path_elts = data["service"], data["path"]
     path = Path('/', *path_elts)
     profile = self.getProfile(request) or C.SERVICE_PROFILE
+    session_data = self.host.getSessionData(
+        request, session_iface.ISATSession
+    )
 
     try:
         files_data = await self.host.bridgeCall(
@@ -82,9 +85,6 @@
                         )
                     else:
                         filename = os.path.basename(thumb_path)
-                        session_data = self.host.getSessionData(
-                            request, session_iface.ISATSession
-                        )
                         file_data["thumb_url"] = os.path.join(
                             session_data.cache_dir, filename
                         )
@@ -112,26 +112,28 @@
                 )
 
     # parent dir affiliation
-    # for the moment the only way to get affiliation of current directory is to check
-    # it from parent's list
     # TODO: some caching? What if affiliation changes?
-    if path.parent != path:
-        try:
-            parent_files_data = await self.host.bridgeCall(
-                "FISList", service.full(), str(path.parent), {}, profile)
-        except BridgeException as e:
-            if e.condition == 'item-not-found':
-                pass
-            else:
-                raise e
-        else:
-            directory_data = next(f for f in parent_files_data if f['name'] == path.name)
-            template_data['directory_affiliation'] = directory_data.get('affiliation')
 
+    affiliations = await self.host.bridgeCall(
+        "FISAffiliationsGet", service.full(), "", str(path), profile
+    )
+    directory_affiliation = affiliations.get(session_data.jid.userhost())
+    if directory_affiliation == "owner":
+        # we need to transtype dict items to str because with some bridges (D-Bus)
+        # we have a specific type which can't be exposed
+        self.exposeToScripts(
+            request,
+            affiliations={str(e): str(a) for e, a in affiliations.items()}
+        )
 
+    template_data["directory_affiliation"] = directory_affiliation
     template_data["files_data"] = files_data
     template_data["path"] = path
-    # we make the service and path accessible from scripts
-    self.exposeToScripts(request, files_service=service.full(), files_path=str(path))
+    self.exposeToScripts(
+        request,
+        directory_affiliation=str(directory_affiliation),
+        files_service=service.full(),
+        files_path=str(path),
+    )
     if path_elts:
         template_data["parent_url"] = self.getURL(service.full(), *path_elts[:-1])