def edit_port(request, handle_id): nh, port = helpers.get_nh_node(handle_id) parent = port.get_parent() connected_to = port.get_connected_to() parent_categories = ['external-equipment', 'firewall', 'host', 'odf', 'optical-node', 'router', 'switch'] connections_categories = Dropdown.get('cable_types').as_values(False) if request.POST: form = forms.EditPortForm(request.POST) if form.is_valid(): # Generic node update helpers.form_update_node(request.user, port.handle_id, form) # Port specific updates if form.cleaned_data['relationship_parent']: parent_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_parent']) helpers.set_has(request.user, parent_nh.get_node(), port.handle_id) if form.cleaned_data['relationship_connected_to']: cable_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_connected_to']) helpers.set_connected_to(request.user, cable_nh.get_node(), port.handle_id) if 'saveanddone' in request.POST: return redirect(nh.get_absolute_url()) else: return redirect('%sedit' % nh.get_absolute_url()) else: form = forms.EditPortForm(port.data) return render(request, 'noclook/edit/edit_port.html', {'node_handle': nh, 'form': form, 'node': port, 'parent': parent, 'connected_to': connected_to, 'parent_categories': parent_categories, 'connections_categories': connections_categories})
def edit_cable(request, handle_id): # Get needed data from node nh, cable = helpers.get_nh_node(handle_id) connections = cable.get_connected_equipment() relations = cable.get_relations() if request.POST: form = forms.EditCableForm(request.POST) if form.is_valid(): # Generic node update helpers.form_update_node(request.user, cable.handle_id, form) if form.cleaned_data['relationship_end_a']: end_a_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_end_a']) helpers.set_connected_to(request.user, cable, end_a_nh.handle_id) if form.cleaned_data['relationship_end_b']: end_b_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_end_b']) helpers.set_connected_to(request.user, cable, end_b_nh.handle_id) if form.cleaned_data['relationship_provider']: owner_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_provider']) helpers.set_provider(request.user, cable, owner_nh.handle_id) if 'saveanddone' in request.POST: return redirect(nh.get_absolute_url()) else: return redirect('%sedit' % nh.get_absolute_url()) else: form = forms.EditCableForm(cable.data) return render(request, 'noclook/edit/edit_cable.html', {'node_handle': nh, 'form': form, 'node': cable, 'connections': connections, 'relations': relations})
def obj_update(self, bundle, **kwargs): bundle = super(CableResource, self).obj_update(bundle, **kwargs) end_point_nodes = self.get_end_point_nodes(bundle) node = bundle.obj.get_node() if end_point_nodes: for result in node.relationships.get('Connected_to', []): helpers.delete_relationship(bundle.request.user, result['relationship_id']) for end_point in end_point_nodes: helpers.set_connected_to(bundle.request.user, node, end_point.handle_id) return bundle
def obj_create(self, bundle, **kwargs): try: node_type = helpers.slug_to_node_type(self.Meta.resource_name, create=True) NodeHandle.objects.get(node_name=bundle.data['node_name'], node_type=node_type) raise_conflict_error('Cable ID (%s) is already in use.' % bundle.data['node_name']) except NodeHandle.DoesNotExist: bundle.data.update(self._initial_form_data(bundle)) bundle.data['name'] = bundle.data['node_name'] form = common_forms.NewCableForm(bundle.data) if form.is_valid(): bundle.data.update({ 'node_name': form.cleaned_data['name'], 'creator': '/api/%s/user/%d/' % (self._meta.api_name, bundle.request.user.pk), 'modifier': '/api/%s/user/%d/' % (self._meta.api_name, bundle.request.user.pk), }) node_data = bundle.data.get('node', {}) node_data.update( {'cable_type': form.cleaned_data['cable_type']}) bundle.data['node'] = node_data del bundle.data['name'] # Create the new cable bundle = super(NodeHandleResource, self).obj_create(bundle, **kwargs) # Depend the created service on provided end points end_point_nodes = self.get_end_point_nodes(bundle) node = bundle.obj.get_node() for end_point in end_point_nodes: helpers.set_connected_to(bundle.request.user, node, end_point.handle_id) return self.hydrate_node(bundle) else: raise_not_acceptable_error([ "%s is missing or incorrect." % key for key in form.errors.keys() ])
def create_cable(self, name=None): # create object if not name: name = self.fake.hostname() cable = self.get_or_create_node( self.fake.hostname(), 'Cable', META_TYPES[0]) # Physical # add context self.add_network_context(cable) # add data cable_types = self.get_dropdown_keys('cable_types') # check if there's any provider or if we should create one provider_type = NetworkFakeDataGenerator.get_nodetype('Provider') providers = NodeHandle.objects.filter(node_type=provider_type) max_providers = self.max_cable_providers provider = None if not providers or len(providers) < max_providers: provider = self.create_provider() else: provider = random.choice(list(providers)) # add ports port_a = None port_b = None port_type = NetworkFakeDataGenerator.get_nodetype('Port') total_ports = NodeHandle.objects.filter(node_type=port_type).count() if total_ports < self.max_ports_total: port_a = self.create_port() port_b = self.create_port() else: all_ports = list(NodeHandle.objects.filter(node_type=port_type)) port_a = random.choice(all_ports) port_b = random.choice(all_ports) data = { 'cable_type' : random.choice(cable_types), 'description' : self.escape_quotes(self.fake.paragraph()), 'relationship_provider' : provider.handle_id, 'relationship_end_a' : port_a.handle_id, 'relationship_end_b' : port_b.handle_id, } if sunet_forms_enabled(): try: cable_contract = random.choice( DropModel.objects.get(name="tele2_cable_contracts").as_choices()[1:][1] ) data['tele2_cable_contract'] = cable_contract except DropModel.DoesNotExist: logger.warn('Tele2 cable contracts dropdown is not available') circuit_id = self.escape_quotes(self.fake.ean8()) data['tele2_alternative_circuit_id'] = circuit_id for key, value in data.items(): value = self.escape_quotes(value) cable.get_node().add_property(key, value) # add relationship to provider helpers.set_provider(self.user, cable.get_node(), provider.handle_id) helpers.set_connected_to(self.user, cable.get_node(), port_a.handle_id) helpers.set_connected_to(self.user, cable.get_node(), port_b.handle_id) return cable