def ip_taken(ip, records): """ Given an ip as an integer and a queryset find an object in the queryset with the same ip as the integer. This is inteded for ptrs and arecords and interfaces. """ ip_low, ip_high = one_to_two(ip) for record in records: if record.ip_lower is ip_low and record.ip_upper is ip_high: return record return None
def find_range(ip_str): Range = get_model('cyder', 'range') ip_upper, ip_lower = one_to_two(int(ipaddr.IPAddress(ip_str))) q_start = (Q(start_upper__lt=ip_upper) | Q(start_upper=ip_upper, start_lower__lte=ip_lower)) q_end = (Q(end_upper__gt=ip_upper) | Q(end_upper=ip_upper, end_lower__gte=ip_lower)) try: return Range.objects.filter(q_start, q_end)[0] except IndexError: return None