Skip to content
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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# CHANGELOG

## Content Management SDK For Python
---
## v1.13.0

#### Date: 04 December 2026

- Taxonomy: added `publish(data)`, `localize(data, locale)` and `unlocalize(locale)`. A 2xx on publish means accepted, not completed. (DX-10478)

---
## v1.12.0

Expand Down
2 changes: 1 addition & 1 deletion contentstack_management/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def get_contentstack_endpoint(region='us', service='', omit_https=False):
__author__ = 'dev-ex'
__status__ = 'debug'
__region__ = 'na'
__version__ = '1.12.0'
__version__ = '1.13.0'
__host__ = 'api.contentstack.io'
__protocol__ = 'https://'
__api_version__ = 'v3'
Expand Down
1 change: 1 addition & 0 deletions contentstack_management/_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@

# Taxonomy messages
TAXONOMY_UID_REQUIRED = "Taxonomy UID is required. Provide a valid Taxonomy UID and try again."
TAXONOMY_UID_NOT_ALLOWED = "Taxonomy UID is not allowed for this call. Publish is collection-level: call taxonomy() without a Taxonomy UID and try again."

# Terms messages
TERMS_UID_REQUIRED = "Terms UID is required. Provide a valid Terms UID and try again."
Expand Down
102 changes: 101 additions & 1 deletion contentstack_management/taxonomies/taxonomy.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ..common import Parameter
from .._errors import ArgumentException
from ..terms.terms import Terms
from .._messages import TAXONOMY_UID_REQUIRED
from .._messages import TAXONOMY_UID_REQUIRED, TAXONOMY_UID_NOT_ALLOWED, LOCALE_CODE_REQUIRED

class Taxonomy(Parameter):
"""
Expand Down Expand Up @@ -151,6 +151,106 @@ def validate_taxonomy_uid(self):
def terms(self, terms_uid: str = None):
self.validate_taxonomy_uid()
return Terms(self.client, self.taxonomy_uid, terms_uid)

def publish(self, data: dict):
"""
The "Publish taxonomies" call starts a job that publishes one or more taxonomies to the
given environments and locales. It is collection-level: call it from taxonomy() without a
Taxonomy UID.

A 2xx response means the publish request was ACCEPTED and queued. It does not mean the
publish has completed. To confirm the outcome, check the publish queue with
`client.stack('api_key').publish_queue().find()`, or subscribe to the publish-failure webhooks.

Note: a `branch` header set through `add_header` is written to the client-wide
`client.client.headers` and stays set for later calls on the same client (params are per instance).

:param data: The `data` parameter is the payload that you want to send in the request body.
It should be a dictionary with `locales`, `environments` and `items`, where each item is `{"uid": ...}`.
:return: Json, with the acknowledgement of the accepted publish request.

-------------------------------
[Example:]
>>> data = {
>>> "locales": ["en-us"],
>>> "environments": ["development"],
>>> "items": [{"uid": "taxonomy_1"}]
>>> }
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy().publish(data).json()

-------------------------------
"""
if self.taxonomy_uid:
raise ArgumentException(TAXONOMY_UID_NOT_ALLOWED)

data = json.dumps(data)
url = f"{self.path}/publish"
return self.client.post(url, headers = self.client.headers, data=data, params = self.params)

def localize(self, data: dict, locale: str):
"""
The "Localize taxonomy" call creates a localized version of a taxonomy in the given locale.
It is instance-level: call it from taxonomy(taxonomy_uid).

:param data: The `data` parameter is the payload that you want to send in the request body,
sent as given. It should be a dictionary, for example `{"taxonomy": {...}}`.
:param locale: Required, must not be blank. The locale to localize the taxonomy into, for example `fr-fr`.
:return: Json, with the localized taxonomy details.

-------------------------------
[Example:]
>>> data = {
>>> "taxonomy": {
>>> "name": "Taxonomie 1",
>>> "description": "Description fr"
>>> }
>>> }
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy('taxonomy_uid').localize(data, 'fr-fr').json()

-------------------------------
"""
if not self.taxonomy_uid:
raise ArgumentException(TAXONOMY_UID_REQUIRED)

if not locale or not str(locale).strip():
raise ArgumentException(LOCALE_CODE_REQUIRED)

self.params['locale'] = locale
url = f"{self.path}/{self.taxonomy_uid}"
data = json.dumps(data)
return self.client.post(url, headers = self.client.headers, data=data, params = self.params)

def unlocalize(self, locale: str):
"""
The "Unlocalize taxonomy" call removes the localized version of a taxonomy for the given locale.
It is instance-level: call it from taxonomy(taxonomy_uid).
The API responds with 204 No Content and an empty body, so the raw response is returned.

:param locale: Required, must not be blank. The locale whose localized version of the taxonomy is removed,
for example `fr-fr`. A missing locale is rejected so the call can never become a whole-taxonomy delete.
:return: The raw response. The status code is 204 No Content on success.

-------------------------------
[Example:]
>>> import contentstack_management
>>> client = contentstack_management.Client(authtoken='your_authtoken')
>>> result = client.stack('api_key').taxonomy('taxonomy_uid').unlocalize('fr-fr')

