Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix+feat): If a language doesn't have a value for taxonomy set the default language value #172

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/collective/taxonomy/jsonimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def generate_json(self, root):
item = {}
item["key"] = root.find("termIdentifier").text
captionnode = root.find("caption")

translations = {}
for langstringnode in captionnode.getchildren():
translations[langstringnode.get("language")] = langstringnode.text
Expand Down Expand Up @@ -69,6 +70,7 @@ def get_data(self):

def get_languages_mapping(self):
"""Get mapping token/value for languages."""

vocab = AvailableContentLanguageVocabularyFactory(self.context)
language_tool = api.portal.get_tool("portal_languages")
plone_selected_languages = language_tool.supported_langs
Expand Down Expand Up @@ -100,7 +102,9 @@ class ImportJson(BrowserView):

def __call__(self):
request = self.request

if request.method == "POST":

data = json.loads(request.get("BODY", ""))
taxonomy = queryUtility(ITaxonomy, name=data["taxonomy"])
tree = data["tree"]
Expand Down Expand Up @@ -137,10 +141,17 @@ def __call__(self):
)

def generate_data_for_taxonomy(self, parsed_data, language, path=PATH_SEPARATOR):
body = self.request.get("BODY", "")
if not body.strip(): # Check if the body is empty or just whitespace
body = "{}" # Default to an empty JSON object instead of an empty string
data = json.loads(body)
taxonomy = queryUtility(ITaxonomy, name=data.get("taxonomy"))
default_language = taxonomy.default_language if taxonomy else None
result = []
for item in parsed_data:
new_key = item["key"]
title = item["translations"].get(language, "")
default_title = item["translations"].get(default_language, "")
title = item["translations"].get(language, "") or default_title
new_path = f"{path}{title}"
result.append(
(
Expand Down
12 changes: 11 additions & 1 deletion src/collective/taxonomy/restapi/services/taxonomy/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from zope.schema import getFields
from zope.schema.interfaces import ValidationError

import json


@implementer(IPublishTraverse)
class TaxonomyPatch(Service):
Expand All @@ -33,11 +35,18 @@ def publishTraverse(self, request, name): # noqa
return self

def generate_data_for_taxonomy(self, parsed_data, language, path=PATH_SEPARATOR):
body = self.request.get("BODY", "")
if not body.strip(): # Check if the body is empty or just whitespace
body = "{}" # Default to an empty JSON object instead of an empty string
data = json.loads(body)
taxonomy = queryUtility(ITaxonomy, name=data.get("taxonomy"))
default_language = taxonomy.default_language if taxonomy else None
result = []
for item in parsed_data:
translations = item.get("translations", {})
new_key = item["key"]
title = item["title"]
default_title = item["translations"].get(default_language, "")
title = item["translations"].get(language, "") or default_title
if language in translations:
title = translations[language]
new_path = f"{path}{title}"
Expand All @@ -57,6 +66,7 @@ def generate_data_for_taxonomy(self, parsed_data, language, path=PATH_SEPARATOR)

def reply(self):
"""Reply"""

if not self.taxonomy_id:
raise Exception("No taxonomy name provided")

Expand Down
Loading