Skip to content

Commit

Permalink
Merge branch 'main' into section-external-content
Browse files Browse the repository at this point in the history
  • Loading branch information
boulch committed Jun 16, 2023
2 parents e83304b + 598ca6f commit 585dbe0
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Changelog
- Removal of unnecessary css in sections contact and gallery
[thomlamb]

- Add new browserview for Plausible
[remdub, boulch]


1.1.17 (2023-05-31)
-------------------
Expand Down
1 change: 1 addition & 0 deletions src/imio/smartweb/core/browser/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
/>

<include package=".banner" />
<include package=".dashboards" />
<include package=".faceted" />
<include package=".footer" />
<include package=".herobanner" />
Expand Down
24 changes: 24 additions & 0 deletions src/imio/smartweb/core/browser/controlpanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,30 @@ class ISmartwebControlPanel(Interface):
)
directives.widget("sendinblue_button_text", DataGridFieldFactory, auto_append=False)

plausible_url = schema.TextLine(
title=_("Plausible URL"),
description=_(
"Example : plausible.imio.be (SMARTWEB_PLAUSIBLE_URL varenv has precedence over this.)"
),
required=False,
)

plausible_site = schema.TextLine(
title=_("Plausible Site"),
description=_(
"Example : namur.be (SMARTWEB_PLAUSIBLE_SITE varenv has precedence over this.)"
),
required=False,
)

plausible_token = schema.TextLine(
title=_("Plausible Token"),
description=_(
"Plausible authentification token (SMARTWEB_PLAUSIBLE_TOKEN varenv has precedence over this.)"
),
required=False,
)


class SmartwebControlPanelForm(RegistryEditForm):
schema = ISmartwebControlPanel
Expand Down
Empty file.
17 changes: 17 additions & 0 deletions src/imio/smartweb/core/browser/dashboards/configure.zcml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<configure
xmlns="http://namespaces.zope.org/zope"
xmlns:browser="http://namespaces.zope.org/browser"
>
<include package="plone.app.contentmenu" />
<browser:page
name="stats"
title="Plausible dashboard"
template="plausible.pt"
menu="plone_displayviews"
for="plone.base.interfaces.siteroot.IPloneSiteRoot"
class=".plausible.PlausibleView"
permission="cmf.ModifyPortalContent"
layer="imio.smartweb.core.interfaces.IImioSmartwebCoreLayer"
/>

</configure>
29 changes: 29 additions & 0 deletions src/imio/smartweb/core/browser/dashboards/plausible.pt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
xmlns:tal="http://xml.zope.org/namespaces/tal"
xmlns:metal="http://xml.zope.org/namespaces/metal"
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
metal:use-macro="context/main_template/macros/master"
i18n:domain="imio.smartweb">
<body>
<metal:main fill-slot="content-core">
<iframe plausible-embed
src=""
scrolling="no"
frameborder="0"
loading="lazy"
id="plausible"
style="width: 1px; min-width: 100%; height: 1600px;"
tal:condition="view/is_plausible_set"
tal:attributes="src view/get_iframe_src">
</iframe>
<script async
src=""
tal:condition="view/is_plausible_set"
tal:attributes="src view/get_embedhostjs_src">
</script>
<h2 tal:condition="not:view/is_plausible_set" i18n:domain="imio.smartweb" i18n:translate="">
Plausible analytics is not set
</h2>
</metal:main>
</body>
</html>
24 changes: 24 additions & 0 deletions src/imio/smartweb/core/browser/dashboards/plausible.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from plone import api
from Products.Five.browser import BrowserView
import os
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from imio.smartweb.core.browser.utils import get_plausible_vars


class PlausibleView(BrowserView):
@property
def is_plausible_set(self):
if not get_plausible_vars(self):
return False
else:
return True

@property
def get_embedhostjs_src(self):
vars = get_plausible_vars(self)
return f"https://{vars['plausible_url']}/js/embed.host.js"

@property
def get_iframe_src(self):
vars = get_plausible_vars(self)
return f"https://{vars['plausible_url']}/share/{vars['plausible_site']}?auth={vars['plausible_token']}&embed=true&theme=light&background=transparent"
33 changes: 33 additions & 0 deletions src/imio/smartweb/core/browser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import json
import requests
import os


class UtilsView(BrowserView):
Expand Down Expand Up @@ -73,3 +74,35 @@ def is_eguichet_aware(self):
"text": _(response.reason),
}
)


def get_plausible_vars(self):
env_plausible_url = os.getenv("SMARTWEB_PLAUSIBLE_URL")
env_plausible_site = os.getenv("SMARTWEB_PLAUSIBLE_SITE")
env_plausible_token = os.getenv("SMARTWEB_PLAUSIBLE_TOKEN")

plausible_url = (
env_plausible_url
if (env_plausible_url and env_plausible_url != "")
else api.portal.get_registry_record("smartweb.plausible_url")
)
plausible_site = (
env_plausible_site
if (env_plausible_site and env_plausible_site != "")
else api.portal.get_registry_record("smartweb.plausible_site")
)
plausible_token = (
env_plausible_token
if (env_plausible_token and env_plausible_token != "")
else api.portal.get_registry_record("smartweb.plausible_token")
)
if not (plausible_url or plausible_site or plausible_token):
if plausible_url == "" or plausible_site == "" or plausible_token == "":
return False
else:
plausible_vars = {
"plausible_url": plausible_url,
"plausible_site": plausible_site,
"plausible_token": plausible_token,
}
return plausible_vars
83 changes: 83 additions & 0 deletions src/imio/smartweb/core/tests/test_plausibleview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# -*- coding: utf-8 -*-

