Skip to content

Commit

Permalink
BookDataModel.merge_into: return and log absorbed fields
Browse files Browse the repository at this point in the history
  • Loading branch information
BartSchuurmans committed Mar 2, 2024
1 parent 8ef6482 commit 3796412
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
3 changes: 2 additions & 1 deletion bookwyrm/management/commands/deduplicate_book_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def dedupe_model(model):
print(f"merging into {canonical.remote_id} based on {field.name} {value}:")
for obj in objs[1:]:
print(f"- {obj.remote_id}")
obj.merge_into(canonical)
absorbed_fields = obj.merge_into(canonical)
print(f" absorbed fields: {absorbed_fields}")


class Command(BaseCommand):
Expand Down
4 changes: 3 additions & 1 deletion bookwyrm/management/merge_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ def handle(self, *args, **options):
print("other book doesn’t exist!")
return

other.merge_into(canonical)
absorbed_fields = other.merge_into(canonical)
print(f"{other.remote_id} has been merged into {canonical.remote_id}")
print(f"absorbed fields: {absorbed_fields}")
12 changes: 8 additions & 4 deletions bookwyrm/models/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from itertools import chain
import re
from typing import Any
from typing import Any, Dict
from typing_extensions import Self

from django.contrib.postgres.search import SearchVectorField
Expand Down Expand Up @@ -108,12 +108,12 @@ def broadcast(self, activity, sender, software="bookwyrm", **kwargs):
"""only send book data updates to other bookwyrm instances"""
super().broadcast(activity, sender, software=software, **kwargs)

def merge_into(self, canonical: Self) -> None:
def merge_into(self, canonical: Self) -> Dict[str, Any]:
"""merge this entity into another entity"""
if canonical.id == self.id:
raise ValueError(f"Cannot merge {self} into itself")

canonical.absorb_data_from(self)
absorbed_fields = canonical.absorb_data_from(self)
canonical.save()

self.merged_model.objects.create(deleted_id=self.id, merged_into=canonical)
Expand Down Expand Up @@ -145,9 +145,11 @@ def merge_into(self, canonical: Self) -> None:
getattr(related_obj, related_field).remove(self)

self.delete()
return absorbed_fields

def absorb_data_from(self, other: Self) -> None:
def absorb_data_from(self, other: Self) -> Dict[str, Any]:
"""fill empty fields with values from another entity"""
absorbed_fields = {}
for data_field in self._meta.get_fields():
if not hasattr(data_field, "activitypub_field"):
continue
Expand All @@ -156,6 +158,8 @@ def absorb_data_from(self, other: Self) -> None:
continue
if not getattr(self, data_field.name):
setattr(self, data_field.name, data_value)
absorbed_fields[data_field.name] = data_value
return absorbed_fields


class MergedBookDataModel(models.Model):
Expand Down

0 comments on commit 3796412

Please sign in to comment.