Skip to content

Commit

Permalink
feat(history): add new history plugin
Browse files Browse the repository at this point in the history
resolves #340
resolves #242
  • Loading branch information
sennierer committed Mar 27, 2024
1 parent ea58f54 commit 7a2e164
Show file tree
Hide file tree
Showing 24 changed files with 1,331 additions and 498 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
href="{% url "apis_core:apis_entities:generic_entities_merge_view" modelname object.id %}">
<span class="material-symbols-outlined material-symbols-align">cell_merge</span> Merge
</a>

{% if object.history %}
<a class="dropdown-item" href="{{ object.get_create_version_url }}"><span class="material-symbols-outlined material-symbols-align">add</span>Add Versiontag</a>
{% endif %}
{% endif %}

</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@
{% if request.path == object.get_edit_url %}active{% endif %}
" href="{{ object.get_edit_url }}"><span class="material-symbols-outlined material-symbols-align">edit</span>Edit</a>
</li>

{% if object.history %}
<li class="nav-item">
<a class="nav-link
{% if request.path == object.get_history_url %}active{% endif %}
" style="color: #007bff" href="{{ object.get_history_url }}"><span class="material-symbols-outlined material-symbols-align">history</span>View History</a>
</li>
{% endif %}

</ul>
{% endif %}

Expand Down
8 changes: 8 additions & 0 deletions apis_core/apis_relations/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ def class_in(self, queryset, name, value):
name, _ = name.split("_")
return queryset.filter(Q(**{f"{name}__self_contenttype__in": value}))
return queryset


class HistoricalTripleFilterSet(TripleFilterSet):
pass


class HistoricalTempTripleFilterSet(TripleFilterSet):
pass
141 changes: 141 additions & 0 deletions apis_core/apis_relations/migrations/0006_versiontemptriple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
# Generated by Django 4.2.11 on 2024-03-27 14:17

import apis_core.apis_relations.models
import apis_core.generic.abc
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
import simple_history.models


class Migration(migrations.Migration):
dependencies = [
("apis_metainfo", "0011_alter_rootobject_deprecated_name"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
("apis_relations", "0005_alter_property_obj_class_alter_property_subj_class"),
]

operations = [
migrations.CreateModel(
name="VersionTempTriple",
fields=[
(
"triple_ptr",
models.ForeignKey(
auto_created=True,
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
parent_link=True,
related_name="+",
to="apis_relations.triple",
),
),
(
"id",
models.IntegerField(
auto_created=True, blank=True, db_index=True, verbose_name="ID"
),
),
(
"version_tag",
models.CharField(blank=True, max_length=255, null=True),
),
(
"review",
models.BooleanField(
default=False,
help_text="Should be set to True, if the data record holds up quality standards.",
),
),
("start_date", models.DateField(blank=True, null=True)),
("start_start_date", models.DateField(blank=True, null=True)),
("start_end_date", models.DateField(blank=True, null=True)),
("end_date", models.DateField(blank=True, null=True)),
("end_start_date", models.DateField(blank=True, null=True)),
("end_end_date", models.DateField(blank=True, null=True)),
(
"start_date_written",
models.CharField(
blank=True, max_length=255, null=True, verbose_name="Start"
),
),
(
"end_date_written",
models.CharField(
blank=True, max_length=255, null=True, verbose_name="End"
),
),
("status", models.CharField(max_length=100)),
("references", models.TextField(blank=True, null=True)),
("notes", models.TextField(blank=True, null=True)),
("history_id", models.AutoField(primary_key=True, serialize=False)),
("history_date", models.DateTimeField(db_index=True)),
("history_change_reason", models.CharField(max_length=100, null=True)),
(
"history_type",
models.CharField(
choices=[("+", "Created"), ("~", "Changed"), ("-", "Deleted")],
max_length=1,
),
),
(
"history_user",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to=settings.AUTH_USER_MODEL,
),
),
(
"obj",
apis_core.apis_relations.models.InheritanceForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="apis_metainfo.rootobject",
verbose_name="Object",
),
),
(
"prop",
models.ForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="apis_relations.property",
verbose_name="Property",
),
),
(
"subj",
apis_core.apis_relations.models.InheritanceForeignKey(
blank=True,
db_constraint=False,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="+",
to="apis_metainfo.rootobject",
verbose_name="Subject",
),
),
],
options={
"verbose_name": "Version",
"verbose_name_plural": "Versions",
"ordering": ("-history_date", "-history_id"),
"get_latest_by": ("history_date", "history_id"),
},
bases=(
simple_history.models.HistoricalChanges,
models.Model,
apis_core.generic.abc.GenericModel,
),
),
]
7 changes: 3 additions & 4 deletions apis_core/apis_relations/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import unicodedata
import copy

import reversion
from crum import get_current_request
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
Expand All @@ -15,6 +14,7 @@
from apis_core.apis_metainfo.models import RootObject
from apis_core.utils import DateParser
from apis_core.apis_metainfo import signals
from apis_core.history.models import VersionMixin


def find_if_user_accepted():
Expand Down Expand Up @@ -46,7 +46,6 @@ def filter_for_user(self):
return self.get_queryset()


@reversion.register(follow=["rootobject_ptr"])
class Property(RootObject):
class Meta:
verbose_name_plural = "Properties"
Expand Down Expand Up @@ -219,7 +218,7 @@ class InheritanceForeignKey(models.ForeignKey):
forward_related_accessor_class = InheritanceForwardManyToOneDescriptor


class Triple(GenericModel, models.Model):
class Triple(models.Model, GenericModel):
subj = InheritanceForeignKey(
RootObject,
blank=True,
Expand Down Expand Up @@ -322,7 +321,7 @@ def duplicate(self):
return duplicate


class TempTriple(Triple):
class TempTriple(Triple, VersionMixin):
review = models.BooleanField(
default=False,
help_text="Should be set to True, if the data record holds up quality standards.",
Expand Down
Empty file added apis_core/history/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions apis_core/history/api_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from apis_core.apis_relations.models import TempTriple
from apis_core.history.serializers import (
HistoryLogSerializer,
HistoryObjectSerializer,
)
from django.db.models import Q
from rest_framework.generics import RetrieveAPIView, ListAPIView


class EntityHistoryLogs(ListAPIView):
serializer_class = HistoryLogSerializer

def get_queryset(self):
return (
self.kwargs.get("contenttype")
.model_class()
.history.filter(id=self.kwargs.get("pk"))
)


class TempTripleHistoryLogs(ListAPIView):
serializer_class = HistoryLogSerializer

def get_queryset(self):
id = self.kwargs.get("pk")
return TempTriple.history.filter(Q(subj_id=id) | Q(obj_id=id))


class GenericHistoryLog(RetrieveAPIView):
serializer_class = HistoryObjectSerializer

def get_queryset(self):
return self.kwargs.get("contenttype").model_class().objects.all()
5 changes: 5 additions & 0 deletions apis_core/history/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class HistoryConfig(AppConfig):
name = "apis_core.history"
Loading

0 comments on commit 7a2e164

Please sign in to comment.