Пример #1
0
def create_static_interface(request, system_pk):
    # TODO, make sure the user has access to this system
    system = get_object_or_404(System, pk=system_pk)
    if request.method == 'POST':
        interface_form = StaticInterfaceForm(request.POST)
        interface_form.instance.system = system

        a, ptr, r = None, None, None
        if interface_form.is_valid():
            try:
                intr = interface_form.instance
                intr.system = system
                intr.full_clean()
                intr.save()
            except ValidationError, e:
                interface_form._errors['__all__'] = ErrorList(e.messages)
                return render(request, 'static_intr/static_intr_form.html', {
                    'form': interface_form,
                    'form_title': 'New Interface for System {0}'.format(
                        system)
                })
        else:
            return render(request, 'static_intr/static_intr_form.html', {
                'form': interface_form,
                'form_title': 'New Interface for System {0}'.format(system)
            })

        messages.success(request, "Success! Interface Created.")
        return redirect(system)
Пример #2
0
def create_static_interface(request, system_pk):
    # TODO, make sure the user has access to this system
    system = get_object_or_404(System, pk=system_pk)
    if request.method == 'POST':
        interface_form = StaticInterfaceForm(request.POST)
        interface_form.instance.system = system

        a, ptr, r = None, None, None
        if interface_form.is_valid():
            try:
                intr = interface_form.instance
                intr.system = system
                intr.full_clean()
                intr.save()
            except ValidationError, e:
                interface_form._errors['__all__'] = ErrorList(e.messages)
                return render(
                    request, 'static_intr/static_intr_form.html', {
                        'form': interface_form,
                        'form_title':
                        'New Interface for System {0}'.format(system)
                    })
        else:
            return render(
                request, 'static_intr/static_intr_form.html', {
                    'form': interface_form,
                    'form_title': 'New Interface for System {0}'.format(system)
                })

        messages.success(request, "Success! Interface Created.")
        return redirect(system)
Пример #3
0
def edit_static_interface(request, intr_pk):
    # TODO, make sure the user has access to this system
    intr = get_object_or_404(StaticInterface, pk=intr_pk)
    system = intr.system
    attrs = intr.staticintrkeyvalue_set.all()
    aa = get_aa(StaticIntrKeyValue())
    docs = get_docstrings(StaticIntrKeyValue())
    if request.method == 'POST':
        interface_form = StaticInterfaceForm(request.POST, instance=intr)
        if interface_form.is_valid():
            try:
                # Handle key value stuff.
                kv = None
                kv = get_attrs(request.POST)
                update_attrs(kv, attrs, StaticIntrKeyValue, intr, 'intr')
                intr = interface_form.save()

                # Everything checks out. Clean and Save all the objects.
                intr.clean()
                intr.save()
            except ValidationError, e:
                interface_form._errors['__all__'] = ErrorList(e.messages)
                if kv:
                    attrs = dict_to_kv(kv, StaticIntrKeyValue)
                return render(
                    request, 'static_intr/static_intr_edit.html', {
                        'form':
                        interface_form,
                        'intr':
                        intr,
                        'attrs':
                        attrs,
                        'aa':
                        json.dumps(aa),
                        'docs':
                        docs,
                        'form_title':
                        'Edit Interface for System {0}'.format(system),
                        'domain':
                        intr.domain
                    })
        else:
            return render(
                request, 'static_intr/static_intr_edit.html', {
                    'form': interface_form,
                    'intr': intr,
                    'attrs': attrs,
                    'aa': json.dumps(aa),
                    'docs': docs,
                    'form_title':
                    'Edit Interface for System {0}'.format(system),
                    'domain': intr.domain
                })

        messages.success(request, "Success! Interface Updated.")
        return redirect(intr.get_update_url())
Пример #4
0
def edit_static_interface(request, intr_pk):
    # TODO, make sure the user has access to this system
    intr = get_object_or_404(StaticInterface, pk=intr_pk)
    system = intr.system
    attrs = intr.staticintrkeyvalue_set.all()
    aa = get_aa(StaticIntrKeyValue())
    docs = get_docstrings(StaticIntrKeyValue())
    if request.method == 'POST':
        interface_form = StaticInterfaceForm(request.POST, instance=intr)
        if interface_form.is_valid():
            try:
                # Handle key value stuff.
                kv = None
                kv = get_attrs(request.POST)
                update_attrs(kv, attrs, StaticIntrKeyValue, intr, 'intr')
                intr = interface_form.save()

                # Everything checks out. Clean and Save all the objects.
                intr.clean()
                intr.save()
            except ValidationError, e:
                interface_form._errors['__all__'] = ErrorList(e.messages)
                if kv:
                    attrs = dict_to_kv(kv, StaticIntrKeyValue)
                return render(request, 'static_intr/static_intr_edit.html', {
                    'form': interface_form,
                    'intr': intr,
                    'attrs': attrs,
                    'aa': json.dumps(aa),
                    'docs': docs,
                    'form_title': 'Edit Interface for System {0}'.format(
                        system),
                    'domain': intr.domain
                })
        else:
            return render(request, 'static_intr/static_intr_edit.html', {
                'form': interface_form,
                'intr': intr,
                'attrs': attrs,
                'aa': json.dumps(aa),
                'docs': docs,
                'form_title': 'Edit Interface for System {0}'.format(
                    system),
                'domain': intr.domain
            })

        messages.success(request, "Success! Interface Updated.")
        return redirect(intr.get_update_url())
Пример #5
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}))
Пример #6
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}))
Пример #7
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}))
Пример #8
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,
        })
Пример #9
0
                request, 'static_intr/static_intr_edit.html', {
                    'form': interface_form,
                    'intr': intr,
                    'attrs': attrs,
                    'aa': json.dumps(aa),
                    'docs': docs,
                    'form_title':
                    'Edit Interface for System {0}'.format(system),
                    'domain': intr.domain
                })

        messages.success(request, "Success! Interface Updated.")
        return redirect(intr.get_update_url())

    else:
        interface_form = StaticInterfaceForm(instance=intr)
        return render(
            request, 'static_intr/static_intr_edit.html', {
                'form': interface_form,
                'intr': intr,
                'attrs': attrs,
                'aa': json.dumps(aa),
                'docs': docs,
                'form_title': 'Edit Interface for System {0}'.format(system),
                'domain': intr.domain
            })


def create_static_interface(request, system_pk):
    # TODO, make sure the user has access to this system
    system = get_object_or_404(System, pk=system_pk)
Пример #10
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,
    })