Source code for ciowarehouse2.managers.image

"""A file manager for Web images."""

from __future__ import annotations
from os.path import splitext

from pyramid.config import Configurator
from pyramid.request import Request

from chrysalio.helpers.literal import Literal
from chrysalio.helpers.tags import image as tag_image
from ..lib.ciotype import CioType
from ..lib.ciopath import CioPath
from ..lib.warehouse import Warehouse
from ..lib.manager import Manager
from ..lib.i18n import _

EXTENSIONS_WEB = ('.png', '.jpg', '.jpeg', '.webp', '.svg', '.gif', '.ico')


# =============================================================================
[docs] def includeme(configurator: Configurator): """Function to include an image manager. :type configurator: pyramid.config.Configurator :param configurator: Object used to do configuration declaration within the application. """ Manager.register(configurator, ManagerImage)
# =============================================================================
[docs] class ManagerImage(Manager): """Class to manage an Web image.""" ciotype = CioType('image') label = _('Generic image file handling') viewings = ( # yapf: disable {'name': 'default', 'label': _('Default'), 'template': 'ciowarehouse2:Templates/manager_layout_view.pt', 'css': ('/ciowarehouse2/css/manager_image.css',)},) # -------------------------------------------------------------------------
[docs] def match(self, ciotype: CioType) -> bool: """Check whether this file manager matches with the CioType. See: :meth:`.lib.manager.Manager.match` """ return ciotype.type_ == 'image' \ and f'.{ciotype.subtype}' in EXTENSIONS_WEB
# -------------------------------------------------------------------------
[docs] def view( self, request: Request, warehouse: Warehouse, ciopath: CioPath, ts_factory=None) -> str | None: """Return a string containing HTML to display the file. See: :meth:`.lib.manager.Manager.view` """ if not ciopath or \ splitext(ciopath.path)[1].lower() not in EXTENSIONS_WEB: return None content = tag_image( request.route_path('file_download', ciopath=ciopath.route()), alt=ciopath.file_name(), title=ciopath) return self._chameleon_render( request, warehouse, ciopath, self.viewings[0], ts_factory or _, {'content': Literal(f'<div class="cioImage">{content}</div>')})