diff --git a/logs/forms.py b/logs/forms.py index 9e2b98a6..f51dbef7 100644 --- a/logs/forms.py +++ b/logs/forms.py @@ -40,9 +40,7 @@ class MachineHistoryForm(Form): ) t = forms.CharField( label=_("Search type"), - widget=forms.Select, - choices=CHOICES_TYPE, - initial=0, + widget=forms.Select(choices=CHOICES_TYPE) ) s = forms.DateField(required=False, label=_("Start date")) e = forms.DateField(required=False, label=_("End date")) diff --git a/logs/templates/logs/machine_history.html b/logs/templates/logs/machine_history.html index 01d6c7e7..50efbcf5 100644 --- a/logs/templates/logs/machine_history.html +++ b/logs/templates/logs/machine_history.html @@ -1,4 +1,4 @@ -{% extends 'log/sidebar.html' %} +{% extends 'logs/sidebar.html' %} {% comment %} 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 diff --git a/logs/templates/logs/search_machine_history.html b/logs/templates/logs/search_machine_history.html index 3baf563d..07c9f68c 100644 --- a/logs/templates/logs/search_machine_history.html +++ b/logs/templates/logs/search_machine_history.html @@ -24,19 +24,17 @@ with this program; if not, write to the Free Software Foundation, Inc., {% load bootstrap3 %} {% load i18n %} -{% block title %}{% trans "Search" %}{% endblock %} +{% block title %}{% trans "Search machine history" %}{% endblock %} {% block content %}
+

{% trans "Search machine history" %}

+ {% bootstrap_field history_form.q %} - {% bootstrap_form_errors history_form.t %} - {% if history_form.s %} + {% bootstrap_field history_form.t %} {% bootstrap_field history_form.s %} - {% endif %} - {% if history_form.e %} {% bootstrap_field history_form.e %} - {% endif %} {% trans "Search" as tr_search %} {% bootstrap_button tr_search button_type="submit" icon="search" %}
diff --git a/logs/urls.py b/logs/urls.py index 8fa0f469..eced2a83 100644 --- a/logs/urls.py +++ b/logs/urls.py @@ -46,4 +46,5 @@ urlpatterns = [ views.history, name="history", ), + url(r"^stats_search_machine/$", views.stats_search_machine_history, name="stats-search-machine"), ] diff --git a/logs/views.py b/logs/views.py index 08ac0c9f..ca75e48f 100644 --- a/logs/views.py +++ b/logs/views.py @@ -483,7 +483,7 @@ def stats_actions(request): @login_required @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é une IP ou une adresse MAC""" history_form = MachineHistoryForm(request.GET or None) diff --git a/test.py b/test.py new file mode 100644 index 00000000..7b777609 --- /dev/null +++ b/test.py @@ -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) +