From 4b96dc0a214e94be178d4d4a96b3ef5dc9ae0012 Mon Sep 17 00:00:00 2001 From: Jean-Romain Garnier Date: Thu, 23 Apr 2020 19:20:55 +0000 Subject: [PATCH] Handle history for non-existant objects --- logs/models.py | 6 ++++-- logs/views.py | 10 +++++----- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/logs/models.py b/logs/models.py index 144bd716..57597c5b 100644 --- a/logs/models.py +++ b/logs/models.py @@ -454,6 +454,7 @@ class UserHistory(History): Version.objects.get_for_model(Adherent) ) obj = next(adherents, None) + model = Adherent # Fallback on a Club if obj is None: @@ -462,6 +463,7 @@ class UserHistory(History): Version.objects.get_for_model(Club) ) obj = next(clubs, None) + model = Club # If nothing was found, abort if obj is None: @@ -492,8 +494,8 @@ class UserHistory(History): # Do the same thing for the Adherent of Club self._last_version = None obj_versions = filter( - lambda x: x.field_dict["id"] == obj.id, - Version.objects.get_for_model(type(obj)).order_by("revision__date_created") + lambda x: x.field_dict["id"] == user_id, + Version.objects.get_for_model(model).order_by("revision__date_created") ) for version in obj_versions: diff --git a/logs/views.py b/logs/views.py index 981d3d00..007da9f7 100644 --- a/logs/views.py +++ b/logs/views.py @@ -526,6 +526,7 @@ def get_history_object(request, model, object_name, object_id, allow_deleted=Fal instance = model.get_instance(**kwargs) except model.DoesNotExist: is_deleted = True + instance = None if is_deleted and not allow_deleted: messages.error(request, _("Nonexistent entry.")) @@ -575,11 +576,7 @@ def detailed_history(request, object_name, object_id): # Generate the pagination with the objects max_result = GeneralOption.get_cached_value("pagination_number") - events = re2o_paginator( - request, - history.get(object_id), - max_result - ) + events = history.get(int(object_id)) # Events is None if object wasn't found if events is None: @@ -588,6 +585,9 @@ def detailed_history(request, object_name, object_id): reverse("users:profil", kwargs={"userid": str(request.user.id)}) ) + # Add the paginator in case there are many results + events = re2o_paginator(request, events, max_result) + return render( request, "logs/detailed_history.html",