view 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
line wrap: on
line source

#!/usr/bin/env python3

import json
import os
from pathlib import Path
from sat.core.i18n import _
from sat.core.log import getLogger
from sat.tools.common import uri
from sat_frontends.bridge.bridge_frontend import BridgeException
from libervia.server.constants import Const as C
from libervia.server import session_iface
from libervia.server import pages_tools

log = getLogger(__name__)
"""files handling pages"""

name = "files_list"
access = C.PAGES_ACCESS_PROFILE
template = "file/overview.html"


def parse_url(self, request):
    self.getPathArgs(request, ["service", "*path"], min_args=1, service="jid", path="")


async def prepare_render(self, request):
    data = self.getRData(request)
    thumb_limit = data.get("thumb_limit", 300)
    template_data = request.template_data
    service, path_elts = data["service"], data["path"]
    path = Path('/', *path_elts)
    profile = self.getProfile(request) or C.SERVICE_PROFILE

    try:
        files_data = await self.host.bridgeCall(
            "FISList", service.full(), str(path), {}, profile)
    except BridgeException as e:
        if e.condition == 'item-not-found':
            log.debug(
                f'"item-not-found" received for {path} at {service}, this may indicate '
                f'that the location is new')
            files_data = []
        else:
            raise e
    for file_data in files_data:
        try:
            extra_raw = file_data["extra"]
        except KeyError:
            pass
        else:
            file_data["extra"] = json.loads(extra_raw) if extra_raw else {}
        dir_path = path_elts + [file_data["name"]]
        if file_data["type"] == C.FILE_TYPE_DIRECTORY:
            page = self
        elif file_data["type"] == C.FILE_TYPE_FILE:
            page = self.getPageByName("files_view")

            ## thumbnails ##
            try:
                thumbnails = file_data["extra"]["thumbnails"]
                if not thumbnails:
                    raise KeyError
            except KeyError:
                pass
            else:
                thumbnails.sort(key=lambda t: t["size"])
                thumb = thumbnails[0]
                for thumb_data in thumbnails:
                    if thumb_data["size"][0] > thumb_limit:
                        break
                    thumb = thumb_data
                if "url" in thumb:
                    file_data["thumb_url"] = thumb["url"]
                elif "id" in thumb:
                    try:
                        thumb_path = await self.host.bridgeCall(
                            "bobGetFile", service.full(), thumb["id"], profile
                        )
                    except Exception as e:
                        log.warning(
                            _("Can't retrieve thumbnail: {reason}").format(reason=e)
                        )
                    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
                        )
        else:
            raise ValueError(
                "unexpected file type: {file_type}".format(file_type=file_data["type"])
            )
        file_data["url"] = page.getURL(service.full(), *dir_path)

        ## comments ##
        comments_url = file_data.get("comments_url")
        if comments_url:
            parsed_url = uri.parseXMPPUri(comments_url)
            comments_service = file_data["comments_service"] = parsed_url["path"]
            comments_node = file_data["comments_node"] = parsed_url["node"]
            try:
                comments_count = file_data["comments_count"] = int(
                    file_data["comments_count"]
                )
            except KeyError:
                comments_count = None
            if comments_count and data.get("retrieve_comments", False):
                file_data["comments"] = await pages_tools.retrieveComments(
                    self, comments_service, comments_node, profile=profile
                )

    # 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')


    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))
    if path_elts:
        template_data["parent_url"] = self.getURL(service.full(), *path_elts[:-1])