Exemple #1
0
def edit_optical_node(request, handle_id):
    # Get needed data from node
    nh, optical_node = helpers.get_nh_node(handle_id)
    location = optical_node.get_location()
    ports = optical_node.get_ports()
    if request.POST:
        form = forms.EditOpticalNodeForm(request.POST)
        if form.is_valid():
            # Generic node update
            helpers.form_update_node(request.user, optical_node.handle_id, form)
            # Optical Node specific updates
            _handle_location(request.user,
                             optical_node,
                             form.cleaned_data['relationship_location'])
            _handle_ports(optical_node,
                          form.cleaned_data['relationship_ports'],
                          request.user)
            if 'saveanddone' in request.POST:
                return redirect(nh.get_absolute_url())
            else:
                return redirect('%sedit' % nh.get_absolute_url())
    else:
        form = forms.EditOpticalNodeForm(optical_node.data)
    return render(request, 'noclook/edit/edit_optical_node.html',
                  {'node_handle': nh, 'node': optical_node, 'form': form, 'location': location,
                   'ports': ports})
Exemple #2
0
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})
Exemple #3
0
def edit_optical_link(request, handle_id):
    # Get needed data from node
    nh, link = helpers.get_nh_node(handle_id)
    relations = link.get_relations()
    depends_on = link.get_dependencies()
    if request.POST:
        form = forms.EditOpticalLinkForm(request.POST)
        if form.is_valid():
            if 'type' in form.cleaned_data:
                form.cleaned_data['type'] = form.cleaned_data['type'].name
            # Generic node update
            helpers.form_update_node(request.user, link.handle_id, form)
            # Optical Link node updates
            if form.cleaned_data['relationship_provider']:
                owner_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_provider'])
                helpers.set_provider(request.user, link, owner_nh.handle_id)
            if form.cleaned_data['relationship_end_a']:
                depends_on_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_end_a'])
                helpers.set_depends_on(request.user, link, depends_on_nh.handle_id)
            if form.cleaned_data['relationship_end_b']:
                depends_on_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_end_b'])
                helpers.set_depends_on(request.user, link, depends_on_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.EditOpticalLinkForm(link.data)
    return render(request, 'noclook/edit/edit_optical_link.html',
                  {'node_handle': nh, 'form': form, 'node': link, 'relations': relations,
                   'depends_on': depends_on})
Exemple #4
0
def edit_external_equipment(request, handle_id):
    # Get needed data from node
    nh, external_equipment = helpers.get_nh_node(handle_id)
    relations = external_equipment.get_relations()
    location = external_equipment.get_location()
    ports = external_equipment.get_ports()
    if request.POST:
        form = forms.EditExternalEquipmentForm(request.POST)
        if form.is_valid():
            # Generic node update
            helpers.form_update_node(request.user, external_equipment.handle_id, form)
            # External Equipment specific updates
            if form.cleaned_data['relationship_owner']:
                owner_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_owner'])
                helpers.set_owner(request.user, external_equipment, owner_nh.handle_id)
            _handle_location(request.user,
                             external_equipment,
                             form.cleaned_data['relationship_location'])
            _handle_ports(external_equipment,
                          form.cleaned_data['relationship_ports'],
                          request.user)
            if 'saveanddone' in request.POST:
                return redirect(nh.get_absolute_url())
            else:
                return redirect('%sedit' % nh.get_absolute_url())
    else:
        form = forms.EditExternalEquipmentForm(external_equipment.data)
    return render(request, 'noclook/edit/edit_external_equipment.html',
                  {'node_handle': nh, 'node': external_equipment, 'form': form, 'relations': relations,
                   'location': location, 'ports': ports})
Exemple #5
0
def edit_rack(request, handle_id):
    # Get needed data from node
    nh, rack = helpers.get_nh_node(handle_id)
    parent = rack.get_parent()
    located_in = rack.get_located_in()
    located_in_categories = ['host', 'odf', 'optical-node', 'router']
    if request.POST:
        form = forms.EditRackForm(request.POST)
        if form.is_valid():
            # Generic node update
            helpers.form_update_node(request.user, rack.handle_id, form)
            # Rack 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(), rack.handle_id)
            if form.cleaned_data['relationship_located_in']:
                equipment = NodeHandle.objects.get(pk=form.cleaned_data['relationship_located_in'])
                helpers.set_location(request.user, equipment.get_node(), rack.handle_id)
            if 'saveanddone' in request.POST:
                return redirect(nh.get_absolute_url())
            else:
                return redirect('%sedit' % nh.get_absolute_url())
    else:
        form = forms.EditRackForm(rack.data)
    return render(request, 'noclook/edit/edit_rack.html',
                  {'node_handle': nh, 'form': form, 'parent': parent, 'node': rack,
                      'located_in': located_in, 'parent_categories': 'site', 'located_in_categories': located_in_categories})
Exemple #6
0
def edit_router(request, handle_id):
    # Get needed data from node
    nh, router = helpers.get_nh_node(handle_id)
    location = router.get_location()
    if request.POST:
        form = forms.EditRouterForm(request.POST)
        ports_form = forms.BulkPortsForm(request.POST)
        if form.is_valid() and ports_form.is_valid():
            # Generic node update
            helpers.form_update_node(request.user, router.handle_id, form)
            # Router specific updates
            _handle_location(request.user,
                             router,
                             form.cleaned_data['relationship_location'])
            _handle_ports(router,
                          form.cleaned_data['relationship_ports'],
                          request.user)
            if not ports_form.cleaned_data['no_ports']:
                data = ports_form.cleaned_data
                helpers.bulk_create_ports(nh.get_node(), request.user, **data)
            if 'saveanddone' in request.POST:
                return redirect(nh.get_absolute_url())
            else:
                return redirect('%sedit' % nh.get_absolute_url())
    else:
        form = forms.EditRouterForm(router.data)
        ports_form = forms.BulkPortsForm({'port_type': 'LC', 'offset': 1, 'num_ports': '0'})
        return render(request, 'noclook/edit/edit_router.html',
                {'node_handle': nh, 'node': router, 'form': form, 'location': location, 'ports_form': ports_form})
Exemple #7
0
def edit_optical_path(request, handle_id):
    # Get needed data from node
    nh, path = helpers.get_nh_node(handle_id)
    relations = path.get_relations()
    depends_on = path.get_dependencies()
    dependency_categories = 'odf,optical-link,optical-multiplex-section,optical-node,router,switch,optical-path'
    if request.POST:
        form = forms.EditOpticalPathForm(request.POST)
        if form.is_valid():
            # Generic node update
            helpers.form_update_node(request.user, path.handle_id, form)
            # Optical Path node updates
            if form.cleaned_data['relationship_provider']:
                owner_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_provider'])
                helpers.set_provider(request.user, path, owner_nh.handle_id)
            if form.cleaned_data['relationship_depends_on']:
                depends_on_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_depends_on'])
                helpers.set_depends_on(request.user, path, depends_on_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.EditOpticalPathForm(path.data)
    return render(request, 'noclook/edit/edit_optical_path.html',
                  {'node_handle': nh, 'form': form, 'node': path, 'relations': relations,
                   'depends_on': depends_on, 'dependency_categories': dependency_categories})
Exemple #8
0
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})
Exemple #9
0
def update_rack_position(request, rack_handle_id, handle_id, position):
    """
    Updates the nodes rack_position if node is placed in rack
    """
    if request.method == 'POST':
        success = False
        nh, node = helpers.get_nh_node(handle_id)
        if nh.node_meta_type == 'Physical':
            helpers.dict_update_node(request.user, node.handle_id, {'rack_position': int(position)})
            success = True
        return JsonResponse({'success': success, 'position': '{}'.format(position), 'handle_id': node.handle_id})
    else:
        return Http404()