from plone import api
from imio.smartweb.core.testing import IMIO_SMARTWEB_CORE_INTEGRATION_TESTING
from imio.smartweb.core.testing import ImioSmartwebTestCase
from imio.smartweb.core.browser.dashboards.plausible import PlausibleView
from unittest import mock
from zope.component import queryMultiAdapter

import os


class Testplausible(ImioSmartwebTestCase):
layer = IMIO_SMARTWEB_CORE_INTEGRATION_TESTING

def setUp(self):
"""Custom shared utility setup for tests"""
self.request = self.layer["request"]
self.portal = self.layer["portal"]

def set_registry_records(self):
api.portal.set_registry_record("smartweb.plausible_site", "site-registry.be")
api.portal.set_registry_record("smartweb.plausible_token", "token-registry")
api.portal.set_registry_record("smartweb.plausible_url", "url-registry.be")

@mock.patch.dict(
os.environ,
{
"SMARTWEB_PLAUSIBLE_SITE": "",
"SMARTWEB_PLAUSIBLE_TOKEN": "",
"SMARTWEB_PLAUSIBLE_URL": "",
},
)
def test_noenv(self):
view = PlausibleView(self.portal, self.request)
self.assertFalse(view.is_plausible_set)
self.set_registry_records()
self.assertTrue(view.is_plausible_set)
self.assertEqual(
view.get_iframe_src,
"https://url-registry.be/share/site-registry.be?auth=token-registry&embed=true&theme=light&background=transparent",
)
self.assertEqual(
view.get_embedhostjs_src,
"https://url-registry.be/js/embed.host.js",
)

@mock.patch.dict(
os.environ,
{
"SMARTWEB_PLAUSIBLE_SITE": "site-varenv.be",
"SMARTWEB_PLAUSIBLE_TOKEN": "token-varenv",
"SMARTWEB_PLAUSIBLE_URL": "url-varenv.be",
},
)
def test_env(self):
view = PlausibleView(self.portal, self.request)
self.assertTrue(view.is_plausible_set)
self.assertEqual(
view.get_iframe_src,
"https://url-varenv.be/share/site-varenv.be?auth=token-varenv&embed=true&theme=light&background=transparent",
)
self.assertEqual(
view.get_embedhostjs_src,
"https://url-varenv.be/js/embed.host.js",
)
self.set_registry_records()
self.assertEqual(
view.get_iframe_src,
"https://url-varenv.be/share/site-varenv.be?auth=token-varenv&embed=true&theme=light&background=transparent",
)
self.assertEqual(
view.get_embedhostjs_src,
"https://url-varenv.be/js/embed.host.js",
)

def test_plausible_view(self):
view = queryMultiAdapter((self.portal, self.request), name="stats")
self.assertNotIn("iframe", view())
self.assertIn("Plausible analytics is not set", view())
self.set_registry_records()
self.assertIn("iframe", view())
self.assertNotIn("Plausible analytics is not set", view())
24 changes: 21 additions & 3 deletions src/imio/smartweb/core/upgrades/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,18 @@
name="upgrade_1037_to_1038"
title="Upgrade core from 1037 to 1038"
directory="profiles/1037_to_1038"
description="Add new content type : imio.smartweb.SectionExternalContent"
description="Add plausible registry keys"
provides="Products.GenericSetup.interfaces.EXTENSION"
/>

<genericsetup:registerProfile
name="upgrade_1038_to_1039"
title="Upgrade core from 1038 to 1039"
directory="profiles/1038_to_1039"
description="Add new content type : imio.smartweb.SectionExternalContent"
provides="Products.GenericSetup.interfaces.EXTENSION"
/>

<genericsetup:upgradeStep
title="Configure first official release"
description="Run needed profiles steps and reindex catalog"
Expand Down Expand Up @@ -528,9 +536,19 @@
destination="1038"
profile="imio.smartweb.core:default">
<genericsetup:upgradeDepends
title="Add new content type : imio.smartweb.SectionExternalContent"
title="Add plausible registry keys"
import_profile="imio.smartweb.core.upgrades:upgrade_1037_to_1038"
/>
</genericsetup:upgradeSteps>


<genericsetup:upgradeSteps
source="1038"
destination="1039"
profile="imio.smartweb.core:default">
<genericsetup:upgradeDepends
title="Add new content type : imio.smartweb.SectionExternalContent"
import_profile="imio.smartweb.core.upgrades:upgrade_1038_to_1039"
/>
</genericsetup:upgradeSteps>

</configure>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0"?>
<registry
xmlns:i18n="http://xml.zope.org/namespaces/i18n"
i18n:domain="imio.smartweb">

<records interface="imio.smartweb.core.browser.controlpanel.ISmartwebControlPanel"
prefix="smartweb" />

</registry>

0 comments on commit 585dbe0

Please sign in to comment.