Skip to content

Commit

Permalink
add metadata for taxonomies
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Jul 19, 2024
1 parent 4556a58 commit 9283f24
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 126 deletions.
10 changes: 6 additions & 4 deletions src/iosanita/contenttypes/indexers/taxonomies.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
# -*- coding: utf-8 -*-
from plone.dexterity.interfaces import IDexterityContent
from plone.indexer.decorator import indexer
from collective.taxonomy import PATH_SEPARATOR
from collective.taxonomy.interfaces import ITaxonomy
from zope.component import getUtility
from zope.globalrequest import getRequest
from plone.dexterity.interfaces import IDexterityContent


def extract_taxonomies(context, field):
request = getRequest()
taxonomy = getUtility(ITaxonomy, name=f"collective.taxonomy.{field}")
taxonomy_voc = taxonomy.makeVocabulary(request.get("LANGUAGE"))

data = []
for key in getattr(context, field, []) or []:
value = taxonomy_voc.inv_data.get(key, None)

if value and value.startswith(PATH_SEPARATOR):
data.append({"title": value.replace(PATH_SEPARATOR, "", 1), "token": key})
if not value:
continue
if value.startswith(PATH_SEPARATOR):
value = value.replace(PATH_SEPARATOR, "", 1)
data.append({"title": value.replace(PATH_SEPARATOR, "", 1), "token": key})
return data


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,99 +5,87 @@
</vocabName>
<vocabIdentifier>a_chi_si_rivolge_tassonomia</vocabIdentifier>
<term>
<termIdentifier>persone</termIdentifier>
<termIdentifier>bambini</termIdentifier>
<caption>
<langstring language="it">Persone</langstring>
<langstring language="it">Bambini</langstring>
</caption>
<term>
<termIdentifier>bambini</termIdentifier>
<caption>
<langstring language="it">Bambini</langstring>
</caption>
</term>
<term>
<termIdentifier>adolescenti</termIdentifier>
<caption>
<langstring language="it">Adolescenti</langstring>
</caption>
</term>
<term>
<termIdentifier>anziani</termIdentifier>
<caption>
<langstring language="it">Anziani</langstring>
</caption>
</term>
<term>
<termIdentifier>donne</termIdentifier>
<caption>
<langstring language="it">Donne</langstring>
</caption>
</term>
<term>
<termIdentifier>persone-con-disabilita</termIdentifier>
<caption>
<langstring language="it">Persone con disabilità</langstring>
</caption>
</term>
<term>
<termIdentifier>cittadini-stranieri</termIdentifier>
<caption>
<langstring language="it">Cittadini stranieri</langstring>
</caption>
</term>
<term>
<termIdentifier>migranti</termIdentifier>
<caption>
<langstring language="it">Migranti</langstring>
</caption>
</term>
<term>
<termIdentifier>lgbtq+</termIdentifier>
<caption>
<langstring language="it">LGBTQ+</langstring>
</caption>
</term>
<term>
<termIdentifier>proprietari-animali-domestici</termIdentifier>
<caption>
<langstring language="it">Proprietari di animali domestici e di affezione</langstring>
</caption>
</term>
<term>
<termIdentifier>prestatori-di cura</termIdentifier>
<caption>
<langstring language="it">Prestatori di cura (Caregiver)</langstring>
</caption>
</term>
</term>
<term>
<termIdentifier>entita</termIdentifier>
<termIdentifier>adolescenti</termIdentifier>
<caption>
<langstring language="it">Entità</langstring>
<langstring language="it">Adolescenti</langstring>
</caption>
</term>
<term>
<termIdentifier>anziani</termIdentifier>
<caption>
<langstring language="it">Anziani</langstring>
</caption>
</term>
<term>
<termIdentifier>donne</termIdentifier>
<caption>
<langstring language="it">Donne</langstring>
</caption>
</term>
<term>
<termIdentifier>persone-con-disabilita</termIdentifier>
<caption>
<langstring language="it">Persone con disabilità</langstring>
</caption>
</term>
<term>
<termIdentifier>cittadini-stranieri</termIdentifier>
<caption>
<langstring language="it">Cittadini stranieri</langstring>
</caption>
</term>
<term>
<termIdentifier>migranti</termIdentifier>
<caption>
<langstring language="it">Migranti</langstring>
</caption>
</term>
<term>
<termIdentifier>lgbtq+</termIdentifier>
<caption>
<langstring language="it">LGBTQ+</langstring>
</caption>
</term>
<term>
<termIdentifier>proprietari-animali-domestici</termIdentifier>
<caption>
<langstring language="it">Proprietari di animali domestici e di affezione</langstring>
</caption>
</term>
<term>
<termIdentifier>prestatori-di cura</termIdentifier>
<caption>
<langstring language="it">Prestatori di cura (Caregiver)</langstring>
</caption>
</term>
<term>
<termIdentifier>imprese</termIdentifier>
<caption>
<langstring language="it">Imprese</langstring>
</caption>
</term>
<term>
<termIdentifier>scuole</termIdentifier>
<caption>
<langstring language="it">Scuole</langstring>
</caption>
</term>
<term>
<termIdentifier>farmacie</termIdentifier>
<caption>
<langstring language="it">Farmacie</langstring>
</caption>
</term>
<term>
<termIdentifier>enti-pubblici</termIdentifier>
<caption>
<langstring language="it">Enti pubblici</langstring>
</caption>
<term>
<termIdentifier>imprese</termIdentifier>
<caption>
<langstring language="it">Imprese</langstring>
</caption>
</term>
<term>
<termIdentifier>scuole</termIdentifier>
<caption>
<langstring language="it">Scuole</langstring>
</caption>
</term>
<term>
<termIdentifier>farmacie</termIdentifier>
<caption>
<langstring language="it">Farmacie</langstring>
</caption>
</term>
<term>
<termIdentifier>enti-pubblici</termIdentifier>
<caption>
<langstring language="it">Enti pubblici</langstring>
</caption>
</term>
</term>
</vdex>
1 change: 1 addition & 0 deletions src/iosanita/contenttypes/profiles/default/metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
<dependency>profile-collective.taxonomy:default</dependency>
<dependency>profile-collective.venue:default</dependency>
<dependency>profile-collective.z3cform.datagridfield:default</dependency>
<dependency>profile-iosanita.contenttypes:taxonomy</dependency>
</dependencies>
</metadata>
28 changes: 2 additions & 26 deletions src/iosanita/contenttypes/setuphandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,8 @@ def post_install(context):
"iosanita.contenttypes:taxonomy", "collective.taxonomy"
)