Exemple #10
0
def edit_host(request, handle_id):
    # Get needed data from node
    nh, host = helpers.get_nh_node(handle_id)
    location = host.get_location()
    relations = host.get_relations()
    depends_on = host.get_dependencies()
    host_services = host.get_host_services()
    ports = host.get_ports()
    dependency_categories = 'service,host'
    if request.POST:
        form = forms.EditHostForm(request.POST)
        if form.is_valid():
            # Generic node update
            helpers.form_update_node(request.user, host.handle_id, form)
            # Host specific updates
            if form.cleaned_data['relationship_user']:
                    user_nh = _nh_safe_get(form.cleaned_data['relationship_user'])
                    if user_nh:
                        helpers.set_user(request.user, host, user_nh.handle_id)
            if form.cleaned_data['relationship_owner']:
                owner_nh = _nh_safe_get(form.cleaned_data['relationship_owner'])
                if owner_nh:
                    helpers.set_owner(request.user, host, owner_nh.handle_id)
            # You can not set location and depends on at the same time
            if form.cleaned_data['relationship_depends_on']:
                depends_on_nh = _nh_safe_get(form.cleaned_data['relationship_depends_on'])
                if depends_on_nh:
                    helpers.set_depends_on(request.user, host, depends_on_nh.handle_id)
            elif form.cleaned_data['relationship_location']:
                _handle_location(request.user,
                                 host,
                                 form.cleaned_data['relationship_location'])
            if form.cleaned_data['services_locked'] and form.cleaned_data['services_checked']:
                helpers.remove_rogue_service_marker(request.user, host.handle_id)
            _handle_ports(host,
                          form.cleaned_data['relationship_ports'],
                          request.user)
            if 'saveanddone' in request.POST:
                return redirect(nh.get_absolute_url())
            else:
                return redirect('%sedit' % nh.get_absolute_url())
    else:
        form = forms.EditHostForm(host.data)
    context = {
        'node_handle': nh, 'node': host, 'form': form, 'location': location,
        'relations': relations, 'depends_on': depends_on, 'ports': ports,
        'host_services': host_services, 'dependency_categories': dependency_categories
    }
    return render(request, 'noclook/edit/edit_host.html', context)
