From 145c9d36d87ce64ad99b4bdd1b8bf54a5f071840 Mon Sep 17 00:00:00 2001 From: Bhushan Lodha Date: Sun, 5 Feb 2023 00:52:27 -0300 Subject: [PATCH 1/2] Adds support for cross-space references --- contentful_management/content_type_field.py | 1 + .../content_type_field_types.py | 11 ++ .../create_with_resource_link_field.yaml | 106 ++++++++++++++++++ tests/content_type_test.py | 34 ++++++ 4 files changed, 152 insertions(+) create mode 100644 fixtures/content_type/create_with_resource_link_field.yaml diff --git a/contentful_management/content_type_field.py b/contentful_management/content_type_field.py index c854ef5..45f664c 100644 --- a/contentful_management/content_type_field.py +++ b/contentful_management/content_type_field.py @@ -33,6 +33,7 @@ def __init__(self, field_data): self.disabled = field_data.get('disabled', False) self.validations = [ContentTypeFieldValidation(v) for v in field_data.get('validations', [])] + self.allowedResources = field_data.get('allowedResources') self._coercion = self._get_coercion() def to_json(self): diff --git a/contentful_management/content_type_field_types.py b/contentful_management/content_type_field_types.py index 291d478..fcf21c6 100644 --- a/contentful_management/content_type_field_types.py +++ b/contentful_management/content_type_field_types.py @@ -187,3 +187,14 @@ def coerce(self, value): """ return value + + +class ResourceLinkField(BasicField): + """ + ResourceLinkField + + Nothing should be done here as ResourceLink does not support entity + resolution on the backend side and linked field can’t be used in + ordering and filtering queries. + """ + pass diff --git a/fixtures/content_type/create_with_resource_link_field.yaml b/fixtures/content_type/create_with_resource_link_field.yaml new file mode 100644 index 0000000..2dbe389 --- /dev/null +++ b/fixtures/content_type/create_with_resource_link_field.yaml @@ -0,0 +1,106 @@ +interactions: +- request: + body: '{"name": "Create Test", "description": "Something goes here...", "fields": + [{"id": "myResourceLink", "name": "My Resource Link", "type": "ResourceLink", + "localized": true, "disabled": false, "omitted": false, "allowedResources": + [{"type": "Contentful:Entry", "source": "crn:contentful:::content:spaces/yb41ceqgyiw5", + "contentTypes": ["foo", "bar"]}]}]}' + headers: + Accept: + - '*/*' + Accept-Encoding: + - identity + Authorization: + - Bearer foobar + Connection: + - keep-alive + Content-Length: + - '352' + Content-Type: + - application/vnd.contentful.management.v1+json + User-Agent: + - python-requests/2.25.1 + X-Contentful-User-Agent: + - sdk contentful-management.py/2.11.0; platform python/3.8.9; os macOS/22.1.0; + method: POST + uri: https://api.contentful.com/spaces/facgnwwgj5fe/environments/master/content_types + response: + body: + string: "{\n \"sys\": {\n \"space\": {\n \"sys\": {\n \"type\"\ + : \"Link\",\n \"linkType\": \"Space\",\n \"id\": \"facgnwwgj5fe\"\ + \n }\n },\n \"id\": \"3F98etLR7D2Qn4pXRRT5Gn\",\n \"type\":\ + \ \"ContentType\",\n \"createdAt\": \"2023-02-05T03:48:07.082Z\",\n \ + \ \"updatedAt\": \"2023-02-05T03:48:07.082Z\",\n \"environment\": {\n \ + \ \"sys\": {\n \"id\": \"master\",\n \"type\": \"Link\"\ + ,\n \"linkType\": \"Environment\"\n }\n },\n \"createdBy\"\ + : {\n \"sys\": {\n \"type\": \"Link\",\n \"linkType\":\ + \ \"User\",\n \"id\": \"59Erm8D1JuuD273aXNb65T\"\n }\n },\n\ + \ \"updatedBy\": {\n \"sys\": {\n \"type\": \"Link\",\n \ + \ \"linkType\": \"User\",\n \"id\": \"59Erm8D1JuuD273aXNb65T\"\n\ + \ }\n },\n \"publishedCounter\": 0,\n \"version\": 1\n },\n\ + \ \"displayField\": null,\n \"name\": \"Create Test\",\n \"description\"\ + : \"Something goes here...\",\n \"fields\": [\n {\n \"id\": \"myResourceLink\"\ + ,\n \"name\": \"My Resource Link\",\n \"type\": \"ResourceLink\"\ + ,\n \"localized\": true,\n \"required\": false,\n \"validations\"\ + : [],\n \"disabled\": false,\n \"omitted\": false,\n \"allowedResources\"\ + : [\n {\n \"type\": \"Contentful:Entry\",\n \"source\"\ + : \"crn:contentful:::content:spaces/yb41ceqgyiw5\",\n \"contentTypes\"\ + : [\n \"foo\",\n \"bar\"\n ]\n }\n \ + \ ]\n }\n ]\n}\n" + headers: + Accept-Ranges: + - bytes + Access-Control-Allow-Headers: + - Accept,Accept-Language,Authorization,Cache-Control,Content-Length,Content-Range,Content-Type,DNT,Destination,Expires,If-Match,If-Modified-Since,If-None-Match,Keep-Alive,Last-Modified,Origin,Pragma,Range,User-Agent,X-Http-Method-Override,X-Mx-ReqToken,X-Requested-With,X-Contentful-Version,X-Contentful-Content-Type,X-Contentful-Organization,X-Contentful-Skip-Transformation,X-Contentful-Tag-Visibility,X-Contentful-User-Agent,X-Contentful-Enable-Alpha-Feature,X-Contentful-Source-Environment,X-Contentful-Team,X-Contentful-Parent-Id,x-contentful-validate-only,X-Contentful-Skip-UI-Draft-Validation,X-Contentful-Marketplace,X-Contentful-UI-Content-Auto-Save,cf-trace + Access-Control-Allow-Methods: + - DELETE,GET,HEAD,POST,PUT,PATCH,OPTIONS + Access-Control-Allow-Origin: + - '*' + Access-Control-Expose-Headers: + - Etag + Access-Control-Max-Age: + - '1728000' + Connection: + - keep-alive + Content-Length: + - '1336' + Content-Type: + - application/vnd.contentful.management.v1+json + Contentful-Api: + - cma + Contentful-Upstream: + - content-api + Date: + - Sun, 05 Feb 2023 03:48:07 GMT + Server: + - Contentful + Strict-Transport-Security: + - max-age=15768000 + X-Content-Type-Options: + - nosniff + X-Contentful-RateLimit-Hour-Limit: + - '36000' + X-Contentful-RateLimit-Hour-Remaining: + - '35999' + X-Contentful-RateLimit-Reset: + - '0' + X-Contentful-RateLimit-Second-Limit: + - '10' + X-Contentful-RateLimit-Second-Remaining: + - '9' + cf-environment-id: + - master + cf-environment-uuid: + - master + cf-space-id: + - facgnwwgj5fe + etag: + - '"3915434941246858595"' + x-contentful-request-id: + - 3e474a8f-daeb-4fe3-b973-9e65f88f2d3d + x-contentful-route: + - /spaces/:space/environments/:environment/content_types + status: + code: 201 + message: Created +version: 1 diff --git a/tests/content_type_test.py b/tests/content_type_test.py index d4e9efd..87a4cce 100644 --- a/tests/content_type_test.py +++ b/tests/content_type_test.py @@ -113,6 +113,40 @@ def test_create_content_type_with_id(self): self.assertEqual(content_type.id, 'id_content_type_create_test') self.assertEqual(content_type.name, 'ID Create Test') + @vcr.use_cassette('fixtures/content_type/create_with_resource_link_field.yaml') + def test_create_content_type_with_resource_link_field(self): + content_type = CLIENT.content_types(PLAYGROUND_SPACE, 'master').create(None, { + 'name': 'Create Test', + 'description': 'Something goes here...', + 'fields': [{ + 'id': 'myResourceLink', + 'name': 'My Resource Link', + 'type': 'ResourceLink', + 'localized': True, + 'disabled': False, + 'omitted': False, + 'allowedResources': [ + { + 'type': 'Contentful:Entry', + 'source': 'crn:contentful:::content:spaces/yb41ceqgyiw5', + 'contentTypes': ["foo", "bar"] + }] + } + ] + }) + + self.assertEqual(len(content_type.fields), 1) + + field = content_type.fields[0] + + self.assertEqual(field.name, 'My Resource Link') + self.assertEqual(field.type, 'ResourceLink') + self.assertEqual(len(field.allowedResources), 1) + self.assertEqual(field.allowedResources[0]['type'], 'Contentful:Entry') + self.assertEqual(field.allowedResources[0]['source'], 'crn:contentful:::content:spaces/yb41ceqgyiw5') + self.assertEqual(field.allowedResources[0]['contentTypes'], ['foo', 'bar']) + + @vcr.use_cassette('fixtures/content_type/create_no_fields.yaml') def test_create_content_type_no_fields(self): content_type = CLIENT.content_types(PLAYGROUND_SPACE, 'master').create(None, { From c436cb4fdef3035a649d460750ad7c4f2ccfa0ed Mon Sep 17 00:00:00 2001 From: Bhushan Lodha Date: Wed, 6 Mar 2024 09:14:40 +0530 Subject: [PATCH 2/2] updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f358ef..bd44f44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ # CHANGELOG ## Unreleased +* Adds support for cross-space references ## v2.13.1 * Preserve fields default values when updating content type [#90](https://github.com/contentful/contentful-management.py/issues/90)