Skip to content

Commit

Permalink
fix(history)!: drop apis_relation related code parts
Browse files Browse the repository at this point in the history
`apis_relations` is deprecated, therefore we drop all remaining code
parts accessing it.

Closes: #1536
  • Loading branch information
b1rger committed Jan 20, 2025
1 parent 577c932 commit 8abc968
Show file tree
Hide file tree
Showing 5 changed files with 2 additions and 193 deletions.
63 changes: 2 additions & 61 deletions apis_core/history/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import inspect
from datetime import datetime
from typing import Any

import django
Expand All @@ -14,7 +13,6 @@
from simple_history import utils
from simple_history.models import HistoricalRecords

from apis_core.apis_metainfo.models import RootObject
from apis_core.generic.abc import GenericModel


Expand Down Expand Up @@ -56,51 +54,8 @@ class Meta:
),
]

def get_triples_for_version(
self,
only_latest: bool = True,
history_date: datetime = None,
filter_for_triples: bool = True,
):
"""returns all triples for a specific version of a model instance.
If only_latest is True, only the latest version of a triple is returned."""
from apis_core.apis_relations.models import TempTriple

if not isinstance(self.instance, RootObject):
return TempTriple.objects.none()

if history_date is None:
filter_date = (
self.next_record.history_date if self.next_record else datetime.now()
)
else:
filter_date = history_date
triples = TempTriple.history.filter(
Q(subj=self.instance) | Q(obj=self.instance), history_date__lte=filter_date
)
if self.version_tag and filter_for_triples:
triples = triples.filter(
Q(version_tag=self.version_tag)
| Q(version_tag__contains=f"{self.version_tag},")
)
if only_latest:
triples = triples.latest_of_each()
if filter_for_triples:
triples = triples.exclude(history_type="-")
return triples

def set_version_tag(self, tag: str, include_triples: bool = True):
def set_version_tag(self, tag: str):
self.version_tag = tag
if include_triples:
triples = self.get_triples_for_version(filter_for_triples=False)
for triple in triples:
if triple.version_tag is None:
triple.version_tag = tag
else:
triple.version_tag = f"{triple.version_tag},{tag},".replace(
",,", ","
)
triple.save()
self.save()

def get_absolute_url(self):
Expand Down Expand Up @@ -178,24 +133,10 @@ def _get_historical_relations(self):
)
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
queries = self._get_historical_relations() + self._get_historical_triples()
queries = self._get_historical_relations()
flatten_queries = [entry for query in queries for entry in query]

for entry in flatten_queries:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,3 @@
<div class="card-body">{% include template_list %}</div>
</div>
{% endblock col-zero %}

{% block col-one %}
<div class="card">
<div class="card-body">
{% object_relations_history as object_relations %}
{% for rel in object_relations %}
{% if rel.1.data|length > 0 %}
<h5>{{ rel.0|title }}</h5>
<div id="tab_{{ rel.2 }}">{% render_table rel.1 %}</div>
{% endif %}
{% endfor %}
</div>
</div>
{% endblock col-one %}
7 changes: 0 additions & 7 deletions apis_core/history/templatetags/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@
from django.utils.safestring import mark_safe

from apis_core.history.serializers import HistoryLogSerializer
from apis_core.history.utils import triple_sidebar_history
from apis_core.utils.helpers import get_html_diff

register = template.Library()


@register.simple_tag(takes_context=True)
def object_relations_history(context, detail=True):
obj = context["object"]
return triple_sidebar_history(obj, context["request"], detail)


@register.filter
def get_history_data(obj):
data = HistoryLogSerializer(obj.get_history_data(), many=True).data
Expand Down
50 changes: 0 additions & 50 deletions apis_core/history/tests/test_simple_history.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from datetime import datetime, timedelta

from django.contrib.contenttypes.models import ContentType
from django.test import TestCase

from apis_core.apis_relations.models import Property, TempTriple
from sample_project.models import Person, Place, Profession


Expand All @@ -15,11 +13,6 @@ def setUp(self):
self.Place = Place
self.Profession = Profession

prop = Property.objects.create(
name_forward="geboren in", name_reverse="Geburtsort von"
)
prop.subj_class.add(ContentType.objects.get(model="person"))
prop.obj_class.add(ContentType.objects.get(model="place"))
Person.objects.create(forename="John", surname="Doe")
Place.objects.create(
label="Steyr", _history_date=datetime.now() - timedelta(hours=0, minutes=50)
Expand All @@ -36,45 +29,6 @@ def test_history(self):
self.assertEqual("Jane", pers.history.most_recent().forename)
self.assertEqual("John", pers.history.earliest().forename)

def test_history_through_triples(self):
"""Tests the newly introduced function for retrieving triples for a specific version of a model instance."""
# first entity
pers = self.Person.objects.get(forename="John")
# second entity
place = self.Place.objects.first()
# create a triple
tt1 = TempTriple.objects.create(
subj=pers, prop=Property.objects.first(), obj=place
)
# test that we get one triple version for both entities
self.assertEqual(len(pers.history.earliest().get_triples_for_version()), 1)
self.assertEqual(len(place.history.earliest().get_triples_for_version()), 1)
# change the triple and test again
tt1.start_date_written = "2020-01-01"
tt1.save()
triples = pers.history.earliest().get_triples_for_version()
self.assertEqual(len(triples), 1)
self.assertEqual(triples[0].start_date_written, "2020-01-01")
# change the place and test again
place2 = self.Place.objects.create(label="testplace")
tt1.obj = place2
tt1.save()
triples = place.history.earliest().get_triples_for_version()
self.assertEqual(len(triples), 1)
self.assertEqual(triples[0].obj, place)
triples = place2.history.earliest().get_triples_for_version()
self.assertEqual(len(triples), 1)
self.assertEqual(triples[0].obj, place2)
# given that the function should return the latest version of a triple, we should get the new place
triples = pers.history.earliest().get_triples_for_version()
self.assertEqual(len(triples), 1)
self.assertEqual(triples[0].obj, place2)
# test retrieving triples for a specific datetime
triples = pers.history.earliest().get_triples_for_version(
history_date=datetime.now() - timedelta(hours=0, minutes=40)
)
self.assertEqual(len(triples), 0)

def test_history_tag(self):
"""Tests the version tag function."""
pers = self.Person.objects.get(forename="John")
Expand All @@ -85,12 +39,8 @@ def test_history_tag(self):
self.assertEqual(pers_history[0].version_tag, "test_tag")
# test with TemTriple
pers2 = self.Person.objects.create(forename="Jane", surname="Doe")
place = self.Place.objects.first()
TempTriple.objects.create(subj=pers2, prop=Property.objects.first(), obj=place)
pers2.history.latest().set_version_tag("test_tag")
self.assertEqual(pers2.history.latest().version_tag, "test_tag")
triples = pers2.history.earliest().get_triples_for_version()
self.assertEqual(triples[0].version_tag, "test_tag")

def test_history_delete_entry(self):
"""Tests the deletion of an entry."""
Expand Down
61 changes: 0 additions & 61 deletions apis_core/history/utils.py

This file was deleted.

0 comments on commit 8abc968

Please sign in to comment.