Exemple #11
0
def edit_pdu(request, handle_id):
    # Get needed data from node
    nh, pdu = helpers.get_nh_node(handle_id)
    location = pdu.get_location()
    relations = pdu.get_relations()
    depends_on = pdu.get_dependencies()
    host_services = pdu.get_host_services()
    ports = pdu.get_ports()
    dependency_categories = 'service'
    ports_form = forms.BulkPortsForm(request.POST or None)
    if request.POST:
        form = forms.EditPDUForm(request.POST)
        if form.is_valid():
            # Generic node update
            helpers.form_update_node(request.user, pdu.handle_id, form)
            # Host specific updates
            if form.cleaned_data['relationship_user']:
                user_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_user'])
                helpers.set_user(request.user, pdu, user_nh.handle_id)
            if form.cleaned_data['relationship_owner']:
                owner_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_owner'])
                helpers.set_owner(request.user, pdu, owner_nh.handle_id)
            # You can not set location and depends on at the same time
            if form.cleaned_data['relationship_depends_on']:
                depends_on_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_depends_on'])
                helpers.set_depends_on(request.user, pdu, depends_on_nh.handle_id)
            elif form.cleaned_data['relationship_location']:
                _handle_location(request.user,
                                 pdu,
                                 form.cleaned_data['relationship_location'])
            if form.cleaned_data['services_locked'] and form.cleaned_data['services_checked']:
                helpers.remove_rogue_service_marker(request.user, pdu.handle_id)
            if ports_form.is_valid() and not ports_form.cleaned_data['no_ports']:
                data = ports_form.cleaned_data
                helpers.bulk_create_ports(pdu, request.user, **data)
            _handle_ports(pdu,
                          form.cleaned_data['relationship_ports'],
                          request.user)
            if 'saveanddone' in request.POST:
                return redirect(nh.get_absolute_url())
            else:
                return redirect('%sedit' % nh.get_absolute_url())
    else:
        form = forms.EditPDUForm(pdu.data)
    return render(request, 'noclook/edit/edit_pdu.html',
                  {'node_handle': nh, 'node': pdu, 'form': form, 'location': location,
                   'relations': relations, 'depends_on': depends_on, 'ports': ports,
                   'host_services': host_services, 'dependency_categories': dependency_categories,
                   'ports_form': ports_form})
