Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge template and planning subject when creating item #2005

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions server/features/assignments_content.feature
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,18 @@ Feature: Assignment content
"data": {
"profile": "#content_types._id#",
"slugline": "Foo",
"headline": "Headline From Template"
"headline": "Headline From Template",
"subject": [
{"name": "Template subject", "qcode": "template", "scheme": "templatecv"},
{"name": "Conflict", "qcode": "conflict", "scheme": "shared"},
{"name": "Single A", "qcode": "singlea", "scheme": "single"}
],
"place": [
{"name": "from template", "qcode": "123"}
],
"anpa_category": [
{"name": "from template", "qcode": "cata"}
]
}
}
]
Expand Down Expand Up @@ -50,7 +61,8 @@ Feature: Assignment content
{"is_active": true, "qcode": "ncostat:onreq", "name": "coverage upon request",
"label": "On request"}
]
}
},
{"_id": "single", "selection_type": "single selection"}
]
"""
When we post to "/planning"
Expand All @@ -59,7 +71,19 @@ Feature: Assignment content
{
"item_class": "item class value",
"slugline": "test slugline",
"planning_date": "2016-01-02"
"planning_date": "2016-01-02",
"subject": [
{"name": "Planning subject", "qcode": "planning", "scheme": "planning-cv"},
{"name": "Conflict", "qcode": "conflict", "scheme": "shared"},
{"name": "Extra", "qcode": "extra", "scheme": "shared"},
{"name": "Single B", "qcode": "singleb", "scheme": "single"}
],
"place": [
{"name": "from planning", "qcode": "345"}
],
"anpa_category": [
{"name": "from planning", "qcode": "catb"}
]
}
]
"""
Expand Down Expand Up @@ -188,7 +212,7 @@ Feature: Assignment content

@auth
@vocabularies
Scenario: Create content with headline and abstract derived from the Assignment
Scenario: Create content will inherit metadata from template and assignment
When we patch "/planning/#planning._id#"
"""
{
Expand Down Expand Up @@ -243,7 +267,22 @@ Feature: Assignment content
"headline": "Headline From Template",
"abstract": "<p>test description</p>",
"profile": "#content_types._id#",
"flags": {"marked_for_not_publication": false, "overide_auto_assign_to_workflow": "__no_value__"}
"flags": {"marked_for_not_publication": false, "overide_auto_assign_to_workflow": "__no_value__"},
"subject": [
{"name": "Template subject", "qcode": "template", "scheme": "templatecv"},
{"name": "Planning subject", "qcode": "planning", "scheme": "planning-cv"},
{"name": "Conflict", "qcode": "conflict", "scheme": "shared"},
{"name": "Extra", "qcode": "extra", "scheme": "shared"},
{"name": "Single A", "qcode": "singlea", "scheme": "single"}
],
"place": [
{"name": "from template", "qcode": "123"},
{"name": "from planning", "qcode": "345"}
],
"anpa_category": [
{"name": "from template", "qcode": "cata"},
{"name": "from planning", "qcode": "catb"}
]
}
"""

Expand Down
40 changes: 38 additions & 2 deletions server/planning/assignments/assignments_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,13 @@
from planning.signals import assignment_content_create


FIELDS_TO_COPY = ("anpa_category", "subject", "urgency", "place")
FIELDS_TO_COPY = ("urgency",)
FIELDS_TO_OVERRIDE = [
"urgency",
"slugline",
"ednote",
"abstract",
"headline",
"ednote",
"language",
]

Expand Down Expand Up @@ -90,6 +89,10 @@ def get_item_from_assignment(assignment, template=None):
if planning.get(field):
item[field] = deepcopy(planning[field])

merge_subject(item, planning)
merge_list("place", item, planning)
merge_list("anpa_category", item, planning)

if assignment.get("description_text"):
item["abstract"] = "<p>{}</p>".format(assignment["description_text"])

Expand Down Expand Up @@ -321,3 +324,36 @@ class AssignmentsContentResource(Resource):
item_methods = []

privileges = {"POST": "archive"}


def merge_subject(item, planning):
if not planning.get("subject"):
return
subject = item.setdefault("subject", [])
vocabularies = get_resource_service("vocabularies").get_from_mongo(
req=None, lookup={"selection_type": "single selection"}, projection={"_id": 1}
)
single_value_vocabularies = set([v["_id"] for v in vocabularies])
for s in planning["subject"]:
if s.get("scheme") in single_value_vocabularies:
if find_subject(subject, s.get("scheme")):
continue
elif find_subject(subject, s.get("scheme"), s.get("qcode")):
continue

subject.append(s)


def merge_list(field, item, planning):
if not planning.get(field):
return
item_values = item.setdefault(field, [])
for value in planning.get(field):
if value.get("qcode") not in set([v.get("qcode") for v in item_values]):
item_values.append(value)


def find_subject(subject, scheme, qcode=None):
for s in subject:
if s.get("scheme") == scheme and (qcode is None or s.get("qcode") == qcode):
return s
Loading