Skip to content

Commit

Permalink
Planning: When starting to work on an Assignment prefill the article …
Browse files Browse the repository at this point in the history
…with the fields matching the language of the Coverage [SDESK-7061] (#1859)

* Planning: When starting to work on an Assignment prefill the article with the fields matching the language of the Coverage [SDESK-7061]

* fix black

* addressed comment
  • Loading branch information
devketanpro authored Oct 9, 2023
1 parent b7f47d7 commit 7743bf2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 7 additions & 1 deletion server/planning/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
]


def create_item_from_template(doc, extra_fields_to_override=None):
def create_item_from_template(doc, extra_fields_to_override=None, translations=None):
fields_to_override = deepcopy(TEMPLATE_FIELDS_TO_OVERRIDE)
if extra_fields_to_override is not None:
fields_to_override.extend(extra_fields_to_override)
Expand All @@ -38,6 +38,12 @@ def create_item_from_template(doc, extra_fields_to_override=None):
# and apply them if any found
updates = {key: val for key, val in doc.items() if key in fields_to_override}

if translations is not None:
translated_value = {
entry["field"]: entry["value"] for entry in translations or [] if entry["language"] == doc.get("language")
}
updates.update({key: val for key, val in translated_value.items() if key in fields_to_override})

if len(updates):
archive_service.patch(item_id, updates)

Expand Down
10 changes: 7 additions & 3 deletions server/planning/assignments/assignments_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def get_item_from_assignment(assignment, template=None):
:return dict: item
"""
item = {}
translations = {}
if not assignment:
return item

Expand Down Expand Up @@ -96,6 +97,9 @@ def get_item_from_assignment(assignment, template=None):
if not item.get("flags"):
item["flags"] = {}

if planning.get("translations"):
translations = planning.get("translations")

item["flags"]["marked_for_not_publication"] = (planning.get("flags") or {}).get(
"marked_for_not_publication"
) or False
Expand Down Expand Up @@ -133,7 +137,7 @@ def get_item_from_assignment(assignment, template=None):
if language:
item["language"] = language

return item
return item, translations


class AssignmentsContentService(Service):
Expand All @@ -147,7 +151,7 @@ def create(self, docs, **kwargs):
assignments_service = get_resource_service("assignments")
for doc in docs:
assignment = assignments_service.find_one(req=None, _id=doc.pop("assignment_id"))
item = get_item_from_assignment(assignment, doc.pop("template_name", None))
item, translations = get_item_from_assignment(assignment, doc.pop("template_name", None))
item[config.VERSION] = 1
item.setdefault("type", "text")
item["assignment_id"] = assignment[config.ID_FIELD]
Expand Down Expand Up @@ -177,7 +181,7 @@ def create(self, docs, **kwargs):
)
else:
# create content
item = create_item_from_template(item, FIELDS_TO_OVERRIDE)
item = create_item_from_template(item, FIELDS_TO_OVERRIDE, translations)

# create delivery references
get_resource_service("delivery").post(
Expand Down

0 comments on commit 7743bf2

Please sign in to comment.