def get_context_data(self, **kwargs): router = kwargs.pop('object') gwports = router.get_gwports().select_related( 'to_netbox', 'to_interface') sorted_ports = sorted( gwports, key=lambda p: natsort.split(p.ifname) ) context = [] for gwport in sorted_ports: c = { 'pk': gwport.pk, 'sysname': router.sysname, 'type': 'gwport', 'fields': model_to_dict(gwport), } if gwport.to_netbox: c.update({ 'netbox_sysname': gwport.to_netbox.sysname, 'trunk_port': (gwport.to_interface.trunk if gwport.to_interface else None) }) c.update(self._get_expandable(gwport)) context.append(c) return context
def get_context_data(self, **kwargs): router = kwargs.pop('object') gwports = router.get_gwports().select_related( 'to_netbox', 'to_interface') sorted_ports = sorted( gwports, key=lambda p: natsort.split(p.ifname) ) context = [] for gwport in sorted_ports: c = { 'pk': gwport.pk, 'sysname': router.sysname, 'type': 'gwport', 'fields': model_to_dict(gwport), } if gwport.to_netbox: c.update({ 'netbox_sysname': gwport.to_netbox.sysname, 'trunk_port': gwport.to_interface.trunk, }) c.update(self._get_expandable(gwport)) context.append(c) return context
def get_context_data(self, **kwargs): switch = kwargs.pop('object') vlan_id = self.kwargs.pop('vlan_id', None) swports = switch.get_swports() swport_vlans = SwPortVlan.objects.filter(interface__in=swports, vlan__id=vlan_id) switch_has_services = switch.service_set.all().count() context = [] for vlan in swport_vlans: interface = vlan.interface c = { 'pk': vlan.interface.pk, 'type': 'switch', 'expandable': False, 'interface': model_to_dict(interface), 'interface_netbox_sysname': interface.netbox.sysname, 'interface_netbox_sysname_short': str(interface.netbox.sysname), } c.update(_get_connected_sysname(interface)) if interface.to_interface and switch_has_services: c.update({'expandable': True}) elif Cam.objects.filter(netbox=interface.netbox, ifindex=interface.ifindex, end_time__gt=dt.max).count(): c.update({'expandable': True}) context.append(c) return sorted( context, key=lambda vlan: natsort.split(vlan['interface']['ifname']))
def filter_queryset(self, request, queryset, view): """Filter on interface__ifname if it exists in GET-request""" to_match = ['interface__ifname', '-interface__ifname'] intersection = (set(request.GET.get('ordering', '').split(',')) & set(to_match)) if intersection: match_field = intersection.pop() return sorted(queryset, key=lambda x: natsort.split(x.interface.ifname), reverse=match_field.startswith('-')) return queryset
def filter_queryset(self, request, queryset, view): """Filter on ifname if it exists as an ordering parameter""" interface_ifnames = ['interface__ifname', '-interface__ifname'] ifnames = ['ifname', '-ifname'] ordering = self.get_ordering(request, queryset, view) if not ordering: return queryset intersection = (set(ordering) & set(interface_ifnames + ifnames)) try: match_field = intersection.pop() except KeyError: return queryset else: if match_field in interface_ifnames: lookup = lambda x: natsort.split(x.interface.ifname) if match_field in ifnames: lookup = lambda x: natsort.split(x.ifname) return sorted(queryset, key=lookup, reverse=match_field.startswith('-'))
def get_context_data(self, **kwargs): gwport = kwargs.pop('object') prefixes = gwport.gwportprefix_set.select_related( 'prefix__vlan', ).exclude(prefix__vlan__net_type='static') vlans = [] vlans_found = set() for prefix in prefixes: for vlan in prefix.prefix.vlan.swportvlan_set.select_related( 'interface__to_interface__netbox', 'interface__netbox', 'vlan', ).filter(interface__netbox=gwport.netbox).order_by( 'interface__ifname'): # Check if port is spanningtreeblocked if vlan.interface.swportblocked_set.filter( vlan=vlan.vlan.vlan # really! ).count(): continue elif vlan and not vlan.pk in vlans_found: vlans_found.add(vlan.pk) interface = vlan.interface vlan_context = { 'pk': interface.pk, 'type': 'swport', 'interface': model_to_dict(interface), 'netbox_sysname': interface.netbox.sysname, 'module_netbox_sysname': interface.module.netbox.sysname if interface.module else '', 'subheader_vlan': unicode(vlan.vlan.vlan), 'subheader_netbox': unicode(interface.netbox), } # Check for children, services and connection # to switches vlan_context.update(self._get_expandable(vlan)) vlan_context.update(_get_connected_sysname(interface)) vlans.append(vlan_context) sorted_vlans = sorted( vlans, key=lambda vlan: natsort.split(vlan['interface']['ifname'])) return sorted_vlans
def get_context_data(self, **kwargs): gwport = kwargs.pop('object') prefixes = gwport.gwportprefix_set.select_related( 'prefix__vlan', ).exclude( prefix__vlan__net_type='static') vlans = [] vlans_found = set() for prefix in prefixes: for vlan in prefix.prefix.vlan.swportvlan_set.select_related( 'interface__to_interface__netbox', 'interface__netbox', 'vlan', ).filter( interface__netbox=gwport.netbox).order_by( 'interface__ifname'): # Check if port is spanningtreeblocked if vlan.interface.swportblocked_set.filter( vlan=vlan.vlan.vlan # really! ).count(): continue elif vlan and not vlan.pk in vlans_found: vlans_found.add(vlan.pk) interface = vlan.interface vlan_context = { 'pk': interface.pk, 'type': 'swport', 'interface': model_to_dict(interface), 'netbox_sysname': interface.netbox.sysname, 'module_netbox_sysname': interface.module.netbox.sysname if interface.module else '', 'subheader_vlan': unicode(vlan.vlan.vlan), 'subheader_netbox': unicode(interface.netbox), } # Check for children, services and connection # to switches vlan_context.update(self._get_expandable(vlan)) vlan_context.update(_get_connected_sysname(interface)) vlans.append(vlan_context) sorted_vlans = sorted( vlans, key=lambda vlan: natsort.split(vlan['interface']['ifname'])) return sorted_vlans
def get_context_data(self, **kwargs): switch = kwargs.pop('object') vlan_id = self.kwargs.pop('vlan_id', None) swports = switch.get_swports() swport_vlans = SwPortVlan.objects.select_related( 'interface__netbox', 'interface__to_interface__netbox', 'interface__to_netbox', ).filter( interface__in=swports, vlan__id=vlan_id) switch_has_services = switch.service_set.all().count() context = [] for vlan in swport_vlans: interface = vlan.interface c = { 'pk': vlan.interface.pk, 'type': 'switch', 'expandable': False, 'interface': model_to_dict(interface), 'interface_netbox_sysname': interface.netbox.sysname, 'interface_netbox_sysname_short': unicode(interface.netbox.sysname), } c.update(_get_connected_sysname(interface)) if interface.to_interface and switch_has_services: c.update({'expandable': True}) elif Cam.objects.filter( netbox=interface.netbox, ifindex=interface.ifindex, end_time__gt=dt.max).count(): c.update({'expandable': True}) context.append(c) return sorted( context, key=lambda vlan: natsort.split(vlan['interface']['ifname']) )
def test_natsort_split_should_split_correctly(string, expected): assert natsort.split(string) == expected