Skip to content

Commit

Permalink
Fix: TypeError when using new 'refresh' commands (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
ababic authored Apr 24, 2024
1 parent 2fdd585 commit 56a80f0
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/wagtail_bynder/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,15 @@ def is_up_to_date(self, asset_data: dict[str, Any]) -> bool:
def update_from_asset_data(
self,
asset_data: dict[str, Any],
**kwargs,
) -> None:
"""
Update this object (without saving) to reflect values in `asset_data`,
which is a representation of the related asset from the Bynder API.
NOTE: Although this base implementation does nothing with them currently,
for compatibility reasons, ``**kwargs`` should always be accepted by all
implementations of this method.
"""
self.title = asset_data.get("name", self.title)
self.copyright = asset_data.get("copyright", self.copyright)
Expand Down Expand Up @@ -126,9 +131,17 @@ class Meta:
def extract_file_source(asset_data: dict[str, Any]) -> str:
raise NotImplementedError

def update_from_asset_data(self, asset_data: dict[str, Any]) -> None:
super().update_from_asset_data(asset_data)
if not self.file or self.asset_file_has_changed(asset_data):
def update_from_asset_data(
self, asset_data: dict[str, Any], *, force_download: bool = False, **kwargs
) -> None:
"""
Overrides ``BynderAssetMixin.update_from_asset_data()`` to explicitly
handle the ``force_download`` option that can be provided by management
commands, and to initiate downloading of the source file when it has
changed in some way.
"""
super().update_from_asset_data(asset_data, **kwargs)
if force_download or not self.file or self.asset_file_has_changed(asset_data):
self.update_file(asset_data)

def asset_file_has_changed(self, asset_data: dict[str, Any]) -> bool:
Expand Down Expand Up @@ -177,11 +190,17 @@ def save(self, *args, **kwargs):
super().save(*args, **kwargs)

def update_from_asset_data(
self,
asset_data: dict[str, Any],
self, asset_data: dict[str, Any], *, force_download: bool = False, **kwargs
) -> None:
"""
Overrides ``BynderAssetWithFileMixin.update_from_asset_data()`` to
handle conversion of focal points to focal areas.
"""

# Update the file and other field values without saving the changes
super().update_from_asset_data(asset_data)
super().update_from_asset_data(
asset_data, force_download=force_download, **kwargs
)

# Update the focal area if a focus point is set
focus_point = asset_data.get("activeOriginalFocusPoint")
Expand Down Expand Up @@ -374,7 +393,13 @@ def fallback_source_mimetype(self) -> str:
def update_from_asset_data(
self,
asset_data: dict[str, Any],
**kwargs,
) -> None:
"""
Overrides ``BynderAssetMixin.update_from_asset_data()`` to handle
setting of video-specific field values.
"""

primary_derivative_name = getattr(
settings, "BYNDER_VIDEO_PRIMARY_DERIVATIVE_NAME", "WebPrimary"
)
Expand Down Expand Up @@ -417,4 +442,4 @@ def update_from_asset_data(
self.original_width = int(asset_data["width"])
self.original_height = int(asset_data["height"])

super().update_from_asset_data(asset_data)
super().update_from_asset_data(asset_data, **kwargs)

0 comments on commit 56a80f0

Please sign in to comment.