Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Apr 10, 2024
1 parent 7da10cc commit 29aa68f
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 46 deletions.
14 changes: 7 additions & 7 deletions src/redturtle/volto/browser/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@

<!-- helpers views for @site endpoint -->
<browser:page
name="site-favicon"
for="*"
class=".site_helpers_views.Favicon"
name="registry-images"
for="plone.base.interfaces.siteroot.INavigationRoot"
class=".site_helpers_views.RegistryImagesView"
permission="zope.Public"
/>
<browser:page
name="site-logo_footer"
for="*"
class=".site_helpers_views.LogoFooter"
<browser:page
name="images"
for="redturtle.volto.interfaces.IRegistryImagesView"
class=".site_helpers_views.ImagesView"
permission="zope.Public"
/>

Expand Down
55 changes: 35 additions & 20 deletions src/redturtle/volto/browser/site_helpers_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
from plone.namedfile.file import NamedImage
from plone.registry.interfaces import IRegistry
from zope.component import getUtility
from redturtle.volto.interfaces import IRegistryImagesView
from zope.interface import implementer
from Products.Five import BrowserView
from zope.publisher.interfaces import IPublishTraverse
from zope.publisher.interfaces import NotFound
from redturtle.volto.restapi.services.utils import FIELD_MAPPING


try:
Expand All @@ -11,29 +17,38 @@
from Products.CMFPlone.interfaces.controlpanel import ISiteSchema


class BaseView(Download):
def __init__(self, context, request):
super().__init__(context, request)
self.filename = None
self.data = None
@implementer(IRegistryImagesView)
class RegistryImagesView(BrowserView):
""""""

registry = getUtility(IRegistry)
settings = registry.forInterface(ISiteSchema, prefix="plone")
value = getattr(settings, self.field_name, False)
if value:
filename, data = b64decode_file(value)
data = NamedImage(data=data, filename=filename)
self.data = data
self.filename = filename
# self.width, self.height = self.data.getImageSize()
def __call__(self):
return

def _getFile(self):
return self.data

@implementer(IPublishTraverse)
class ImagesView(Download):

class Favicon(BaseView):
field_name = "site_favicon"
def __init__(self, context, request):
super().__init__(context=context, request=request)
self.data = None

def publishTraverse(self, request, name):
super().publishTraverse(request=request, name=name)

if self.fieldname:
registry = getUtility(IRegistry)
registry_interface = FIELD_MAPPING.get(self.fieldname, None)
if not registry_interface:
raise NotFound(self, "a", self.request)
settings = registry.forInterface(registry_interface, prefix="plone")
value = getattr(settings, self.fieldname, None)
if value:
filename, data = b64decode_file(value)
data = NamedImage(data=data, filename=filename)
self.data = data
return self

class LogoFooter(BaseView):
field_name = "site_logo_footer"
def _getFile(self):
if not self.data:
raise NotFound(self, "b", self.request)
return self.data
6 changes: 6 additions & 0 deletions src/redturtle/volto/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,9 @@ class IRedTurtleVoltoSiteSchema(ISiteSchema, IRedTurtleVoltoAdditionalSiteSchema

class IRedTurtleVoltoSiteControlpanel(IControlpanel):
""" """


class IRegistryImagesView(Interface):
"""
Marker interface for view
"""
32 changes: 13 additions & 19 deletions src/redturtle/volto/restapi/services/site/get.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from plone.restapi.services.site.get import Site as BaseSite
from plone.restapi.services.site.get import SiteGet as BaseSiteGet
from redturtle.volto.interfaces import IRedTurtleVoltoAdditionalSiteSchema
from redturtle.volto.restapi.services.utils import FIELD_MAPPING
from zope.component import adapter
from zope.component import getUtility
from zope.interface import implementer
Expand Down Expand Up @@ -34,7 +35,6 @@ def __call__(self, expand=False):
result = super().__call__(expand=expand)

registry = getUtility(IRegistry)
site_settings = registry.forInterface(ISiteSchema, prefix="plone", check=False)
additional_settings = registry.forInterface(
IRedTurtleVoltoAdditionalSiteSchema, prefix="plone", check=False
)
Expand All @@ -53,29 +53,23 @@ def __call__(self, expand=False):
result["site"]["plone.site_subtitle"] = self.get_translated_value(site_subtitle)

# images
field_mapping = {
"logo": site_settings,
"logo_footer": additional_settings,
"favicon": site_settings,
}
site_url = api.portal.get().absolute_url()
for field, settings_name in field_mapping.items():
value = self.get_value_from_registry(settings_name, f"site_{field}")
result["site"][f"plone.site_{field}"] = {}
for field, interface_name in FIELD_MAPPING.items():
settings = registry.forInterface(
interface_name, prefix="plone", check=False
)
value = self.get_value_from_registry(settings, field)
result["site"][f"plone.{field}"] = {}
if value:
filename, data = b64decode_file(value)
result["site"][f"plone.site_{field}"][
result["site"][f"plone.{field}"][
"url"
] = f"{site_url}/@@site-{field}/{filename}"
result["site"][f"plone.site_{field}"]["width"] = (
self.get_value_from_registry(
additional_settings, f"site_{field}_width"
)
] = f"{site_url}/registry-images/@@images/{field}/{filename}"
result["site"][f"plone.{field}"]["width"] = (
self.get_value_from_registry(additional_settings, f"{field}_width")
)
result["site"][f"plone.site_{field}"]["height"] = (
self.get_value_from_registry(
additional_settings, f"site_{field}_height"
)
result["site"][f"plone.{field}"]["height"] = (
self.get_value_from_registry(additional_settings, f"{field}_height")
)

return result
Expand Down
12 changes: 12 additions & 0 deletions src/redturtle/volto/restapi/services/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from redturtle.volto.interfaces import IRedTurtleVoltoAdditionalSiteSchema

try:
from plone.base.interfaces.controlpanel import ISiteSchema
except ImportError:
from Products.CMFPlone.interfaces.controlpanel import ISiteSchema

FIELD_MAPPING = {
"site_logo": ISiteSchema,
"site_logo_footer": IRedTurtleVoltoAdditionalSiteSchema,
"site_favicon": ISiteSchema,
}

0 comments on commit 29aa68f

Please sign in to comment.