Beispiel #1
0
Datei: edit.py Projekt: hmpf/nav
def netbox_edit(request, netbox_id=None, suggestion=None, action='edit'):
    """Controller for edit or create of netbox"""
    info = NI()
    netbox = None
    copy_url = None
    if netbox_id:
        netbox = get_object_or_404(Netbox, pk=netbox_id)
        if action == 'edit':
            copy_url = reverse_lazy(
                info.copy_url_name, kwargs={'action': 'copy', 'netbox_id': netbox_id}
            )

    if request.method == 'POST':
        if action == 'copy':
            # Remove stuff that should not be copied over
            post = request.POST.copy()
            post.pop('sysname', None)
            post.pop('type', None)
            post.pop('virtual_instance', None)
            form = NetboxModelForm(post)
        else:
            form = NetboxModelForm(request.POST, instance=netbox)
        if form.is_valid():
            old_netbox = copy.deepcopy(netbox)
            netbox = netbox_do_save(form)
            messages.add_message(request, messages.SUCCESS, 'IP Device saved')
            log_netbox_change(request.account, old_netbox, netbox)
            return redirect(reverse('seeddb-netbox-edit', args=[netbox.pk]))
        else:
            messages.add_message(request, messages.ERROR, 'Form was not valid')
    else:
        if suggestion:
            form = NetboxModelForm(instance=netbox, initial={'ip': suggestion})
        else:
            form = NetboxModelForm(instance=netbox)

    page_title = "Add new IP Device"
    if netbox:
        page_title = "Edit IP Device"
        if action == 'copy':
            page_title = "Copy IP Device"
    context = info.template_context
    context.update(
        {
            'object': netbox,
            'form': form,
            'title': page_title,
            '_navpath': [('Edit Device', reverse_lazy('seeddb-netbox-edit'))],
            'sub_active': netbox and {'edit': True} or {'add': True},
            'tab_template': 'seeddb/tabs_generic.html',
            'copy_url': copy_url,
            'copy_title': 'Use this netbox as a template for creating a new netbox',
            'action': action,
        }
    )
    return render(request, 'seeddb/netbox_wizard.html', context)
Beispiel #2
0
def netbox_edit(request, netbox_id=None):
    """Controller for edit or create of netbox"""
    netbox = None
    if netbox_id:
        netbox = get_object_or_404(Netbox, pk=netbox_id)

    old_netbox = copy.deepcopy(netbox)

    if request.method == 'POST':
        form = NetboxModelForm(request.POST, instance=netbox)
        if form.is_valid():
            netbox = netbox_do_save(form)
            messages.add_message(request, messages.SUCCESS, 'IP Device saved')
            log_netbox_change(request.account, old_netbox, netbox)
            return redirect(reverse('seeddb-netbox-edit', args=[netbox.pk]))
        else:
            messages.add_message(request, messages.ERROR, 'Form was not valid')
    else:
        suggestion = request.GET.get('suggestion')
        if suggestion:
            form = NetboxModelForm(instance=netbox, initial={'ip': suggestion})
        else:
            form = NetboxModelForm(instance=netbox)

    info = NI()
    context = info.template_context
    context.update({
        'object':
        netbox,
        'form':
        form,
        'title':
        get_title(netbox),
        '_navpath': [('Edit Device', reverse_lazy('seeddb-netbox-edit'))],
        'sub_active':
        netbox and {
            'edit': True
        } or {
            'add': True
        },
        'tab_template':
        'seeddb/tabs_generic.html',
    })
    return render(request, 'seeddb/netbox_wizard.html', context)