Exemple #12
0
def edit_end_user(request, handle_id):
    # Get needed data from node
    nh, end_user = helpers.get_nh_node(handle_id)
    if request.POST:
        form = forms.EditEndUserForm(request.POST)
        if form.is_valid():
            # Generic node update
            helpers.form_update_node(request.user, end_user.handle_id, form)
            if 'saveanddone' in request.POST:
                return redirect(nh.get_absolute_url())
            else:
                return redirect('%sedit' % nh.get_absolute_url())
    else:
        form = forms.EditEndUserForm(end_user.data)
    return render(request, 'noclook/edit/edit_end_user.html', {'node_handle': nh, 'form': form, 'node': end_user})
Exemple #13
0
def delete_relationship(request, slug, handle_id, rel_id):
    """
    Removes the relationship if the node has a relationship matching the
    supplied id.
    """
    success = False
    if request.method == 'POST':
        nh, node = helpers.get_nh_node(handle_id)
        try:
            relationship = nc.get_relationship_model(nc.graphdb.manager, rel_id)
            if node.handle_id == relationship.start['handle_id'] or node.handle_id == relationship.end['handle_id']:
                activitylog.delete_relationship(request.user, relationship)
                relationship.delete()
                success = True
        except nc.exceptions.RelationshipNotFound:
            success = True
    return JsonResponse({'success': success, 'relationship_id': '{}'.format(rel_id)})
Exemple #14
0
def edit_peering_group(request, handle_id):
    # Get needed data from node
    nh, peering_group = helpers.get_nh_node(handle_id)
    depends_on = peering_group.get_dependencies()

    if request.POST:
        form = forms.EditPeeringGroupForm(request.POST)
        if form.is_valid():
            # Generic node update
            helpers.form_update_node(request.user, peering_group.handle_id, form)
            if 'saveanddone' in request.POST:
                return redirect(nh.get_absolute_url())
            else:
                return redirect('%sedit' % nh.get_absolute_url())
    else:
        form = forms.EditPeeringGroupForm(peering_group.data)
    return render(request, 'noclook/edit/edit_peering_group.html',
                  {'node_handle': nh, 'node': peering_group, 'form': form,
                      'depends_on': depends_on})
Exemple #15
0
def delete_node(request, slug, handle_id):
    """
    Removes the node and all its relationships.
    """
    redirect_url = '/{}'.format(slug)
    nh, node = helpers.get_nh_node(handle_id)
    if nh.node_type.get_label() == 'Unit':
        part_of = node.get_part_of()
        if part_of.get('Part_of'):
            redirect_url = helpers.get_node_url(part_of.get('Part_of')[0]['node'].handle_id)
    try:
        # Redirect to parent if deleted node was a child node
        parent = node.get_parent().get('Has', [])
        if parent:
            redirect_url = helpers.get_node_url(parent[0]['node'].handle_id)
    except AttributeError:
        pass
    helpers.delete_node(request.user, node.handle_id)
    return redirect(redirect_url)
