Skip to content

Commit

Permalink
Validation of planning items when posted alongside events [SDESK-7202] (
Browse files Browse the repository at this point in the history
#1941)

* change place of validation in which it can work when user double click

* add tests
  • Loading branch information
devketanpro authored Apr 8, 2024
1 parent 3d7ba0b commit 9a1d24a
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
103 changes: 103 additions & 0 deletions server/planning/events/events_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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")
6 changes: 3 additions & 3 deletions server/planning/planning/planning_post.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9a1d24a

Please sign in to comment.