mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-23 20:03:11 +00:00
Better handle deleted objects in detailed history view
This commit is contained in:
parent
81693808d5
commit
434ab15543
2 changed files with 31 additions and 19 deletions
|
@ -308,6 +308,10 @@ class History:
|
||||||
for version in interface_versions:
|
for version in interface_versions:
|
||||||
self._add_revision(version)
|
self._add_revision(version)
|
||||||
|
|
||||||
|
# Return None if interface_versions was empty
|
||||||
|
if self._last_version is None:
|
||||||
|
return None
|
||||||
|
|
||||||
return self.events[::-1]
|
return self.events[::-1]
|
||||||
|
|
||||||
def _compute_diff(self, v1, v2, ignoring=[]):
|
def _compute_diff(self, v1, v2, ignoring=[]):
|
||||||
|
@ -444,13 +448,26 @@ class UserHistory(History):
|
||||||
"""
|
"""
|
||||||
self.events = []
|
self.events = []
|
||||||
|
|
||||||
# Find whether this is a Club or an Adherent
|
# Try to find an Adherent object
|
||||||
try:
|
adherents = filter(
|
||||||
obj = Adherent.objects.get(user_ptr_id=user_id)
|
lambda x: x.field_dict["user_ptr_id"] == user_id,
|
||||||
except Adherent.DoesNotExist:
|
Version.objects.get_for_model(Adherent)
|
||||||
obj = Club.objects.get(user_ptr_id=user_id)
|
)
|
||||||
|
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
|
# that were once owned by this user
|
||||||
self.related = filter(
|
self.related = filter(
|
||||||
lambda x: x.field_dict["user_id"] == user_id,
|
lambda x: x.field_dict["user_id"] == user_id,
|
||||||
|
|
|
@ -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
|
"""Get the objet of type model with the given object_id
|
||||||
Handles permissions and DoesNotExist errors
|
Handles permissions and DoesNotExist errors
|
||||||
"""
|
"""
|
||||||
instance = None
|
|
||||||
is_deleted = False
|
is_deleted = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -526,20 +525,9 @@ def get_history_object(request, model, object_name, object_id, allow_deleted=Fal
|
||||||
kwargs = {object_name_id: object_id}
|
kwargs = {object_name_id: object_id}
|
||||||
instance = model.get_instance(**kwargs)
|
instance = model.get_instance(**kwargs)
|
||||||
except model.DoesNotExist:
|
except model.DoesNotExist:
|
||||||
pass
|
|
||||||
|
|
||||||
if instance is None and allow_deleted:
|
|
||||||
# Try to find an instance among the Version objects
|
|
||||||
is_deleted = True
|
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."))
|
messages.error(request, _("Nonexistent entry."))
|
||||||
return False, redirect(
|
return False, redirect(
|
||||||
reverse("users:profil", kwargs={"userid": str(request.user.id)})
|
reverse("users:profil", kwargs={"userid": str(request.user.id)})
|
||||||
|
@ -593,6 +581,13 @@ def detailed_history(request, object_name, object_id):
|
||||||
max_result
|
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(
|
return render(
|
||||||
request,
|
request,
|
||||||
"logs/detailed_history.html",
|
"logs/detailed_history.html",
|
||||||
|
|
Loading…
Reference in a new issue