8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-25 00:13:45 +00:00

Fix comments for machine history view and model

This commit is contained in:
Jean-Romain Garnier 2020-04-22 23:23:12 +02:00 committed by Gabriel Detraz
parent 636b291d79
commit 6d8fa42487
2 changed files with 47 additions and 19 deletions

View file

@ -18,8 +18,8 @@
# You should have received a copy of the GNU General Public License along # You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc., # with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""machines.models """logs.models
The models definitions for the Machines app The models definitions for the logs app
""" """
from reversion.models import Version from reversion.models import Version
@ -30,7 +30,14 @@ from users.models import User
class HistoryEvent: class HistoryEvent:
def __init__(self, user: User, machine: Version, interface: Version, start=None, end=None): def __init__(self, user, machine, interface, start=None, end=None):
"""
:param user: User, The user owning the maching at the time of the event
:param machine: Version, the machine version related to the interface
:param interface: Version, the interface targeted by this event
:param start: datetime, the date at which this version was created
:param end: datetime, the date at which this version was replace by a new one
"""
self.user = user self.user = user
self.machine = machine self.machine = machine
self.interface = interface self.interface = interface
@ -43,6 +50,7 @@ class HistoryEvent:
def is_similar(self, elt2): def is_similar(self, elt2):
""" """
Checks whether two events are similar enough to be merged Checks whether two events are similar enough to be merged
:return: bool
""" """
return ( return (
elt2 is not None elt2 is not None
@ -69,6 +77,11 @@ class MachineHistory:
self.__last_evt = None self.__last_evt = None
def get(self, search, params): def get(self, search, params):
"""
:param search: ip or mac to lookup
:param params: dict built by the search view
:return: list or None, a list of HistoryEvent
"""
self.start = params.get("s", None) self.start = params.get("s", None)
self.end = params.get("e", None) self.end = params.get("e", None)
search_type = params.get("t", 0) search_type = params.get("t", 0)
@ -81,9 +94,12 @@ class MachineHistory:
return None return None
def __add_revision(self, user: User, machine: Version, interface: Version): def __add_revision(self, user, machine, interface):
""" """
Add a new revision to the chronological order Add a new revision to the chronological order
:param user: User, The user owning the maching at the time of the event
:param machine: Version, the machine version related to the interface
:param interface: Version, the interface targeted by this event
""" """
evt = HistoryEvent(user, machine, interface) evt = HistoryEvent(user, machine, interface)
evt.start_date = interface.revision.date_created evt.start_date = interface.revision.date_created
@ -109,9 +125,10 @@ class MachineHistory:
self.events.append(evt) self.events.append(evt)
self.__last_evt = evt self.__last_evt = evt
def __get_interfaces_for_ip(self, ip: str): def __get_interfaces_for_ip(self, ip):
""" """
Returns an iterable object with the Version objects :param ip: str
:return: An iterable object with the Version objects
of Interfaces with the given IP of Interfaces with the given IP
""" """
# TODO: What if ip list was deleted? # TODO: What if ip list was deleted?
@ -125,20 +142,22 @@ class MachineHistory:
Version.objects.get_for_model(Interface).order_by("revision__date_created") Version.objects.get_for_model(Interface).order_by("revision__date_created")
) )
def __get_interfaces_for_mac(self, mac: str): def __get_interfaces_for_mac(self, mac):
""" """
Returns an iterable object with the Version objects :param mac: str
of Interfaces with the given MAC :return: An iterable object with the Version objects
of Interfaces with the given MAC address
""" """
return filter( return filter(
lambda x: str(x.field_dict["mac_address"]) == mac, lambda x: str(x.field_dict["mac_address"]) == mac,
Version.objects.get_for_model(Interface).order_by("revision__date_created") Version.objects.get_for_model(Interface).order_by("revision__date_created")
) )
def __get_machines_for_interface(self, interface: Version): def __get_machines_for_interface(self, interface):
""" """
Returns an iterable object with the Verison objects :param interface: Version, the interface for which to find the machines
of Machines to which the given interface was attributed :return: An iterable object with the Version objects of Machine to
which the given interface was attributed
""" """
machine_id = interface.field_dict["machine_id"] machine_id = interface.field_dict["machine_id"]
return filter( return filter(
@ -146,15 +165,20 @@ class MachineHistory:
Version.objects.get_for_model(Machine).order_by("revision__date_created") Version.objects.get_for_model(Machine).order_by("revision__date_created")
) )
def __get_user_for_machine(self, machine: Version): def __get_user_for_machine(self, machine):
""" """
Returns the user to which the given machine belongs :param machine: Version, the machine of which the owner must be found
:return: The user to which the given machine belongs
""" """
# TODO: What if user was deleted? # TODO: What if user was deleted?
user_id = machine.field_dict["user_id"] user_id = machine.field_dict["user_id"]
return User.objects.get(id=user_id) return User.objects.get(id=user_id)
def __get_by_ip(self, ip: str): def __get_by_ip(self, ip):
"""
:param ip: str, The IP to lookup
:returns: list, a list of HistoryEvent
"""
interfaces = self.__get_interfaces_for_ip(ip) interfaces = self.__get_interfaces_for_ip(ip)
for interface in interfaces: for interface in interfaces:
@ -166,7 +190,11 @@ class MachineHistory:
return self.events return self.events
def __get_by_mac(self, mac: str): def __get_by_mac(self, mac):
"""
:param mac: str, The MAC address to lookup
:returns: list, a list of HistoryEvent
"""
interfaces = self.__get_interfaces_for_mac(mac) interfaces = self.__get_interfaces_for_mac(mac)
for interface in interfaces: for interface in interfaces:

View file

@ -484,8 +484,8 @@ def stats_actions(request):
@login_required @login_required
@can_view_app("users") @can_view_app("users")
def stats_search_machine_history(request): def stats_search_machine_history(request):
"""Vue qui permet de rechercher l'historique des machines ayant utilisé """View which displays the history of machines with the given
une IP ou une adresse MAC""" une IP or MAC adresse"""
history_form = MachineHistoryForm(request.GET or None) history_form = MachineHistoryForm(request.GET or None)
if history_form.is_valid(): if history_form.is_valid():
history = MachineHistory() history = MachineHistory()