Skip to content

Commit c8c6723

Browse files
committed
Apply requested changes for PR#3969
1 parent 5008662 commit c8c6723

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

kpi/exceptions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ def __init__(self, *args, **kwargs):
1919
)
2020

2121

22+
class AssetAdjustContentError(Exception):
23+
pass
24+
25+
2226
class AttachmentNotFoundException(Exception):
2327
pass
2428

kpi/models/asset.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
)
4747
from kpi.deployment_backends.mixin import DeployableMixin
4848
from kpi.exceptions import (
49+
AssetAdjustContentError,
4950
BadPermissionsException,
5051
DeploymentDataException,
5152
)
@@ -698,13 +699,34 @@ def save(
698699
)
699700
return
700701

702+
update_content_field = update_fields and 'content' in update_fields
703+
704+
# Raise an exception if we want to adjust asset content
705+
# (i.e. `adjust_content` is True) but we are trying to update only
706+
# certain fields and `content` is not part of them, or if we
707+
# specifically ask to not adjust asset content, but trying to
708+
# update only certain fields and `content` is one of them.
709+
if (
710+
(adjust_content and update_fields and 'content' not in update_fields)
711+
or
712+
(not adjust_content and update_content_field)
713+
):
714+
raise AssetAdjustContentError
715+
716+
# If `content` is part of the updated fields, `summary` and
717+
# `report_styles` must be too.
718+
if update_content_field:
719+
update_fields += ['summary', 'report_styles']
720+
# Avoid duplicates
721+
update_fields = list(set(update_fields))
722+
701723
# `self.content` must be the second condition. We do not want to get
702724
# the value of `self.content` if first condition is false.
703725
# The main purpose of this is avoid to load `self.content` when it is
704726
# deferred (see `AssetNestedObjectViewsetMixin.asset`) and does not need
705727
# to be updated.
706728
if (
707-
(not update_fields or update_fields and 'content' in update_fields)
729+
(not update_fields or update_content_field)
708730
and self.content is None
709731
):
710732
self.content = {}

0 commit comments

Comments
 (0)