Skip to content

Commit

Permalink
- Rename 'update_outdated_objects()' to 'process_batch()'
Browse files Browse the repository at this point in the history
- Rename 'get_outdated_objects()' to 'get_stale_objects()'
- Report batch numbers and size to the console
- Report the number of stale objects found for each batch to the console
  • Loading branch information
ababic committed Feb 24, 2024
1 parent 994b9ff commit 8432985
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/wagtail_bynder/management/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def handle(self, *args, **options):
f"Looking for {self.bynder_asset_type or 'all'} assets modified in the last {timespan_desc}"
)

self.batch_count = 1
self.bynder_client = get_bynder_client()
asset_dict: dict[str, dict[str, Any]] = {}

Expand All @@ -71,13 +72,12 @@ def handle(self, *args, **options):
asset_dict[asset["id"]] = asset
# Process the gathered assets once the batch reaches a certain size
if len(asset_dict) == self.page_size:
self.update_outdated_objects(asset_dict)
self.process_batch(asset_dict)
# Clear this batch to start another
asset_dict.clear()

# Process any remaining assets
if asset_dict:
self.update_outdated_objects(asset_dict)
self.process_batch(asset_dict)

def get_assets(self) -> Generator[dict[str, Any]]:
Expand All @@ -98,12 +98,26 @@ def get_assets(self) -> Generator[dict[str, Any]]:
results = self.bynder_client.asset_bank_client.media_list(query)
if not results:
break

yield from results

page += 1

def get_outdated_objects(self, assets: dict[str, dict[str, Any]]) -> QuerySet:
def process_batch(self, assets: dict[str, dict[str, Any]]) -> None:
"""
Identifies and updates (where needed) model objects to reflect changes
in the supplied 'batch' of Bynder assets.
"""
self.stdout.write(
f"Processing batch {self.batch_count} ({len(assets)} assets)..."
)
self.batch_count += 1

stale = self.get_stale_objects(assets)
self.stdout.write(f"{len(stale)} stale objects were found for this batch.")
for obj in stale:
data = assets.get(obj.bynder_id)
self.update_object(obj, data)

def get_stale_objects(self, assets: dict[str, dict[str, Any]]) -> QuerySet:
"""
Return a queryset of model instances that represent items in the supplied
batch of assets, and are out-of-sync with the data in Bynder (and
Expand All @@ -116,15 +130,6 @@ def get_outdated_objects(self, assets: dict[str, dict[str, Any]]) -> QuerySet:
q |= Q(bynder_id=id, bynder_last_modified__lt=asset["dateModified"])
return self.model.objects.filter(q)

def update_outdated_objects(self, assets: dict[str, dict[str, Any]]) -> None:
"""
Identifies and updates (where needed) model objects to reflect changes
in the supplied 'batch' of Bynder assets.
"""
for obj in self.get_outdated_objects(assets):
data = assets.get(obj.bynder_id)
self.update_object(obj, data)

def update_object(self, obj: Model, asset_data: dict[str:Any]) -> None:
self.stdout.write("\n")
self.stdout.write(f"Updating object for asset '{asset_data['id']}'")
Expand Down

0 comments on commit 8432985

Please sign in to comment.