Example #1
0
def validate_system_create_form(request):
    system = None
    post_data = copy(request.POST)
    if not post_data.get('ctnr'):
        post_data['ctnr'] = request.session['ctnr'].id

    system_form = ExtendedSystemForm(post_data)
    if system_form.is_valid():
        system = system_form.save()
        post_data['system'] = system.id
    else:
        return HttpResponse(json.dumps({'errors': system_form.errors}))

    if post_data.get('interface_type', None) == 'static_interface':
        try:
            post_data['ip_type'] = ipaddr.IPAddress(
                post_data.get('ip_str', None)).version
        except:
            post_data['ip_type'] = None

        form = StaticInterfaceForm(post_data)
    else:
        form = DynamicInterfaceForm(post_data)

    if form.is_valid():
        try:
            form.save()
            return HttpResponse(
                json.dumps({
                    'success': True,
                    'system_id': system.id
                }))
        except ValidationError as e:
            if form.errors is None:
                form.errors = ErrorDict()
            form.errors.update(e.message_dict)
            return HttpResponse(json.dumps({'errors': form.errors}))
    else:
        if system:
            system.delete()

        return HttpResponse(json.dumps({'errors': form.errors}))
Example #2
0
def validate_system_create_form(request):
    system = None
    post_data = copy(request.POST)
    if not post_data.get('ctnr'):
        post_data['ctnr'] = request.session['ctnr'].id

    system_form = ExtendedSystemForm(post_data)
    if system_form.is_valid():
        system = system_form.save()
        post_data['system'] = system.id
    else:
        return HttpResponse(json.dumps({'errors': system_form.errors}))

    if post_data.get('interface_type', None) == 'static_interface':
        try:
            post_data['ip_type'] = ipaddr.IPAddress(
                post_data.get('ip_str', None)).version
        except:
            post_data['ip_type'] = None

        form = StaticInterfaceForm(post_data)
    else:
        form = DynamicInterfaceForm(post_data)

    if form.is_valid():
        try:
            form.save()
            return HttpResponse(json.dumps(
                {'success': True, 'system_id': system.id}))
        except ValidationError as e:
            if form.errors is None:
                form.errors = ErrorDict()
            form.errors.update(e.message_dict)
            return HttpResponse(json.dumps({'errors': form.errors}))
    else:
        if system:
            system.delete()

        return HttpResponse(json.dumps({'errors': form.errors}))
Example #3
0
def system_create_view(request):
    if request.method == 'POST':
        return validate_system_create_form(request)

    initial_type = None
    static_form = StaticInterfaceForm()
    dynamic_form = DynamicInterfaceForm()
    system_form = ExtendedSystemForm()
    if(request.GET.get('initial', None)):
        initial_type = request.GET['interface_type']
        system_form = ExtendedSystemForm(
            initial={'interface_type': initial_type})
        system_form.fields['interface_type'].widget = forms.HiddenInput()
        if request.GET.get('ip_str', None):
            static_form = StaticInterfaceForm(
                initial={'ip_str': request.GET['ip_str']})
            static_form.fields['range'].widget = forms.HiddenInput()
            static_form.fields['vrf'].widget = forms.HiddenInput()
            static_form.fields['site'].widget = forms.HiddenInput()
            static_form.fields['next_ip'].widget = forms.HiddenInput()
        elif request.GET.get('range', None):
            dynamic_form = DynamicInterfaceForm(
                initial={'range': request.GET['range']})
            dynamic_form.fields['vrf'].widget = forms.HiddenInput()
            dynamic_form.fields['site'].widget = forms.HiddenInput()

    Range = get_model('cyder', 'range')
    static_form.fields['system'].widget = forms.HiddenInput()
    static_form.fields['range'].queryset = Range.objects.filter(range_type='st')
    dynamic_form.fields['system'].widget = forms.HiddenInput()
    dynamic_form.fields['range'].queryset = Range.objects.filter(range_type='dy')
    static_form.fields['ip_type'].widget = forms.HiddenInput()

    # ctnr field shouldn't exist on these forms
    dynamic_form.fields['ctnr'].widget = forms.HiddenInput()
    static_form.fields['ctnr'].widget = forms.HiddenInput()

    system_form.make_usable(request)

    static_form.make_usable(request)
    dynamic_form.make_usable(request)

    return HttpResponse(json.dumps({
        'form_title': 'Creating system',
        'submit_btn_label': 'Create system',
        'system_form': system_form.as_p(),
        'static_form': static_form.as_p(),
        'dynamic_form': dynamic_form.as_p(),
        'initial_type': initial_type}))
