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", 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):