From 66734d37c1c7919dba09f0983d89f45bf27a7294 Mon Sep 17 00:00:00 2001 From: devketanpro Date: Mon, 14 Oct 2024 11:01:37 +0530 Subject: [PATCH 1/3] fix backend validation issue on non enabled fields --- client/components/ContentProfiles/ContentProfileModal.tsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/client/components/ContentProfiles/ContentProfileModal.tsx b/client/components/ContentProfiles/ContentProfileModal.tsx index 5f9b79c83..513f02d84 100644 --- a/client/components/ContentProfiles/ContentProfileModal.tsx +++ b/client/components/ContentProfiles/ContentProfileModal.tsx @@ -392,6 +392,11 @@ class ContentProfileModalComponent extends React.Component { fields.forEach((item, index) => { profile.editor[item.name] = {...item.field}; profile.editor[item.name].index = index; + + // If the field is disabled in the editor, mark it as not required to avoid backend validation issues. + if (item.field?.enabled == false) { + profile.schema[item.name].required = false; + } }); return key === 'profile' ? From 4ab8368991a89398b30517156c49a8bd7bf5f0e3 Mon Sep 17 00:00:00 2001 From: devketanpro Date: Tue, 15 Oct 2024 13:47:56 +0530 Subject: [PATCH 2/3] remove frontend chnages and implement backend --- .../ContentProfiles/ContentProfileModal.tsx | 5 - server/planning/events/events_tests.py | 91 +++++++++++++++++++ server/planning/validate/planning_validate.py | 15 +-- 3 files changed, 100 insertions(+), 11 deletions(-) diff --git a/client/components/ContentProfiles/ContentProfileModal.tsx b/client/components/ContentProfiles/ContentProfileModal.tsx index 513f02d84..5f9b79c83 100644 --- a/client/components/ContentProfiles/ContentProfileModal.tsx +++ b/client/components/ContentProfiles/ContentProfileModal.tsx @@ -392,11 +392,6 @@ class ContentProfileModalComponent extends React.Component { fields.forEach((item, index) => { profile.editor[item.name] = {...item.field}; profile.editor[item.name].index = index; - - // If the field is disabled in the editor, mark it as not required to avoid backend validation issues. - if (item.field?.enabled == false) { - profile.schema[item.name].required = false; - } }); return key === 'profile' ? diff --git a/server/planning/events/events_tests.py b/server/planning/events/events_tests.py index 1fb210264..d04fe0d8f 100644 --- a/server/planning/events/events_tests.py +++ b/server/planning/events/events_tests.py @@ -607,3 +607,94 @@ def test_new_planning_is_published_when_adding_to_published_event(self): planning_item = planning_service.find_one(req=None, _id=planning_id) self.assertIsNotNone(planning_item) self.assertEqual(planning_item["pubstatus"], POST_STATE.USABLE) + + def test_related_planning_item_fields_validation_on_post(self): + with self.app.app_context(): + events_service = get_resource_service("events") + planning_service = get_resource_service("planning") + event = { + "type": "event", + "_id": "1234", + "occur_status": { + "qcode": "eocstat:eos5", + "name": "Planned, occurs certainly", + "label": "Planned, occurs certainly", + }, + "dates": { + "start": datetime(2099, 11, 21, 11, 00, 00, tzinfo=pytz.UTC), + "end": datetime(2099, 11, 21, 12, 00, 00, tzinfo=pytz.UTC), + "tz": "Asia/Calcutta", + }, + "calendars": [], + "state": "draft", + "language": "en", + "languages": ["en"], + "place": [], + "_time_to_be_confirmed": False, + "name": "Demo ", + "update_method": "single", + } + event_id = events_service.post([event]) + planning = { + "planning_date": datetime(2099, 11, 21, 12, 00, 00, tzinfo=pytz.UTC), + "name": "Demo 1", + "place": [], + "language": "en", + "type": "planning", + "slugline": "slug", + "agendas": [], + "languages": ["en"], + "event_item": event_id[0], + "coverages": [ + { + "coverage_id": "urn:newsmle264a179-5b1a-4b52-b73b-332660848cae", + "planning": { + "scheduled": datetime(2099, 11, 21, 12, 00, 00, tzinfo=pytz.UTC), + "g2_content_type": "text", + "language": "en", + "genre": "None", + }, + "news_coverage_status": { + "qcode": "ncostat:int", + "name": "coverage intended", + "label": "Planned", + }, + "workflow_status": "draft", + "assigned_to": {}, + "firstcreated": datetime(2099, 11, 21, 12, 00, 00, tzinfo=pytz.UTC), + } + ], + } + planning_id = planning_service.post([planning]) + self.app.data.insert( + "planning_types", + [ + { + "_id": "event", + "name": "event", + "editor": { + "related_plannings": {"enabled": True}, + }, + "schema": { + "related_plannings": {"planning_auto_publish": True}, + }, + }, + { + "_id": "planning", + "name": "planning", + "editor": {"subject": {"enabled": False}}, + "schema": {"subject": {"required": True}}, + }, + ], + ) + get_resource_service("events_post").post( + [{"event": event_id[0], "pubstatus": "usable", "update_method": "single", "failed_planning_ids": []}] + ) + + event_item = events_service.find_one(req=None, _id=event_id[0]) + self.assertEqual(len([event_item]), 1) + self.assertEqual(event_item.get("state"), "scheduled") + + planning_item = planning_service.find_one(req=None, _id=planning_id[0]) + self.assertEqual(len([planning_item]), 1) + self.assertEqual(planning_item.get("state"), "scheduled") diff --git a/server/planning/validate/planning_validate.py b/server/planning/validate/planning_validate.py index bb536b717..2d8afdd6e 100644 --- a/server/planning/validate/planning_validate.py +++ b/server/planning/validate/planning_validate.py @@ -17,6 +17,7 @@ from apps.validate.validate import SchemaValidator as Validator from copy import deepcopy +from planning.content_profiles.utils import get_enabled_fields REQUIRED_ERROR = "{} is a required field" @@ -128,14 +129,16 @@ def _get_validator(self, doc): return get_resource_service("planning_types").find_one(req=None, name=doc[ITEM_TYPE]) def _get_validator_schema(self, validator, validate_on_post): - """Get schema for given validator. + """Get schema for a given validator, excluding fields with None values, + and only include fields that are in enabled_fields.""" - And make sure there is no `None` value which would raise an exception. - """ + enabled_fields = get_enabled_fields(validator) return { - field: get_validator_schema(schema) - for field, schema in validator["schema"].items() - if schema and schema.get("validate_on_post", False) == validate_on_post + field: get_validator_schema(field_schema) + for field, field_schema in validator["schema"].items() + if field in enabled_fields + and field_schema + and field_schema.get("validate_on_post", False) == validate_on_post } def _validate(self, doc): From d45dfa4ba2e419963fdd4b0e516efb6cd6e14a96 Mon Sep 17 00:00:00 2001 From: devketanpro Date: Tue, 15 Oct 2024 14:17:52 +0530 Subject: [PATCH 3/3] update behave tests --- server/features/planning_validate.feature | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/server/features/planning_validate.feature b/server/features/planning_validate.feature index 01d9d91fc..0e5935482 100644 --- a/server/features/planning_validate.feature +++ b/server/features/planning_validate.feature @@ -30,6 +30,11 @@ Feature: Planning Validate } }, { "_id": "planning", "name": "planning", + "editor":{ + "place": { + "enabled":true + } + }, "schema": { "slugline": { "type": "string",