Example #4
0
def system_create_view(request):
    static_form = StaticInterfaceForm()
    dynamic_form = DynamicInterfaceForm()
    system_form = ExtendedSystemForm()
    if request.method == 'POST':
        if (request.POST.get('initial', None)
                and request.POST.get('interface_type', None)):
            system_form = ExtendedSystemForm(
                initial={'interface_type': request.POST['interface_type']})
            if request.POST.get('ip_str', None):
                static_form = StaticInterfaceForm(
                    initial={'ip_str': request.POST['ip_str']})
            elif request.POST.get('range', None):
                dynamic_form = DynamicInterfaceForm(
                    initial={'range': request.POST['range']})

        else:
            system = None
            post_data = copy(request.POST)
            if not post_data.get('ctnr'):
                post_data['ctnr'] = request.session['ctnr'].id

            system_form = ExtendedSystemForm(post_data)
            if system_form.is_valid():
                system = system_form.save()
                post_data['system'] = system.id
            else:
                return HttpResponse(json.dumps({'errors': system_form.errors}))

            if post_data.get('interface_type', None) == 'static_interface':
                try:
                    post_data['ip_type'] = ipaddr.IPAddress(
                        post_data.get('ip_str', None)).version
                except:
                    post_data['ip_type'] = None

                form = StaticInterfaceForm(post_data)
                static_form = form
            else:
                form = DynamicInterfaceForm(post_data)
                dynamic_form = form

            if form.is_valid():
                try:
                    form.save()
                    return HttpResponse(
                        json.dumps({
                            'success': True,
                            'system_id': system.id
                        }))
                except ValidationError as e:
                    if form.errors is None:
                        form.errors = ErrorDict()
                    form.errors.update(e.message_dict)
                    return HttpResponse(json.dumps({'errors': form.errors}))
            else:
                if system:
                    system.delete()

                return HttpResponse(json.dumps({'errors': form.errors}))

    static_form.fields['system'].widget = forms.HiddenInput()
    dynamic_form.fields['system'].widget = forms.HiddenInput()
    static_form.fields['ip_type'].widget = forms.HiddenInput()

    if request.session['ctnr'].name != 'global':
        dynamic_form.fields['ctnr'].widget = forms.HiddenInput()
        static_form.fields['ctnr'].widget = forms.HiddenInput()

    system_form.make_usable(request)
    static_form.make_usable(request)
    dynamic_form.make_usable(request)

    initial_interface_type = request.GET.get('interface_type', '')

    return render(
        request, 'system/system_create.html', {
            'system_form': system_form,
            'static_form': static_form,
            'dynamic_form': dynamic_form,
            'initial_interface_type': initial_interface_type,
        })
Example #5
0
def system_create_view(request):
    static_form = StaticInterfaceForm()
    dynamic_form = DynamicInterfaceForm()
    system_form = ExtendedSystemForm()
    if request.method == 'POST':
        if (request.POST.get('initial', None) and
                request.POST.get('interface_type', None)):
            system_form = ExtendedSystemForm(
                initial={'interface_type': request.POST['interface_type']})
            if request.POST.get('ip_str', None):
                static_form = StaticInterfaceForm(
                    initial={'ip_str': request.POST['ip_str']})
            elif request.POST.get('range', None):
                dynamic_form = DynamicInterfaceForm(
                    initial={'range': request.POST['range']})

        else:
            system = None
            post_data = copy(request.POST)
            if not post_data.get('ctnr'):
                post_data['ctnr'] = request.session['ctnr'].id

            system_form = ExtendedSystemForm(post_data)
            if system_form.is_valid():
                system = system_form.save()
                post_data['system'] = system.id
            else:
                return HttpResponse(json.dumps({'errors': system_form.errors}))

            if post_data.get('interface_type', None) == 'static_interface':
                try:
                    post_data['ip_type'] = ipaddr.IPAddress(
                        post_data.get('ip_str', None)).version
                except:
                    post_data['ip_type'] = None

                form = StaticInterfaceForm(post_data)
                static_form = form
            else:
                form = DynamicInterfaceForm(post_data)
                dynamic_form = form

            if form.is_valid():
                try:
                    form.save()
                    return HttpResponse(json.dumps(
                        {'success': True, 'system_id': system.id}))
                except ValidationError as e:
                    if form.errors is None:
                        form.errors = ErrorDict()
                    form.errors.update(e.message_dict)
                    return HttpResponse(json.dumps({'errors': form.errors}))
            else:
                if system:
                    system.delete()

                return HttpResponse(json.dumps({'errors': form.errors}))


    static_form.fields['system'].widget = forms.HiddenInput()
    dynamic_form.fields['system'].widget = forms.HiddenInput()
    static_form.fields['ip_type'].widget = forms.HiddenInput()

    if request.session['ctnr'].name != 'global':
        dynamic_form.fields['ctnr'].widget = forms.HiddenInput()
        static_form.fields['ctnr'].widget = forms.HiddenInput()

    system_form.make_usable(request)
    static_form.make_usable(request)
    dynamic_form.make_usable(request)

    initial_interface_type = request.GET.get('interface_type', '')

    return render(request, 'system/system_create.html', {
        'system_form': system_form,
        'static_form': static_form,
        'dynamic_form': dynamic_form,
        'initial_interface_type': initial_interface_type,
    })