From a5b99e022efabc4465291e1183d8ae84e3bc3be0 Mon Sep 17 00:00:00 2001 From: devketanpro Date: Tue, 2 Apr 2024 17:52:56 +0530 Subject: [PATCH 1/2] change place of validation in which it can work when user double click --- server/planning/planning/planning_post.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/server/planning/planning/planning_post.py b/server/planning/planning/planning_post.py index cb24df166..6bc81ae47 100644 --- a/server/planning/planning/planning_post.py +++ b/server/planning/planning/planning_post.py @@ -63,6 +63,9 @@ def create(self, docs, **kwargs): if not plan: abort(412) + if kwargs.get("related_planning"): + self.validate_related_item(plan) + self.validate_post_state(doc["pubstatus"]) if event and doc["pubstatus"] == POST_STATE.USABLE: self.post_associated_event(event) @@ -154,9 +157,6 @@ def post_planning(self, plan, new_post_state, assignments_to_delete, **kwargs): updated_plan = get_resource_service("planning").update(plan["_id"], updates, plan) plan.update(updated_plan) - if kwargs.get("related_planning"): - self.validate_related_item(plan) - # Set a version number version, plan = get_version_item_for_post(plan) self.publish_planning(plan, version) From bc1a1b89491e31e994c167471f75df800a2dc1f9 Mon Sep 17 00:00:00 2001 From: devketanpro Date: Fri, 5 Apr 2024 11:44:28 +0530 Subject: [PATCH 2/2] add tests --- server/planning/events/events_tests.py | 103 +++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/server/planning/events/events_tests.py b/server/planning/events/events_tests.py index a399361c4..4152359a8 100644 --- a/server/planning/events/events_tests.py +++ b/server/planning/events/events_tests.py @@ -8,6 +8,7 @@ from planning.common import format_address, POST_STATE from planning.item_lock import LockService from planning.events.events import generate_recurring_dates +from werkzeug.exceptions import BadRequest class EventTestCase(TestCase): @@ -596,3 +597,105 @@ 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_validation_on_post(self): + """ + check planning item fields validation + if validation fails, plannning item is not posted. + """ + events_service = get_resource_service("events") + planning_service = get_resource_service("planning") + + with self.app.app_context(): + 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": {"slugline": {"enabled": True}}, + "schema": {"slugline": {"required": True}}, + }, + ], + ) + + event_id = events_service.post( + [ + { + "type": "event", + "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": "Australia/Sydney", + }, + "state": "draft", + "name": "Foo event one", + } + ] + )[0] + planning_id = planning_service.post( + [ + { + "planning_date": datetime(2099, 11, 21, 12, 00, 00, tzinfo=pytz.UTC), + "name": "foo planning 1", + "type": "planning", + "event_item": event_id, + } + ] + )[0] + try: + get_resource_service("events_post").post( + [ + { + "event": event_id, + "pubstatus": "usable", + "update_method": "single", + } + ] + ) + except BadRequest as e: + self.assertEqual(str(e), "400 Bad Request: ['Related planning : SLUGLINE is a required field']") + + planning_item = planning_service.find_one(req=None, _id=planning_id) + self.assertEqual(planning_item["state"], "draft") + + # try to re-post an event. + try: + get_resource_service("events_post").post( + [ + { + "event": event_id, + "pubstatus": "usable", + "update_method": "single", + } + ] + ) + except BadRequest as e: + self.assertEqual(str(e), "400 Bad Request: ['Related planning : SLUGLINE is a required field']") + + # udpate slugline + planning_service.patch(planning_id, {"slugline": "update slug"}) + + get_resource_service("events_post").post( + [ + { + "event": event_id, + "pubstatus": "usable", + "update_method": "single", + } + ] + ) + planning_item = planning_service.find_one(req=None, _id=planning_id) + self.assertIsNotNone(planning_item) + self.assertEqual(planning_item["state"], "scheduled")