-------------------------------
"""
if not self.taxonomy_uid:
raise ArgumentException(TAXONOMY_UID_REQUIRED)

if not locale or not str(locale).strip():
raise ArgumentException(LOCALE_CODE_REQUIRED)

self.params['locale'] = locale
url = f"{self.path}/{self.taxonomy_uid}"
return self.client.delete(url, headers = self.client.headers, params = self.params)



131 changes: 131 additions & 0 deletions tests/api/taxonomies/test_taxonomy_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import uuid
import contentstack_management
from tests.cred import get_credentials

Expand Down Expand Up @@ -51,5 +52,135 @@ def test_delete_taxonomy(self):
response = self.client.stack(api_key).taxonomy(taxonomy_uid).delete()
self.assertEqual(response.status_code, 200)

# Publish / localize / unlocalize. Environments, locales and branches are looked up on the
# stack that runs the suite; a test skips with a reason when the stack has none to use.
# Taxonomies are created per test with a unique UID and deleted in cleanup.

def _create_taxonomy(self):
uid = f"sdk_it_{uuid.uuid4().hex[:8]}"
data = {
"taxonomy": {
"uid": uid,
"name": "SDK integration taxonomy",
"description": "Created by the SDK integration tests"
}
}
response = self.client.stack(api_key).taxonomy().create(data)
self.assertEqual(response.status_code, 201)
self.addCleanup(self._delete_taxonomy, uid)
return uid

def _delete_taxonomy(self, uid):
response = self.client.stack(api_key).taxonomy(uid).delete()
self.assertEqual(response.status_code, 200)

def _environment_names(self):
response = self.client.stack(api_key).environments().find()
self.assertEqual(response.status_code, 200)
return [env["name"] for env in response.json().get("environments", [])]

def _locale_codes(self, exclude_master=False):
response = self.client.stack(api_key).locale().find()
self.assertEqual(response.status_code, 200)
return [loc["code"] for loc in response.json().get("locales", [])
if not (exclude_master and loc.get("default"))]

def _branch_uids(self):
# Unlike environments and locales, which every stack can list (so a failed listing fails the test),
# branching is a plan feature: a stack without it cannot list branches, so the test skips here.
# The skip reason names the HTTP status, so a real 401 or 5xx is named rather than silently passed.
response = self.client.stack(api_key).branch().find()
if not 200 <= response.status_code < 300:
self.skipTest(f"could not list branches (HTTP {response.status_code}); "
"not treated as 'no main branch'")
return [branch["uid"] for branch in response.json().get("branches", [])]

def test_publish_one_taxonomy_to_one_environment_and_locale_returns_acknowledgement(self):
environments = self._environment_names()
locales = self._locale_codes()
if not environments or not locales:
self.skipTest("stack has no environment or locale to publish to")
uid = self._create_taxonomy()
data = {
"locales": [locales[0]],
"environments": [environments[0]],
"items": [{"uid": uid}]
}
response = self.client.stack(api_key).taxonomy().publish(data)
# The API accepts the publish job; it does not report that publishing completed.
self.assertTrue(200 <= response.status_code < 300, response.text)
body = response.json()
self.assertTrue("notice" in body or "job_id" in body, body)

def test_publish_multiple_locales_and_environments_returns_acknowledgement(self):
environments = self._environment_names()
locales = self._locale_codes()
if len(environments) < 2 or len(locales) < 2:
self.skipTest("stack needs at least two environments and two locales")
uid = self._create_taxonomy()
data = {
"locales": locales[:2],
"environments": environments[:2],
"items": [{"uid": uid}]
}
response = self.client.stack(api_key).taxonomy().publish(data)
self.assertTrue(200 <= response.status_code < 300, response.text)
body = response.json()
self.assertTrue("notice" in body or "job_id" in body, body)

def test_publish_with_branch_header_returns_acknowledgement(self):
environments = self._environment_names()
locales = self._locale_codes()
branches = self._branch_uids()
if not environments or not locales:
self.skipTest("stack has no environment or locale to publish with")
if "main" not in branches:
self.skipTest("stack has no branch named 'main'")
uid = self._create_taxonomy()
# Asserts only that publish accepts a `branch` header, not that routing changed.
# Registered after _create_taxonomy's cleanup, so the shared header is removed
# before the taxonomy DELETE runs (cleanups run last-in, first-out).
self.addCleanup(self.client.client.headers.pop, "branch", None)
data = {
"locales": [locales[0]],
"environments": [environments[0]],
"items": [{"uid": uid}]
}
taxonomy = self.client.stack(api_key).taxonomy()
taxonomy.add_header("branch", "main")
response = taxonomy.publish(data)
self.assertTrue(200 <= response.status_code < 300, response.text)
body = response.json()
self.assertTrue("notice" in body or "job_id" in body, body)

def test_localize_taxonomy_into_non_master_locale(self):
locales = self._locale_codes(exclude_master=True)
if not locales:
self.skipTest("stack has no non-master locale to localize into")
uid = self._create_taxonomy()
data = {
"taxonomy": {
"name": "SDK localized name",
"description": "SDK localized description"
}
}
response = self.client.stack(api_key).taxonomy(uid).localize(data, locales[0])
self.assertIn(response.status_code, (200, 201), response.text)
self.assertEqual(response.json()["taxonomy"]["uid"], uid)

def test_unlocalize_taxonomy_returns_204_with_empty_body(self):
locales = self._locale_codes(exclude_master=True)
if not locales:
self.skipTest("stack has no non-master locale to localize and unlocalize")
uid = self._create_taxonomy()
data = {"taxonomy": {"name": "SDK localized name"}}
localized = self.client.stack(api_key).taxonomy(uid).localize(data, locales[0])
self.assertIn(localized.status_code, (200, 201), localized.text)
response = self.client.stack(api_key).taxonomy(uid).unlocalize(locales[0])
self.assertEqual(response.status_code, 204)
# 204 No Content: the body is empty and is not parsed as JSON.
self.assertEqual(response.text, "")


if __name__ == '__main__':
unittest.main()
Loading
Loading