Skip to content

Commit

Permalink
Fix: Planning items created via an event are not always scheduled, bu…
Browse files Browse the repository at this point in the history
…t are being kept in draft mode [SDBELGA-879] (#2113)

* fix backend validation issue on non enabled fields

* remove frontend chnages and implement backend

* update behave tests
  • Loading branch information
devketanpro authored Oct 16, 2024
1 parent 47a308c commit 575f205
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 6 deletions.
5 changes: 5 additions & 0 deletions server/features/planning_validate.feature
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ Feature: Planning Validate
}
}, {
"_id": "planning", "name": "planning",
"editor":{
"place": {
"enabled":true
}
},
"schema": {
"slugline": {
"type": "string",
Expand Down
91 changes: 91 additions & 0 deletions server/planning/events/events_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
15 changes: 9 additions & 6 deletions server/planning/validate/planning_validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 575f205

Please sign in to comment.