Skip to content

Commit

Permalink
refactor(generic,apis_entities): move merge logic to GenericModel
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Dec 5, 2024
1 parent 2f52529 commit 9ff1de5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 43 deletions.
43 changes: 0 additions & 43 deletions apis_core/apis_entities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@
import re

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.db.models.query import QuerySet
from django.db.models.signals import post_save
from django.dispatch import receiver
from django.urls import NoReverseMatch, reverse

from apis_core.apis_metainfo.models import RootObject, Uri
from apis_core.generic import signals

NEXT_PREV = getattr(settings, "APIS_NEXT_PREV", True)

Expand Down Expand Up @@ -90,46 +87,6 @@ def merge_start_date_written(self, other):
def merge_end_date_written(self, other):
self.end_date_written = self.end_date_written or other.end_date_written

def merge_with(self, entities):
if self in entities:
entities.remove(self)
origin = self.__class__
signals.pre_merge_with.send(sender=origin, instance=self, entities=entities)

# TODO: check if these imports can be put to top of module without
# causing circular import issues.
from apis_core.apis_metainfo.models import Uri

e_a = type(self).__name__
self_model_class = ContentType.objects.get(model__iexact=e_a).model_class()
if isinstance(entities, int):
entities = self_model_class.objects.get(pk=entities)
if not isinstance(entities, list) and not isinstance(entities, QuerySet):
entities = [entities]
entities = [
self_model_class.objects.get(pk=ent) if isinstance(ent, int) else ent
for ent in entities
]
for ent in entities:
e_b = type(ent).__name__
if e_a != e_b:
continue
for f in ent._meta.local_many_to_many:
if not f.name.endswith("_set"):
sl = list(getattr(self, f.name).all())
for s in getattr(ent, f.name).all():
if s not in sl:
getattr(self, f.name).add(s)
Uri.objects.filter(root_object=ent).update(root_object=self)

for ent in entities:
self.merge_fields(ent)

signals.post_merge_with.send(sender=origin, instance=self, entities=entities)

for ent in entities:
ent.delete()

def get_serialization(self):
from apis_core.apis_entities.serializers_generic import EntitySerializer

Expand Down
42 changes: 42 additions & 0 deletions apis_core/generic/abc.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from django.contrib.contenttypes.models import ContentType
from django.db.models import BooleanField, CharField, TextField
from django.db.models.query import QuerySet
from django.urls import reverse

from apis_core.generic.helpers import permission_fullname
from apis_core.generic.signals import post_merge_with, pre_merge_with


class GenericModel:
Expand Down Expand Up @@ -121,3 +123,43 @@ def merge_fields(self, other):
if newval != getattr(self, field.name):
setattr(self, field.name, newval)
self.save()

def merge_with(self, entities):
if self in entities:
entities.remove(self)
origin = self.__class__
pre_merge_with.send(sender=origin, instance=self, entities=entities)

# TODO: check if these imports can be put to top of module without
# causing circular import issues.
from apis_core.apis_metainfo.models import Uri

e_a = type(self).__name__
self_model_class = ContentType.objects.get(model__iexact=e_a).model_class()
if isinstance(entities, int):
entities = self_model_class.objects.get(pk=entities)
if not isinstance(entities, list) and not isinstance(entities, QuerySet):
entities = [entities]
entities = [
self_model_class.objects.get(pk=ent) if isinstance(ent, int) else ent
for ent in entities
]
for ent in entities:
e_b = type(ent).__name__
if e_a != e_b:
continue
for f in ent._meta.local_many_to_many:
if not f.name.endswith("_set"):
sl = list(getattr(self, f.name).all())
for s in getattr(ent, f.name).all():
if s not in sl:
getattr(self, f.name).add(s)
Uri.objects.filter(root_object=ent).update(root_object=self)

for ent in entities:
self.merge_fields(ent)

post_merge_with.send(sender=origin, instance=self, entities=entities)

for ent in entities:
ent.delete()

0 comments on commit 9ff1de5

Please sign in to comment.