Exemple #16
0
def edit_service(request, handle_id):
    # Get needed data from node
    nh, service = helpers.get_nh_node(handle_id)
    relations = service.get_relations()
    depends_on = service.get_dependencies()
    dependency_categories = ['host',
                             'firewall',
                             'odf',
                             'optical-node',
                             'optical-path',
                             'optical-filter',
                             'router',
                             'service',
                             'switch',
                             'external-equipment']
    user_categories = ['customer', 'end-user']
    if request.POST:
        form = forms.EditServiceForm(request.POST)
        if form.is_valid():
            # Generic node update
            helpers.form_update_node(request.user, service.handle_id, form)
            # Service node updates
            if form.cleaned_data['relationship_provider']:
                owner_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_provider'])
                helpers.set_provider(request.user, service, owner_nh.handle_id)
            if form.cleaned_data['relationship_user']:
                user_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_user'])
                helpers.set_user(request.user, service, user_nh.handle_id)
            if form.cleaned_data['relationship_depends_on']:
                depends_on_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_depends_on'])
                helpers.set_depends_on(request.user, service, depends_on_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.EditServiceForm(service.data)
    return render(request, 'noclook/edit/edit_service.html',
                  {'node_handle': nh, 'form': form, 'node': service, 'relations': relations,
                   'depends_on': depends_on, 'dependency_categories': dependency_categories,
                   'user_categories': user_categories})
Exemple #17
0
def edit_site(request, handle_id):
    # Get needed data from node
    nh, site = helpers.get_nh_node(handle_id)
    relations = site.get_relations()
    if request.POST:
        form = forms.EditSiteForm(request.POST)
        if form.is_valid():
            # Generic node update
            helpers.form_update_node(request.user, site.handle_id, form)
            # Set site owner
            if form.cleaned_data['relationship_responsible_for']:
                responsible_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_responsible_for'])
                helpers.set_responsible_for(request.user, site, responsible_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.EditSiteForm(site.data)
    return render(request, 'noclook/edit/edit_site.html',
                  {'node_handle': nh, 'form': form, 'relations': relations, 'node': site})
Exemple #18
0
def edit_optical_fillter(request, handle_id):
    nh, of = helpers.get_nh_node(handle_id)
    location = of.get_location()
    ports = of.get_ports()
    form = forms.EditOpticalFilterForm(request.POST or of.data)
    if request.POST and form.is_valid():
        # Generic node update
        helpers.form_update_node(request.user, of.handle_id, form)
        # Optical Filter specific updates
        _handle_location(request.user,
                         of,
                         form.cleaned_data['relationship_location'])
        _handle_ports(of,
                      form.cleaned_data['relationship_ports'],
                      request.user)
        if 'saveanddone' in request.POST:
            return redirect(nh.get_absolute_url())
        else:
            return redirect('%sedit' % nh.get_absolute_url())

    return render(request, 'noclook/edit/edit_optical_filter.html', {'node_handle': nh, 'node': of, 'form': form, 'location': location, 'ports': ports})
Exemple #19
0
def update_relationship(request, slug, handle_id, rel_id):
    """
    Removes the relationship if the node has a relationship matching the
    supplied id.
    """
    success = False
    properties = {}
    if request.POST:
        nh, node = helpers.get_nh_node(handle_id)
        try:
            for key, value in request.POST.items():
                properties[key] = json.loads(value)
            relationship = nc.get_relationship_model(nc.graphdb.manager, int(rel_id))
            if node.handle_id == relationship.start['handle_id'] or node.handle_id == relationship.end['handle_id']:
                success = helpers.dict_update_relationship(request.user, relationship.id, properties)
        except nc.exceptions.RelationshipNotFound:
            # If the relationship does not exist, then we cannot update
            success = False
        except ValueError:
            pass
    return JsonResponse({'success': success, 'relationship_id': '{}'.format(rel_id), 'data': properties})
Exemple #20
0
def edit_firewall(request, handle_id):
    # Get needed data from node
    nh, firewall = helpers.get_nh_node(handle_id)
    location = firewall.get_location()
    relations = firewall.get_relations()
    host_services = firewall.get_host_services()
    ports = firewall.get_ports()
    if request.POST:
        form = forms.EditFirewallForm(request.POST)
        if form.is_valid():
            # Generic node update
            helpers.form_update_node(request.user, firewall.handle_id, form)
            #  Firewall specific updates
            if form.cleaned_data['relationship_user']:
                user_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_user'])
                helpers.set_user(request.user, firewall, user_nh.handle_id)
            if form.cleaned_data['relationship_owner']:
                owner_nh = NodeHandle.objects.get(pk=form.cleaned_data['relationship_owner'])
                helpers.set_owner(request.user, firewall, owner_nh.handle_id)
            _handle_location(request.user,
                             firewall,
                             form.cleaned_data['relationship_location'])
            if form.cleaned_data['services_locked'] and form.cleaned_data['services_checked']:
                helpers.remove_rogue_service_marker(request.user, firewall.handle_id)
            _handle_ports(firewall,
                          form.cleaned_data['relationship_ports'],
                          request.user)
            if 'saveanddone' in request.POST:
                return redirect(nh.get_absolute_url())
            else:
                return redirect('%sedit' % nh.get_absolute_url())
    else:
        form = forms.EditFirewallForm(firewall.data)
    return render(request, 'noclook/edit/edit_firewall.html',
                  {'node_handle': nh, 'node': firewall, 'form': form, 'location': location,
                   'relations': relations, 'ports': ports,
                   'host_services': host_services})
Exemple #21
0
    def do_request(cls, request, **kwargs):
        form_class     = kwargs.get('form_class')
        nimetaclass    = getattr(cls, 'NIMetaClass')
        graphql_type   = getattr(nimetaclass, 'graphql_type')
        nimetatype     = getattr(graphql_type, 'NIMetaType')
        node_type      = getattr(nimetatype, 'ni_type').lower()
        node_meta_type = getattr(nimetatype, 'ni_metatype').capitalize()
        id      = request.POST.get('id')
        has_error      = False

        # check authorization
        handle_id = relay.Node.from_global_id(id)[1]
        authorized = sriutils.authorice_write_resource(request.user, handle_id)

        if not authorized:
            raise GraphQLAuthException()

        # Get needed data from node
        nh, organization = helpers.get_nh_node(handle_id)
        relations = organization.get_relations()
        out_relations = organization.get_outgoing_relations()

        if request.POST:
            # set handle_id into POST data and remove relay id
            post_data = request.POST.copy()
            post_data.pop('id')
            post_data.update({'handle_id': handle_id})

            # replace relay ids for handle_id in contacts if present
            for field, roledict in DEFAULT_ROLES.items():
                if field in post_data:
                    handle_id = post_data.get(field)
                    handle_id = relay.Node.from_global_id(handle_id)[1]
                    post_data.pop(field)
                    post_data.update({field: handle_id})

            relay_extra_ids = ('relationship_parent_of', 'relationship_uses_a')
            for field in relay_extra_ids:
                handle_id = post_data.get(field)
                if handle_id:
                    handle_id = relay.Node.from_global_id(handle_id)[1]
                    post_data.pop(field)
                    post_data.update({field: handle_id})

            form = form_class(post_data)
            form.strict_validation = True

            if form.is_valid():
                # Generic node update
                # use property keys to avoid inserting contacts as a string property of the node
                property_keys = [
                    'name', 'description', 'organization_id', 'type', 'incident_management_info',
                    'affiliation_customer', 'affiliation_end_customer', 'affiliation_provider',
                    'affiliation_partner', 'affiliation_host_user', 'affiliation_site_owner',
                    'website', 'organization_number'
                ]
                helpers.form_update_node(request.user, organization.handle_id, form, property_keys)

                # specific role setting
                for field, roledict in DEFAULT_ROLES.items():
                    if field in form.cleaned_data:
                        contact_id = form.cleaned_data[field]
                        role = RoleModel.objects.get(slug=field)
                        set_contact = helpers.get_contact_for_orgrole(organization.handle_id, role)

                        if contact_id:
                            if set_contact:
                                if set_contact.handle_id != contact_id:
                                    helpers.unlink_contact_with_role_from_org(request.user, organization, role)
                                    helpers.link_contact_role_for_organization(request.user, organization, contact_id, role)
                            else:
                                helpers.link_contact_role_for_organization(request.user, organization, contact_id, role)
                        elif set_contact:
                            helpers.unlink_contact_and_role_from_org(request.user, organization, set_contact.handle_id, role)

                # Set child organizations
                if form.cleaned_data['relationship_parent_of']:
                    organization_nh = NodeHandle.objects.get(handle_id=form.cleaned_data['relationship_parent_of'])
                    helpers.set_parent_of(request.user, organization, organization_nh.handle_id)
                if form.cleaned_data['relationship_uses_a']:
                    procedure_nh = NodeHandle.objects.get(handle_id=form.cleaned_data['relationship_uses_a'])
                    helpers.set_uses_a(request.user, organization, procedure_nh.handle_id)

                return has_error, { graphql_type.__name__.lower(): nh }
            else:
                # get the errors and return them
                has_error = True
                errordict = cls.format_error_array(form.errors)
                return has_error, errordict
        else:
            # get the errors and return them
            has_error = True
            errordict = cls.format_error_array(form.errors)
            return has_error, errordict
Exemple #22
0
    def do_request(cls, request, **kwargs):
        form_class     = kwargs.get('form_class')
        nimetaclass    = getattr(cls, 'NIMetaClass')
        graphql_type   = getattr(nimetaclass, 'graphql_type')
        nimetatype     = getattr(graphql_type, 'NIMetaType')
        node_type      = getattr(nimetatype, 'ni_type').lower()
        node_meta_type = getattr(nimetatype, 'ni_metatype').capitalize()
        context_method = getattr(nimetatype, 'context_method')
        has_error      = False

        context = context_method()

        # check it can write on this context
        authorized = sriutils.authorize_create_resource(request.user, context)

        if not authorized:
            raise GraphQLAuthException()

        # Get needed data from node
        if request.POST:
            # replace relay ids for handle_id in contacts if present
            post_data = request.POST.copy()

            for field, roledict in DEFAULT_ROLES.items():
                if field in post_data:
                    handle_id = post_data.get(field)
                    handle_id = relay.Node.from_global_id(handle_id)[1]
                    post_data.pop(field)
                    post_data.update({field: handle_id})

            relay_extra_ids = ('relationship_parent_of', 'relationship_uses_a')
            for field in relay_extra_ids:
                handle_id = post_data.get(field)
                if handle_id:
                    try:
                        handle_id = relay.Node.from_global_id(handle_id)[1]
                        post_data.pop(field)
                        post_data.update({field: handle_id})
                    except BinasciiError:
                        pass # the id is already in handle_id format

            form = form_class(post_data)
            form.strict_validation = True

            if form.is_valid():
                try:
                    nh = helpers.form_to_generic_node_handle(request, form,
                            node_type, node_meta_type, context)
                except UniqueNodeError:
                    has_error = True
                    return has_error, [ErrorType(field="_", messages=["A {} with that name already exists.".format(node_type)])]

                # Generic node update
                # use property keys to avoid inserting contacts as a string property of the node
                property_keys = [
                    'name', 'description', 'organization_id', 'type', 'incident_management_info',
                    'affiliation_customer', 'affiliation_end_customer', 'affiliation_provider',
                    'affiliation_partner', 'affiliation_host_user', 'affiliation_site_owner',
                    'website', 'organization_number'
                ]
                helpers.form_update_node(request.user, nh.handle_id, form, property_keys)
                nh_reload, organization = helpers.get_nh_node(nh.handle_id)

                # add default context
                NodeHandleContext(nodehandle=nh, context=context).save()

                # specific role setting
                for field, roledict in DEFAULT_ROLES.items():
                    if field in form.cleaned_data:
                        contact_id = form.cleaned_data[field]

                        role = RoleModel.objects.get(slug=field)
                        set_contact = helpers.get_contact_for_orgrole(organization.handle_id, role)

                        if contact_id:
                            if set_contact:
                                if set_contact.handle_id != contact_id:
                                    helpers.unlink_contact_with_role_from_org(request.user, organization, role)
                                    helpers.link_contact_role_for_organization(request.user, organization, contact_id, role)
                            else:
                                helpers.link_contact_role_for_organization(request.user, organization, contact_id, role)
                        elif set_contact:
                            helpers.unlink_contact_and_role_from_org(request.user, organization, set_contact.handle_id, role)

                # Set child organizations
                if form.cleaned_data['relationship_parent_of']:
                    organization_nh = NodeHandle.objects.get(handle_id=form.cleaned_data['relationship_parent_of'])
                    helpers.set_parent_of(request.user, organization, organization_nh.handle_id)
                if form.cleaned_data['relationship_uses_a']:
                    procedure_nh = NodeHandle.objects.get(handle_id=form.cleaned_data['relationship_uses_a'])
                    helpers.set_uses_a(request.user, organization, procedure_nh.handle_id)

                return has_error, { graphql_type.__name__.lower(): nh }
            else:
                # get the errors and return them
                has_error = True
                errordict = cls.format_error_array(form.errors)
                return has_error, errordict
        else:
            # get the errors and return them
            has_error = True
            errordict = cls.format_error_array(form.errors)
            return has_error, errordict