8
0
Fork 0
mirror of https://gitlab.federez.net/re2o/re2o synced 2024-07-02 04:04:06 +00:00

Add view for InterfaceHistory

This commit is contained in:
Jean-Romain Garnier 2020-04-23 18:06:21 +02:00 committed by Jean-Romain Garnier
parent 9a88fe1d08
commit bb901a4c3f
3 changed files with 70 additions and 28 deletions

View file

@ -27,7 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% block title %}{% trans "History" %}{% endblock %}
{% block content %}
<h2>{% blocktrans %}History of {{ user }}{% endblocktrans %}</h2>
<h2>{% blocktrans %}History of {{ object }}{% endblocktrans %}</h2>
{% if events %}
<table class="table table-striped">

View file

@ -37,6 +37,11 @@ urlpatterns = [
views.revert_action,
name="revert-action",
),
url(
r"(?P<object_name>\w+)/(?P<object_id>[0-9]+)$",
views.detailed_history,
name="detailed-history",
),
url(r"^stats_general/$", views.stats_general, name="stats-general"),
url(r"^stats_models/$", views.stats_models, name="stats-models"),
url(r"^stats_users/$", views.stats_users, name="stats-users"),
@ -47,5 +52,4 @@ urlpatterns = [
name="history",
),
url(r"^stats_search_machine/$", views.stats_search_machine_history, name="stats-search-machine"),
url(r"^user/(?P<userid>[0-9]+)$", views.user_history, name="user-history"),
]

View file

@ -101,7 +101,12 @@ from re2o.utils import (
from re2o.base import re2o_paginator, SortTable
from re2o.acl import can_view_all, can_view_app, can_edit_history, can_view
from .models import MachineHistorySearch, UserHistory
from .models import (
MachineHistorySearch,
UserHistory,
InterfaceHistory
)
from .forms import MachineHistorySearchForm
@ -508,32 +513,77 @@ def stats_search_machine_history(request):
return render(request, "logs/search_machine_history.html", {"history_form": history_form})
@login_required
@can_view(User)
def user_history(request, users, **_kwargs):
history = UserHistory()
events = history.get(users)
def get_history_object(request, model, object_name, object_id):
"""Get the objet of type model with the given object_id
Handles permissions and DoesNotExist errors
"""
try:
object_name_id = object_name + "id"
kwargs = {object_name_id: object_id}
instance = model.get_instance(**kwargs)
except model.DoesNotExist:
messages.error(request, _("Nonexistent entry."))
return False, redirect(
reverse("users:profil", kwargs={"userid": str(request.user.id)})
)
can, msg, _permissions = instance.can_view(request.user)
if not can:
messages.error(
request, msg or _("You don't have the right to access this menu.")
)
return False, redirect(
reverse("users:profil", kwargs={"userid": str(request.user.id)})
)
return True, instance
@login_required
def detailed_history(request, object_name, object_id):
"""Render a detailed history for a model.
Permissions are handled by get_history_object.
"""
# Only handle objects for which a detailed view exists
if object_name == "user":
model = User
history = UserHistory()
elif object_name == "machine":
model = Machine
elif object_name == "interface":
model = Interface
history = InterfaceHistory()
else:
raise Http404(_("No model found."))
# Get instance and check permissions
can_view, instance = get_history_object(model, object_name, object_id)
if not can_view:
return instance
# Generate the pagination with the objects
max_result = GeneralOption.get_cached_value("pagination_number")
events = re2o_paginator(
request,
events,
history.get(instance),
max_result
)
return render(
request,
"logs/user_history.html",
{ "user": users, "events": events },
"logs/detailed_history.html",
{"object": instance, "events": events},
)
@login_required
def history(request, application, object_name, object_id):
"""Render history for a model.
The model is determined using the `HISTORY_BIND` dictionnary if none is
found, raises a Http404. The view checks if the user is allowed to see the
history using the `can_view` method of the model.
Permissions are handled by get_history_object.
Args:
request: The request sent by the user.
@ -552,23 +602,11 @@ def history(request, application, object_name, object_id):
model = apps.get_model(application, object_name)
except LookupError:
raise Http404(_("No model found."))
object_name_id = object_name + "id"
kwargs = {object_name_id: object_id}
try:
instance = model.get_instance(**kwargs)
except model.DoesNotExist:
messages.error(request, _("Nonexistent entry."))
return redirect(
reverse("users:profil", kwargs={"userid": str(request.user.id)})
)
can, msg, _permissions = instance.can_view(request.user)
if not can:
messages.error(
request, msg or _("You don't have the right to access this menu.")
)
return redirect(
reverse("users:profil", kwargs={"userid": str(request.user.id)})
)
can_view, instance = get_history_object(model, object_name, object_id)
if not can_view:
return instance
pagination_number = GeneralOption.get_cached_value("pagination_number")
reversions = Version.objects.get_for_object(instance)
if hasattr(instance, "linked_objects"):