Skip to content

Commit

Permalink
feat(history): add ng relations to history view
Browse files Browse the repository at this point in the history
resolves #1112
  • Loading branch information
sennierer authored and b1rger committed Aug 30, 2024
1 parent 7ef2959 commit 36b02ab
Showing 1 changed file with 42 additions and 7 deletions.
49 changes: 42 additions & 7 deletions apis_core/history/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from typing import Any

from django.conf import settings
from apis_core.generic.abc import GenericModel
from apis_core.apis_metainfo.models import RootObject
from django.urls import reverse
Expand Down Expand Up @@ -153,16 +155,49 @@ def get_create_version_url(self):
ct = ContentType.objects.get_for_model(self)
return reverse("apis_core:history:add_new_history_version", args=[ct, self.id])

def get_history_data(self):
from apis_core.apis_relations.models import TempTriple
def _get_historical_relations(self):
ret = []
if "apis_core.relations" in settings.INSTALLED_APPS:
from apis_core.relations.utils import relation_content_types

ct = ContentType.objects.get_for_model(self)

rel_content_types = relation_content_types(any_model=type(self))
rel_models = [ct.model_class() for ct in rel_content_types]
rel_history_models = [
model for model in rel_models if issubclass(model, VersionMixin)
]

for model in rel_history_models:
ret.append(
model.history.filter(
Q(subj_object_id=self.id, subj_content_type=ct)
| Q(obj_object_id=self.id, obj_content_type=ct)
).order_by("history_id")
)
return ret

def _get_historical_triples(self):
# TODO: this is a workaround to filter out Triple history entries and leave
# TempTriple entries only. Fix when we switch to new relations model.
ret = []
if "apis_core.apis_relations" in settings.INSTALLED_APPS:
from apis_core.apis_relations.models import TempTriple

ret.append(
TempTriple.history.filter(Q(subj=self) | Q(obj=self)).order_by(
"history_id"
)
)
return ret

def get_history_data(self):
data = []
prev_entry = None
# TODO: this is a workaround to filter out Triple history entries and leave
# TempTriple entries only. Fix when we switch to new relations model.
for entry in TempTriple.history.filter(
Q(subj_id=self.id) | Q(obj_id=self.id)
).order_by("history_id"):
queries = self._get_historical_relations() + self._get_historical_triples()
flatten_queries = [entry for query in queries for entry in query]

for entry in flatten_queries:
if prev_entry is not None:
if (
entry.history_date == prev_entry.history_date
Expand Down

0 comments on commit 36b02ab

Please sign in to comment.