From 434ab155431be848e94c571655a2a4f026d721d0 Mon Sep 17 00:00:00 2001 From: Jean-Romain Garnier Date: Thu, 23 Apr 2020 20:50:31 +0200 Subject: [PATCH] Better handle deleted objects in detailed history view --- logs/models.py | 29 +++++++++++++++++++++++------ logs/views.py | 21 ++++++++------------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/logs/models.py b/logs/models.py index c52762a2..144bd716 100644 --- a/logs/models.py +++ b/logs/models.py @@ -308,6 +308,10 @@ class History: for version in interface_versions: self._add_revision(version) + # Return None if interface_versions was empty + if self._last_version is None: + return None + return self.events[::-1] def _compute_diff(self, v1, v2, ignoring=[]): @@ -444,13 +448,26 @@ class UserHistory(History): """ self.events = [] - # Find whether this is a Club or an Adherent - try: - obj = Adherent.objects.get(user_ptr_id=user_id) - except Adherent.DoesNotExist: - obj = Club.objects.get(user_ptr_id=user_id) + # Try to find an Adherent object + adherents = filter( + lambda x: x.field_dict["user_ptr_id"] == user_id, + Version.objects.get_for_model(Adherent) + ) + obj = next(adherents, None) - # Add as "related" histories the list of Machine objects + # Fallback on a Club + if obj is None: + clubs = filter( + lambda x: x.field_dict["user_ptr_id"] == user_id, + Version.objects.get_for_model(Club) + ) + obj = next(clubs, None) + + # If nothing was found, abort + if obj is None: + return None + + # Add in "related" elements the list of Machine objects # that were once owned by this user self.related = filter( lambda x: x.field_dict["user_id"] == user_id, diff --git a/logs/views.py b/logs/views.py index c0f3d6e9..981d3d00 100644 --- a/logs/views.py +++ b/logs/views.py @@ -518,7 +518,6 @@ def get_history_object(request, model, object_name, object_id, allow_deleted=Fal """Get the objet of type model with the given object_id Handles permissions and DoesNotExist errors """ - instance = None is_deleted = False try: @@ -526,20 +525,9 @@ def get_history_object(request, model, object_name, object_id, allow_deleted=Fal kwargs = {object_name_id: object_id} instance = model.get_instance(**kwargs) except model.DoesNotExist: - pass - - if instance is None and allow_deleted: - # Try to find an instance among the Version objects is_deleted = True - versions = filter( - lambda x: x.field_dict["id"] == object_id, - Version.objects.get_for_model(model) - ) - versions = list(versions) - if len(versions): - instance = versions[0] - if instance is None: + if is_deleted and not allow_deleted: messages.error(request, _("Nonexistent entry.")) return False, redirect( reverse("users:profil", kwargs={"userid": str(request.user.id)}) @@ -593,6 +581,13 @@ def detailed_history(request, object_name, object_id): max_result ) + # Events is None if object wasn't found + if events is None: + messages.error(request, _("Nonexistent entry.")) + return redirect( + reverse("users:profil", kwargs={"userid": str(request.user.id)}) + ) + return render( request, "logs/detailed_history.html",