mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-12-23 15:33:45 +00:00
Fix machine history views
This commit is contained in:
parent
4b7393b5e4
commit
c4634191f5
6 changed files with 58 additions and 11 deletions
|
@ -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"))
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 %}
|
||||
|
||||
<form class="form">
|
||||
<h3>{% trans "Search machine history" %}</h3>
|
||||
|
||||
{% 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" %}
|
||||
</form>
|
||||
|
|
|
@ -46,4 +46,5 @@ urlpatterns = [
|
|||
views.history,
|
||||
name="history",
|
||||
),
|
||||
url(r"^stats_search_machine/$", views.stats_search_machine_history, name="stats-search-machine"),
|
||||
]
|
||||
|
|
|
@ -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)
|
||||
|
|
50
test.py
Normal file
50
test.py
Normal 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)
|
||||
|
Loading…
Reference in a new issue