# for utility_name, utility in list(getUtilitiesFor(ITaxonomy)):
# utility.updateBehavior(**{"field_prefix": ""})
# logger.info(
# f"{colors.DARKCYAN} Change taxonomy prefix for {utility_name} {colors.ENDC}" # noqa
# )

# logger.info(
# f"{colors.DARKCYAN} iosanita.contentypes taxonomies imported {colors.ENDC}" # noqa
# )
# update_types(context)
# update_registry(context)
# update_catalog(context)

# Do something at the end of the installation of this package.


def post_install_taxonomy(context):
for index in [
"tipologia_notizia",
"a_chi_si_rivolge_tassonomia",
"tipologia_argomento",
"tipologia_evento",
"tipologia_punti_di_contatto",
]:
api.portal.get_tool("portal_catalog").delIndex(index)

for utility_name, utility in list(getUtilitiesFor(ITaxonomy)):
utility.updateBehavior(**{"field_prefix": ""})
logger.info(
Expand All @@ -91,8 +67,8 @@ def post_install_taxonomy(context):
logger.info(
f"{colors.DARKCYAN} iosanita.contentypes taxonomies imported {colors.ENDC}" # noqa
)
update_types(context)
update_registry(context)
# update_types(context)
# update_registry(context)
update_catalog(context)


Expand Down
1 change: 1 addition & 0 deletions src/iosanita/contenttypes/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import plone.app.caching
import plone.formwidget.geolocation
import plone.restapi
import collective.taxonomy


class TestLayer(RedturtleVoltoLayer):
Expand Down
47 changes: 39 additions & 8 deletions src/iosanita/contenttypes/tests/test_taxonomies_custom_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
from plone import api
from plone.app.testing import setRoles
from plone.app.testing import TEST_USER_ID
from z3c.relationfield import RelationValue
from zope.component import getUtility
from zope.event import notify
from zope.intid.interfaces import IIntIds
from zope.lifecycleevent import ObjectModifiedEvent

import unittest

Expand All @@ -21,6 +16,10 @@ class TestTaxonomiesCustomMetadata(unittest.TestCase):
def setUp(self):
self.app = self.layer["app"]
self.portal = self.layer["portal"]
self.request = self.layer["request"]

self.request["LANGUAGE"] = "it"

self.portal_url = self.portal.absolute_url()
setRoles(self.portal, TEST_USER_ID, ["Manager"])

Expand All @@ -30,12 +29,44 @@ def test_parliamo_di_metadata_has_array_of_objects(self):
container=self.portal,
type="Struttura",
title="Test servizio",
parliamo_di=["vaccinazioni-anti-covid-19", "igiene-pubblica"],
parliamo_di=["ticket-ed-esenzioni", "igiene-pubblica"],
)
res = api.content.find(UID=struttura.UID())

self.assertEqual(len(res), 1)
self.assertEqual(
res[0].parliamo_di, sorted(["ticket-ed-esenzioni", "igiene-pubblica"])
)
self.assertEqual(
res[0].parliamo_di_metadata,
[
{"title": "Ticket ed esenzioni", "token": "ticket-ed-esenzioni"},
{"title": "Igiene Pubblica", "token": "igiene-pubblica"},
],
)

def test_a_chi_si_rivolge_tassonomia_metadata_has_array_of_objects(self):
""" """
struttura = api.content.create(
container=self.portal,
type="Struttura",
title="Test servizio",
a_chi_si_rivolge_tassonomia=[
"farmacie",
"imprese",
],
)
res = api.content.find(UID=struttura.UID())

self.assertEqual(len(res), 1)
self.assertEqual(
res[0].parliamo_di, ["vaccinazioni-anti-covid-19", "igiene-pubblica"]
res[0].a_chi_si_rivolge_tassonomia,
sorted(["farmacie", "imprese"]),
)
self.assertEqual(
res[0].a_chi_si_rivolge_tassonomia_metadata,
[
{"title": "Farmacie", "token": "farmacie"},
{"title": "Imprese", "token": "imprese"},
],
)
self.assertEqual(res[0].parliamo_di_metadata, [])

0 comments on commit 9283f24

Please sign in to comment.