Replaced icons from admin layout with HTML metas adapter
authorThierry Florac <thierry.florac@onf.fr>
Tue, 29 Jan 2019 15:53:39 +0100
changeset 111 49974ce9a976
parent 110 f08f5db5d350
child 112 9d98137903bb
Replaced icons from admin layout with HTML metas adapter
src/pyams_zmi/interfaces/templates/admin-layout.pt
src/pyams_zmi/metas.py
--- a/src/pyams_zmi/interfaces/templates/admin-layout.pt	Fri Jan 18 15:36:12 2019 +0100
+++ b/src/pyams_zmi/interfaces/templates/admin-layout.pt	Tue Jan 29 15:53:39 2019 +0100
@@ -1,46 +1,14 @@
 <!DOCTYPE html>
-<html lang="en" i18n:domain="pyams_skin"
+<html lang="${request.locale_name}" i18n:domain="pyams_skin"
 	  tal:define="config tales:back_configuration;
 				  zmi tales:zmi_configuration;
-				  anonymous request.principal.id == '__none__';"
-	  tal:attributes="lang request.locale_name">
+				  anonymous request.principal.id == '__none__';">
 <head>
 	<tal:var replace="structure tales:metas" />
-
-	<tal:if define="icon config.icon | nothing;
-					url tales:absolute_url(icon);"
-			condition="icon">
-		<!-- Favorite icon -->
-		<tal:var define="size icon.get_image_size()[0];
-						 timestamp tales:timestamp(icon)">
-			<link tal:condition="size > 144"
-				  rel="apple-touch-icon" sizes="180x180" href="${url}/++thumb++180x180?_=${timestamp}" type="${icon.content_type}" />
-			<link tal:condition="size > 114"
-				  rel="apple-touch-icon" sizes="144x144" href="${url}/++thumb++144x144?_=${timestamp}" type="${icon.content_type}" />
-			<link tal:condition="size > 72"
-				  rel="apple-touch-icon" sizes="114x114" href="${url}/++thumb++114x114?_=${timestamp}" type="${icon.content_type}" />
-			<link tal:condition="size > 32"
-				  rel="apple-touch-icon" sizes="72x72" href="${url}/++thumb++72x72?_=${timestamp}" type="${icon.content_type}" />
-			<link tal:condition="size > 16"
-				  rel="apple-touch-icon" sizes="32x32" href="${url}/++thumb++32x32?_=${timestamp}" type="${icon.content_type}" />
-			<link rel="apple-touch-icon" sizes="16x16" href="${url}/++thumb++16x16?_=${timestamp}" type="${icon.content_type}" />
-			<link tal:condition="size > 64"
-				  rel="icon" sizes="128x128" href="${url}/++thumb++128x128?_=${timestamp}" type="${icon.content_type}" />
-			<link tal:condition="size > 32"
-				  rel="icon" sizes="64x64" href="${url}/++thumb++64x64?_=${timestamp}" type="${icon.content_type}" />
-			<link tal:condition="size > 16"
-				  rel="icon" sizes="32x32" href="${url}/++thumb++32x32?_=${timestamp}" type="${icon.content_type}" />
-			<link rel="icon" sizes="16x16" href="${url}/++thumb++16x16?_=${timestamp}" type="${icon.content_type}" />
-			<link rel="shortcut icon" href="${url}" type="${icon.content_type}" />
-		</tal:var>
-	</tal:if>
-
+	<!-- JS extensions -->
+	<tal:var content="structure provider:pyams.jsextensions" />
+	<!-- End JS extensions -->
 	<tal:var replace="tales:resources" />
-
-	<!-- Javascript extensions -->
-	<tal:var content="structure provider:pyams.jsextensions" />
-	<!-- end javascript extensions -->
-
 </head>
 <body tal:attributes="class zmi.body_css_class">
 
--- a/src/pyams_zmi/metas.py	Fri Jan 18 15:36:12 2019 +0100
+++ b/src/pyams_zmi/metas.py	Tue Jan 29 15:53:39 2019 +0100
@@ -12,20 +12,20 @@
 
 __docformat__ = 'restructuredtext'
 
+from datetime import datetime
 
-# import standard library
+from zope.dublincore.interfaces import IZopeDublinCore
+from zope.interface import Interface
 
-# import interfaces
 from pyams_skin.interfaces.configuration import IBackOfficeConfiguration
 from pyams_skin.interfaces.metas import IHTMLContentMetas
+from pyams_skin.metas import HTMLTagMeta, LinkMeta
+from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
 from pyams_utils.interfaces.site import ISiteRoot
+from pyams_utils.timezone import tztime
+from pyams_utils.url import absolute_url
 from pyams_zmi.layer import IAdminLayer
 
-# import packages
-from pyams_skin.metas import HTMLTagMeta
-from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
-from zope.interface import Interface
-
 
 @adapter_config(name='title', context=(Interface, IAdminLayer, Interface), provides=IHTMLContentMetas)
 @adapter_config(name='title', context=(ISiteRoot, IAdminLayer, Interface), provides=IHTMLContentMetas)
@@ -38,3 +38,32 @@
         config = IBackOfficeConfiguration(self.request.root)
         yield HTMLTagMeta('title', config.title,
                           **{'data-ams-title-prefix': config.get_title_prefix(self.request)})
+
+
+@adapter_config(name='icon', context=(Interface, IAdminLayer, Interface), provides=IHTMLContentMetas)
+class IconMetasAdapter(ContextRequestViewAdapter):
+    """Icon metas adapter"""
+
+    order = 20
+
+    def get_metas(self):
+        config = IBackOfficeConfiguration(self.request.root)
+        if (config is not None) and (config.icon is not None):
+            icon = config.icon
+            icon_url = absolute_url(icon, self.request)
+            icon_size = icon.get_image_size()[0]
+            dc = IZopeDublinCore(icon)
+            timestamp = datetime.timestamp(tztime(dc.modified))
+            for size in (180, 144, 114, 72, 32, 16):
+                if icon_size >= size:
+                    yield LinkMeta('apple-touch-icon',
+                                   type=icon.content_type,
+                                   href='{}/++thumb++{}x{}?_={}'.format(icon_url, size, size, timestamp),
+                                   sizes='{0}x{0}'.format(size))
+            for size in (128, 124, 32):
+                if icon_size >= size:
+                    yield LinkMeta('icon',
+                                   type=icon.content_type,
+                                   href='{}/++thumb++{}x{}?_={}'.format(icon_url, size, size, timestamp),
+                                   sizes='{0}x{0}'.format(size))
+            yield LinkMeta('shortcut-icon', type=icon.content_type, href=icon_url)