def test_mac_canonicalizer(self): self.assertEqual(canonicalize_mac('1111.2222.3333'), '11:11:22:22:33:33') self.assertEqual(canonicalize_mac('11-11-22-22-33-33'), '11:11:22:22:33:33') self.assertEqual(canonicalize_mac('11:11:22:22:33:33'), '11:11:22:22:33:33')
def search(q): ''' Searches in Equipment model for racks, units, IP address, MACs, serial, hostnames etc @type q: string or iterable @param q: a string or an iterable of strings to search for. @rtype: QuerySet @return: A QuerySet with results matching all items of q ''' if q is None or len(q) == 0 or 'hwdoc' not in settings.INSTALLED_APPS: return Equipment.objects.none() # A way to get all if q == "ALL_EQS": return Equipment.objects.all() # Working on iterables. However in case we are not given one it is cheaper # to create one than fail try: q.__iter__() except AttributeError: q = (q,) ids = [] try: for key in q: try: dns = gethostbyaddr(key)[0] except (herror, gaierror, IndexError, error, UnicodeEncodeError): dns = '' mac = canonicalize_mac(key) # A heuristic to allow user to filter queries down to the unit level # using a simple syntax m = re.search('^(\w{1,3}\d\d)[Uu](\d\d)$', key) if m: rack = m.group(1) unit = m.group(2) else: rack = '' unit = None result = Equipment.objects.filter( Q(serial=key)| Q(rack__name__contains=key)| Q(rack__name__iexact=rack)| Q(model__name__icontains=key)| Q(allocation__name__icontains=key)| Q(allocation__contacts__name__icontains=key)| Q(allocation__contacts__surname__icontains=key)| Q(servermanagement__mac__icontains=mac)| Q(servermanagement__hostname__icontains=key)| Q(servermanagement__hostname=dns)| Q(attrs__value__icontains=key) ) if unit: result = result.filter(unit=unit) ids.extend(result.distinct().values_list('id', flat=True)) ids = list(set(ids)) return Equipment.objects.filter(pk__in=ids).distinct() except DatabaseError as e: return Equipment.objects.none() # TODO: Log this raise RuntimeError(_('An error occured while querying db: %(databaseerror)s') % {'databaseerror': e})
def search(q): ''' Searches in Equipment model for racks, units, IP address, MACs, serial, hostnames etc @type q: string or iterable @param q: a string or an iterable of strings to search for. @rtype: QuerySet @return: A QuerySet with results matching all items of q ''' if q is None or len(q) == 0 or 'hwdoc' not in settings.INSTALLED_APPS: return Equipment.objects.none() # A way to get all if q == "ALL_EQS": return Equipment.objects.all() # Working on iterables. However in case we are not given one it is cheaper # to create one than fail try: q.__iter__() except AttributeError: q = (q, ) ids = [] try: for key in q: try: dns = gethostbyaddr(key)[0] except (herror, gaierror, IndexError, error, UnicodeEncodeError): dns = '' mac = canonicalize_mac(key) # A heuristic to allow user to filter queries down to the unit level # using a simple syntax m = re.search('^(\w{1,3}\d\d)[Uu](\d\d)$', key) if m: rack = m.group(1) unit = m.group(2) else: rack = '' unit = None result = Equipment.objects.filter( Q(serial=key) | Q(rack__name__contains=key) | Q(rack__name__iexact=rack) | Q(model__name__icontains=key) | Q(allocation__name__icontains=key) | Q(allocation__contacts__name__icontains=key) | Q(allocation__contacts__surname__icontains=key) | Q(servermanagement__mac__icontains=mac) | Q(servermanagement__hostname__icontains=key) | Q(servermanagement__hostname=dns) | Q(attrs__value__icontains=key)) if unit: result = result.filter(unit=unit) ids.extend(result.distinct().values_list('id', flat=True)) ids = list(set(ids)) return Equipment.objects.filter(pk__in=ids).distinct() except DatabaseError as e: return Equipment.objects.none() # TODO: Log this raise RuntimeError( _('An error occured while querying db: %(databaseerror)s') % {'databaseerror': e})