Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ms/340 add simple history to apis #698

Merged
merged 3 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions apis_core/apis_entities/serializers_generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from django.db.models.query import QuerySet
from django.urls import reverse
from rest_framework import serializers
from reversion.models import Version


base_uri = getattr(settings, "APIS_BASE_URI", "http://apis.info")
Expand Down Expand Up @@ -39,25 +38,6 @@ class EntitySerializer(serializers.Serializer):
id = serializers.IntegerField()
url = serializers.SerializerMethodField(method_name="add_url")
uris = EntityUriSerializer(source="uri_set", many=True)
revisions = serializers.SerializerMethodField(method_name="add_revisions")

def add_revisions(self, obj):
ver = Version.objects.get_for_object(obj)
res = []
for v in ver:
usr_1 = getattr(v.revision, "user", None)
if usr_1 is not None:
usr_1 = usr_1.username
else:
usr_1 = "Not specified"
res.append(
{
"id": v.id,
"date_created": v.revision.date_created,
"user_created": usr_1,
}
)
return res

def add_relations(self, obj):
res = {}
Expand Down Expand Up @@ -163,25 +143,6 @@ class RelationEntitySerializer(serializers.Serializer):
start_date_written = serializers.DateField()
end_date_written = serializers.DateField()
relation_type = serializers.SerializerMethodField(method_name="add_relation_label")
revisions = serializers.SerializerMethodField(method_name="add_revisions")

def add_revisions(self, obj):
ver = Version.objects.get_for_object(obj)
res = []
for v in ver:
usr_1 = getattr(v.revision, "user", None)
if usr_1 is not None:
usr_1 = usr_1.username
else:
usr_1 = "Not specified"
res.append(
{
"id": v.id,
"date_created": v.revision.date_created,
"user_created": usr_1,
}
)
return res

def add_entity(self, obj):
return EntitySerializer(
Expand Down
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
4 changes: 0 additions & 4 deletions apis_core/apis_metainfo/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import reversion
from django.conf import settings
from django.contrib.auth.models import Group
from django.contrib.contenttypes.models import ContentType
Expand All @@ -19,7 +18,6 @@
NEXT_PREV = getattr(settings, "APIS_NEXT_PREV", True)


@reversion.register()
class RootObject(GenericModel, models.Model):
"""
The very root thing that can exist in a given ontology. Several classes inherit from it.
Expand Down Expand Up @@ -80,7 +78,6 @@ def duplicate(self):
duplicate.alters_data = True


@reversion.register()
class Collection(GenericModel, models.Model):
"""Allows to group entities and relation."""

Expand Down Expand Up @@ -138,7 +135,6 @@ def get_queryset(self):
return UriQuerySet(self.model)


@reversion.register()
class Uri(GenericModel, models.Model):
uri = models.URLField(blank=True, null=True, unique=True, max_length=255)
domain = models.CharField(max_length=255, blank=True)
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
Loading