def network_select(request,object_id): if request.method == 'POST': host = request.POST.getlist('host') NetworkHost.objects.filter(network = object_id).delete() for hosts in host: network_entry = NetworkHost(network_id = object_id, host_id = hosts.replace("/","")) network_entry.save() return HttpResponseRedirect('../.././../list')
def network_detail(request, object_id): """ Network detail page has the following features: * displaying basic network info (name, description, etc.) * listing hosts related to network * creating relations between network and host * removing relations between network and host(s) """ network, edit = get_object_or_forbidden(Network, object_id, request.user) # remove relation between the network and selected host(s) if request.POST.getlist('remove_host'): if edit: hosts_pk = request.POST.getlist('remove_host') network_host = NetworkHost.objects.filter(network=network, host__pk__in=hosts_pk) network_host.delete() # create relation between the network and selected host if request.POST.get('add_host'): if edit: host = Host.objects.get(pk=request.POST.get('add_host')) network_host = NetworkHost(network=network, host=host) network_host.save() queryset = Network.objects.filter(user=request.user) if network.hosts(): hosts_ids = [host.pk for host in network.hosts()] hosts_other = Host.objects.exclude(pk__in=hosts_ids).filter(user=request.user) else: hosts_other = Host.objects.filter(user=request.user) extra_context = { 'hosts_other': hosts_other, 'can_edit': edit } return object_detail(request, queryset, object_id, extra_context=extra_context)
def subnet_network(request): if request.method == 'POST': form = SubnetCreateFrom(request.POST) if form.is_valid(): subnet = form.cleaned_data['Subnet_Address'] ip = form.cleaned_data['IP_Address'] user_host = get_hosts(user=request.user) hosts_list = get_subnet(user_host, subnet,ip) subnet_network = form.save() network_obj = Network.objects.get(name__exact = form.cleaned_data['name']) for hosts in hosts_list: network_entry = NetworkHost(network_id = network_obj.id, host_id = hosts.id) network_entry.save() extra_context = { 'form': SubnetCreateFrom(initial={'user': request.user.pk}), 'host_list': hosts_list } return redirect_to(request, url=subnet_network.get_absolute_url()) else: form = SubnetCreateFrom() extra_context = { 'form':SubnetCreateFrom(initial={'user': request.user.pk}) } return direct_to_template(request,'networks/subnet_form.html',extra_context)