mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-12-23 15:33:45 +00:00
Add view for InterfaceHistory
This commit is contained in:
parent
9a88fe1d08
commit
bb901a4c3f
3 changed files with 70 additions and 28 deletions
|
@ -27,7 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
||||||
{% block title %}{% trans "History" %}{% endblock %}
|
{% block title %}{% trans "History" %}{% endblock %}
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h2>{% blocktrans %}History of {{ user }}{% endblocktrans %}</h2>
|
<h2>{% blocktrans %}History of {{ object }}{% endblocktrans %}</h2>
|
||||||
|
|
||||||
{% if events %}
|
{% if events %}
|
||||||
<table class="table table-striped">
|
<table class="table table-striped">
|
|
@ -37,6 +37,11 @@ urlpatterns = [
|
||||||
views.revert_action,
|
views.revert_action,
|
||||||
name="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_general/$", views.stats_general, name="stats-general"),
|
||||||
url(r"^stats_models/$", views.stats_models, name="stats-models"),
|
url(r"^stats_models/$", views.stats_models, name="stats-models"),
|
||||||
url(r"^stats_users/$", views.stats_users, name="stats-users"),
|
url(r"^stats_users/$", views.stats_users, name="stats-users"),
|
||||||
|
@ -47,5 +52,4 @@ urlpatterns = [
|
||||||
name="history",
|
name="history",
|
||||||
),
|
),
|
||||||
url(r"^stats_search_machine/$", views.stats_search_machine_history, name="stats-search-machine"),
|
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"),
|
|
||||||
]
|
]
|
||||||
|
|
|
@ -101,7 +101,12 @@ from re2o.utils import (
|
||||||
from re2o.base import re2o_paginator, SortTable
|
from re2o.base import re2o_paginator, SortTable
|
||||||
from re2o.acl import can_view_all, can_view_app, can_edit_history, can_view
|
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
|
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})
|
return render(request, "logs/search_machine_history.html", {"history_form": history_form})
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
def get_history_object(request, model, object_name, object_id):
|
||||||
@can_view(User)
|
"""Get the objet of type model with the given object_id
|
||||||
def user_history(request, users, **_kwargs):
|
Handles permissions and DoesNotExist errors
|
||||||
history = UserHistory()
|
"""
|
||||||
events = history.get(users)
|
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")
|
max_result = GeneralOption.get_cached_value("pagination_number")
|
||||||
events = re2o_paginator(
|
events = re2o_paginator(
|
||||||
request,
|
request,
|
||||||
events,
|
history.get(instance),
|
||||||
max_result
|
max_result
|
||||||
)
|
)
|
||||||
|
|
||||||
return render(
|
return render(
|
||||||
request,
|
request,
|
||||||
"logs/user_history.html",
|
"logs/detailed_history.html",
|
||||||
{ "user": users, "events": events },
|
{"object": instance, "events": events},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@login_required
|
||||||
def history(request, application, object_name, object_id):
|
def history(request, application, object_name, object_id):
|
||||||
"""Render history for a model.
|
"""Render history for a model.
|
||||||
|
|
||||||
The model is determined using the `HISTORY_BIND` dictionnary if none is
|
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
|
found, raises a Http404. The view checks if the user is allowed to see the
|
||||||
history using the `can_view` method of the model.
|
history using the `can_view` method of the model.
|
||||||
|
Permissions are handled by get_history_object.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
request: The request sent by the user.
|
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)
|
model = apps.get_model(application, object_name)
|
||||||
except LookupError:
|
except LookupError:
|
||||||
raise Http404(_("No model found."))
|
raise Http404(_("No model found."))
|
||||||
object_name_id = object_name + "id"
|
|
||||||
kwargs = {object_name_id: object_id}
|
can_view, instance = get_history_object(model, object_name, object_id)
|
||||||
try:
|
if not can_view:
|
||||||
instance = model.get_instance(**kwargs)
|
return instance
|
||||||
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)})
|
|
||||||
)
|
|
||||||
pagination_number = GeneralOption.get_cached_value("pagination_number")
|
pagination_number = GeneralOption.get_cached_value("pagination_number")
|
||||||
reversions = Version.objects.get_for_object(instance)
|
reversions = Version.objects.get_for_object(instance)
|
||||||
if hasattr(instance, "linked_objects"):
|
if hasattr(instance, "linked_objects"):
|
||||||
|
|
Loading…
Reference in a new issue