From d2f80bb39e17b9c1057506fc65005cc84225a9b7 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sat, 3 Nov 2018 19:24:46 +0100 Subject: [PATCH] Reparation ip search + formatage exact des macs --- machines/models.py | 5 ++--- search/views.py | 2 +- users/models.py | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/machines/models.py b/machines/models.py index 8db0c9d8..48e50644 100644 --- a/machines/models.py +++ b/machines/models.py @@ -43,7 +43,7 @@ from django.forms import ValidationError from django.utils import timezone from django.utils.functional import cached_property from django.utils.translation import ugettext_lazy as _ -from macaddress.fields import MACAddressField +from macaddress.fields import MACAddressField, default_dialect from netaddr import mac_bare, EUI, IPSet, IPRange, IPNetwork, IPAddress import preferences.models @@ -1099,7 +1099,7 @@ class Interface(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model): """ Tente un formatage mac_bare, si échoue, lève une erreur de validation""" try: - self.mac_address = str(EUI(self.mac_address)) + self.mac_address = str(EUI(self.mac_address, dialect=default_dialect())) except: raise ValidationError(_("The given MAC address is invalid.")) @@ -1116,7 +1116,6 @@ class Interface(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model): if not hasattr(self, 'type'): raise ValidationError(_("The selected IP type is invalid.")) self.filter_macaddress() - self.mac_address = str(EUI(self.mac_address)) or None if not self.ipv4 or self.type.ip_type != self.ipv4.ip_type: self.assign_ipv4() super(Interface, self).clean(*args, **kwargs) diff --git a/search/views.py b/search/views.py index 75bd67f0..a92b0105 100644 --- a/search/views.py +++ b/search/views.py @@ -174,7 +174,7 @@ def search_single_word(word, filters, user, interface__ipv4__ipv4__icontains=word ) try: - _mac_addr = EUI(word) + _mac_addr = EUI(word, 48) filter_machines |= Q(interface__mac_address=word) except AddrFormatError: pass diff --git a/users/models.py b/users/models.py index da0fb45a..63d0a875 100755 --- a/users/models.py +++ b/users/models.py @@ -616,9 +616,9 @@ class User(RevMixin, FieldPermissionModelMixin, AbstractBaseUser, if access_refresh: user_ldap.dialupAccess = str(self.has_access()) if mac_refresh: - user_ldap.macs = sorted([str(mac) for mac in Interface.objects.filter( + user_ldap.macs = [str(mac) for mac in Interface.objects.filter( machine__user=self - ).values_list('mac_address', flat=True).distinct()]) + ).values_list('mac_address', flat=True).distinct()] if group_refresh: # Need to refresh all groups because we don't know which groups # were updated during edition of groups and the user may no longer