Skip to content

Commit

Permalink
feat: add comments to revisions
Browse files Browse the repository at this point in the history
This commit makes use of the reversion `set_comment` functionality.
Using comments, we can store additional information in the revisions.
That way we store the information if an object got added, if it was
updated and which fields. If another object was merged into it, we store
the original name of the other object. If an entity was created by
duplicating it, we store from which object it was duplicated.
  • Loading branch information
b1rger committed Sep 21, 2023
1 parent 6e4df12 commit ce9316f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
15 changes: 13 additions & 2 deletions apis_core/apis_entities/edit_generic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import importlib
import json

from django.conf import settings
from django.contrib.auth.decorators import login_required
Expand All @@ -14,6 +15,7 @@
from django.views.generic import DeleteView
from django_tables2 import RequestConfig
from reversion.models import Version
from reversion import set_comment as reversion_set_comment, create_revision

from apis_core.apis_labels.models import Label
from apis_core.apis_metainfo.models import Uri
Expand Down Expand Up @@ -139,6 +141,8 @@ def post(self, request, *args, **kwargs):
form = form(request.POST, instance=self.instance)
if form.is_valid():
entity_2 = form.save()
comment = [{"changed": {"name": str(entity_2._meta.verbose_name), "object": str(entity_2), "fields": form.changed_data }}]
reversion_set_comment(json.dumps(comment))
return redirect(
reverse(
"apis:apis_entities:generic_entities_edit_view",
Expand Down Expand Up @@ -186,6 +190,8 @@ def post(self, request, *args, **kwargs):
form = form(request.POST)
if form.is_valid():
entity_2 = form.save()
comment = [{"added": {"name": str(entity_2._meta.verbose_name), "object": str(entity_2)}}]
reversion_set_comment(json.dumps(comment))
return redirect(
reverse(
"apis:apis_entities:generic_entities_detail_view",
Expand Down Expand Up @@ -255,6 +261,7 @@ class GenericEntitiesDeleteView(EntityInstanceMixin, DeleteView):
)

def dispatch(self, request, *args, **kwargs):

self.success_url = reverse(
"apis_core:apis_entities:generic_entities_list",
kwargs={"entity": self.entity},
Expand All @@ -265,9 +272,13 @@ def dispatch(self, request, *args, **kwargs):
@method_decorator(login_required, name="dispatch")
class GenericEntitiesDuplicateView(EntityInstanceMixin, View):
def get(self, request, *args, **kwargs):
source_obj = get_object_or_404(self.entity_model, pk=self.pk)
with create_revision():
source_obj = get_object_or_404(self.entity_model, pk=self.pk)

newobj = source_obj.duplicate()

newobj = source_obj.duplicate()
comment = [{"added": {"name": str(newobj._meta.verbose_name), "object": str(newobj), "duplicate_from": str(source_obj)}}]
reversion_set_comment(json.dumps(comment))

return redirect(
reverse(
Expand Down
8 changes: 8 additions & 0 deletions apis_core/apis_entities/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import re
import unicodedata
import json

from django.contrib.contenttypes.models import ContentType
from django.apps import apps
Expand All @@ -13,6 +14,7 @@
from django.urls import reverse
from model_utils.managers import InheritanceManager
from django.db.models.query import QuerySet
from reversion import set_comment as reversion_set_comment

from apis_core.utils import caching
from apis_core.utils import DateParser
Expand Down Expand Up @@ -322,6 +324,8 @@ def merge_with(self, entities):
origin = self.__class__
signals.pre_merge_with.send(sender=origin, instance=self, entities=entities)

reversion_comments = []

# TODO: check if these imports can be put to top of module without
# causing circular import issues.
from apis_core.apis_labels.models import Label
Expand Down Expand Up @@ -361,6 +365,10 @@ def merge_with(self, entities):
TempTriple.objects.filter(obj__id=ent.id).update(obj=self)
TempTriple.objects.filter(subj__id=ent.id).update(subj=self)

reversion_comments.append({"changed": {"name": str(self._meta.verbose_name), "object": str(self), "merged_from": str(ent) }})

reversion_set_comment(json.dumps(reversion_comments))

for ent in entities:
self.merge_fields(ent)

Expand Down

0 comments on commit ce9316f

Please sign in to comment.