8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-23 23:43:47 +00:00

Better handle deleted objects in detailed history view

This commit is contained in:
Jean-Romain Garnier 2020-04-23 20:50:31 +02:00 committed by Gabriel Detraz
parent 81693808d5
commit 434ab15543
2 changed files with 31 additions and 19 deletions

View file

@ -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,

View file

@ -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",