Пример #1
0
 def test_get_network_tree(self):
     res = get_network_tree()
     correct = [
         {
             'network': self.net1,
             'subnetworks': [
                 {
                     'network': self.net2,
                     'subnetworks': [],
                 },
                 {
                     'network': self.net3,
                     'subnetworks': [
                         {
                             'network': self.net4,
                             'subnetworks': [],
                         }
                     ]
                 },
             ]
         },
         {
             'network': self.net5,
             'subnetworks': [],
         }
     ]
     self.assertEquals(res, correct)
Пример #2
0
    def get_context_data(self, **kwargs):
        ret = super(SidebarNetworks, self).get_context_data(**kwargs)
        self.set_network()
        networks = Network.objects.all().select_related("kind__icon")
        contains = self.request.GET.get('contains')
        if contains:
            networks_filtered = Network.objects.filter(
                Q(name__contains=contains) | Q(address__contains=contains)
            )
            minimax = [(n.min_ip, n.max_ip) for n in networks_filtered]
            ids = []
            for mini, maxi in minimax:
                ids.extend(
                    map(
                        lambda net: net['id'],
                        Network.objects.filter(
                            min_ip__gte=mini,
                            max_ip__lte=maxi,
                        ).values('id')
                    )
                )
            networks = networks.filter(id__in=ids)
        network_kind = self.request.GET.get('kind')
        if network_kind:
            networks = networks.filter(
                kind__id=int(network_kind),
            )
        networks = networks.order_by('-min_ip', 'max_ip')

        sidebar_items = cache.get('cache_network_sidebar_items')
        if not sidebar_items:
            self.networks = get_network_tree(
                qs=networks
            )
            sidebar_items = []
            sidebar_items.extend(
                network_tree_menu(
                    self.networks,
                    ret['details'],
                    status=self.status,
                    get_params=self.request.GET.urlencode(),
                ),
            )
            # 24 hours cache
            cache.set('cache_network_sidebar_items', sidebar_items, 24 * 60)

        ret.update({
            'sidebar_items': sidebar_items,
            'sidebar_selected': (self.network.name if
                                 self.network else self.network),
            'section': 'networks',
            'subsection': self.network.name if self.network else self.network,
            'searchform': NetworksFilterForm(self.request.GET),
            'searchform_filter': True,
            'sidebar_alternative_span': 'span3',
            'content_alternative_span': 'span9',
        })
        return ret
Пример #3
0
    def get_context_data(self, **kwargs):
        ret = super(SidebarNetworks, self).get_context_data(**kwargs)
        self.set_network()
        networks = Network.objects.all().select_related("kind__icon")
        contains = self.request.GET.get('contains')
        if contains:
            networks_filtered = Network.objects.filter(
                Q(name__contains=contains) | Q(address__contains=contains)
            )
            minimax = [(n.min_ip, n.max_ip) for n in networks_filtered]
            ids = []
            for mini, maxi in minimax:
                ids.extend(
                    map(
                        lambda net: net['id'],
                        Network.objects.filter(
                            min_ip__gte=mini,
                            max_ip__lte=maxi,
                        ).values('id')
                    )
                )
            networks = networks.filter(id__in=ids)
        network_kind = self.request.GET.get('kind')
        if network_kind:
            networks = networks.filter(
                kind__id=int(network_kind),
            )
        networks = networks.order_by('-min_ip', 'max_ip')

        sidebar_items = cache.get('cache_network_sidebar_items')
        if not sidebar_items:
            self.networks = get_network_tree(
                qs=networks
            )
            sidebar_items = []
            sidebar_items.extend(
                network_tree_menu(
                    self.networks,
                    ret['details'],
                    status=self.status,
                    get_params=self.request.GET.urlencode(),
                ),
            )
            # indef.
            cache.set('cache_network_sidebar_items', sidebar_items, None)

        ret.update({
            'sidebar_items': sidebar_items,
            'sidebar_selected': (self.network.name if
                                 self.network else self.network),
            'section': 'networks',
            'subsection': self.network.name if self.network else self.network,
            'searchform': NetworksFilterForm(self.request.GET),
            'searchform_filter': True,
            'sidebar_alternative_span': 'span3',
            'content_alternative_span': 'span9',
        })
        return ret
Пример #4
0
    def get_context_data(self, **kwargs):
        ret = super(SidebarNetworks, self).get_context_data(**kwargs)
        self.set_network()
        networks = Network.objects.all().select_related("kind__icon")
        contains = self.request.GET.get("contains")
        if contains:
            networks_filtered = Network.objects.filter(Q(name__contains=contains) | Q(address__contains=contains))
            minimax = [(n.min_ip, n.max_ip) for n in networks_filtered]
            ids = []
            for mini, maxi in minimax:
                ids.extend(
                    map(lambda net: net["id"], Network.objects.filter(min_ip__gte=mini, max_ip__lte=maxi).values("id"))
                )
            networks = networks.filter(id__in=ids)
        network_kind = self.request.GET.get("kind")
        if network_kind:
            networks = networks.filter(kind__id=int(network_kind))
        networks = networks.order_by("-min_ip", "max_ip")

        sidebar_items = cache.get("cache_network_sidebar_items")
        if not sidebar_items:
            self.networks = get_network_tree(qs=networks)
            sidebar_items = []
            sidebar_items.extend(
                network_tree_menu(
                    self.networks, ret["details"], status=self.status, get_params=self.request.GET.urlencode()
                )
            )
            # indef.
            cache.set("cache_network_sidebar_items", sidebar_items, None)

        ret.update(
            {
                "sidebar_items": sidebar_items,
                "sidebar_selected": (self.network.name if self.network else self.network),
                "section": "networks",
                "subsection": self.network.name if self.network else self.network,
                "searchform": NetworksFilterForm(self.request.GET),
                "searchform_filter": True,
                "sidebar_alternative_span": "span3",
                "content_alternative_span": "span9",
            }
        )
        return ret
Пример #5
0
 def test_get_network_tree(self):
     res = get_network_tree()
     correct = [{
         'network':
         self.net1,
         'subnetworks': [
             {
                 'network': self.net2,
                 'subnetworks': [],
             },
             {
                 'network': self.net3,
                 'subnetworks': [{
                     'network': self.net4,
                     'subnetworks': [],
                 }]
             },
         ]
     }, {
         'network': self.net5,
         'subnetworks': [],
     }]
     self.assertEquals(res, correct)