8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-25 22:22:26 +00:00

Fix machine history views

This commit is contained in:
Jean-Romain Garnier 2020-04-22 16:32:56 +00:00 committed by Gabriel Detraz
parent ec22065a34
commit 7bce33a84f
6 changed files with 58 additions and 11 deletions

View file

@ -40,9 +40,7 @@ class MachineHistoryForm(Form):
) )
t = forms.CharField( t = forms.CharField(
label=_("Search type"), label=_("Search type"),
widget=forms.Select, widget=forms.Select(choices=CHOICES_TYPE)
choices=CHOICES_TYPE,
initial=0,
) )
s = forms.DateField(required=False, label=_("Start date")) s = forms.DateField(required=False, label=_("Start date"))
e = forms.DateField(required=False, label=_("End date")) e = forms.DateField(required=False, label=_("End date"))

View file

@ -1,4 +1,4 @@
{% extends 'log/sidebar.html' %} {% extends 'logs/sidebar.html' %}
{% comment %} {% comment %}
Re2o est un logiciel d'administration développé initiallement au rezometz. Il Re2o est un logiciel d'administration développé initiallement au rezometz. Il
se veut agnostique au réseau considéré, de manière à être installable en se veut agnostique au réseau considéré, de manière à être installable en

View file

@ -24,19 +24,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
{% load bootstrap3 %} {% load bootstrap3 %}
{% load i18n %} {% load i18n %}
{% block title %}{% trans "Search" %}{% endblock %} {% block title %}{% trans "Search machine history" %}{% endblock %}
{% block content %} {% block content %}
<form class="form"> <form class="form">
<h3>{% trans "Search machine history" %}</h3>
{% bootstrap_field history_form.q %} {% bootstrap_field history_form.q %}
{% bootstrap_form_errors history_form.t %} {% bootstrap_field history_form.t %}
{% if history_form.s %}
{% bootstrap_field history_form.s %} {% bootstrap_field history_form.s %}
{% endif %}
{% if history_form.e %}
{% bootstrap_field history_form.e %} {% bootstrap_field history_form.e %}
{% endif %}
{% trans "Search" as tr_search %} {% trans "Search" as tr_search %}
{% bootstrap_button tr_search button_type="submit" icon="search" %} {% bootstrap_button tr_search button_type="submit" icon="search" %}
</form> </form>

View file

@ -46,4 +46,5 @@ urlpatterns = [
views.history, views.history,
name="history", name="history",
), ),
url(r"^stats_search_machine/$", views.stats_search_machine_history, name="stats-search-machine"),
] ]

View file

@ -483,7 +483,7 @@ def stats_actions(request):
@login_required @login_required
@can_view_app("users") @can_view_app("users")
def search_machine_history(request): def stats_search_machine_history(request):
"""Vue qui permet de rechercher l'historique des machines ayant utilisé """Vue qui permet de rechercher l'historique des machines ayant utilisé
une IP ou une adresse MAC""" une IP ou une adresse MAC"""
history_form = MachineHistoryForm(request.GET or None) history_form = MachineHistoryForm(request.GET or None)

50
test.py Normal file
View file

@ -0,0 +1,50 @@
from reversion.models import Version
from machines.models import IpList
from machines.models import Interface
from machines.models import Machine
from users.models import User
def get_interfaces_with_ip(ip):
"""
Get all the interfaces that, at some point, had the given ip
"""
# TODO: What if IpList was deleted?
ip_id = IpList.objects.get(ipv4=ip).id
interfaces = Version.objects.get_for_model(Interface)
interfaces = filter(lambda x: x.field_dict["ipv4_id"] == ip_id, interfaces)
return interfaces
def get_machine_with_interface(interface):
"""
Get the machine which contained the given interface, even if deleted
"""
machine_id = interface.field_dict["machine_id"]
machines = Version.objects.get_for_model(Machine)
machines = filter(lambda x: x.field_dict["id"] == machine_id, machines)
return machines[0]
def get_user_with_machine(machine):
"""
"""
user_id = machine.field_dict["user_id"]
user = User.objects.filter(id=user_id)
return user[0]
interfaces = get_interfaces_with_ip("10.0.0.0")
output_dict = {}
for interface in interfaces:
mac = interface.field_dict["mac_address"]
machine = get_machine_with_interface(interface)
user = get_user_with_machine(machine)
output_dict[mac] = {
"machine": machine.field_dict["name"],
"user": user
}
print(output_dict)