Commit 8d391e1f by Őry Máté

network: accept initial fields in HostCreate

parent 84a1d61c
...@@ -17,8 +17,9 @@ ...@@ -17,8 +17,9 @@
from django.views.generic import (TemplateView, UpdateView, DeleteView, from django.views.generic import (TemplateView, UpdateView, DeleteView,
CreateView) CreateView)
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse_lazy from django.core.urlresolvers import reverse_lazy
from django.shortcuts import render, redirect from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse from django.http import HttpResponse
from django_tables2 import SingleTableView from django_tables2 import SingleTableView
...@@ -409,6 +410,20 @@ class HostCreate(LoginRequiredMixin, SuperuserRequiredMixin, ...@@ -409,6 +410,20 @@ class HostCreate(LoginRequiredMixin, SuperuserRequiredMixin,
form_class = HostForm form_class = HostForm
success_message = _(u'Successfully created host %(hostname)s!') success_message = _(u'Successfully created host %(hostname)s!')
def get_initial(self):
initial = super(HostCreate, self).get_initial()
for i in ("vlan", "mac", "hostname"):
if i in self.request.GET and i not in self.request.POST:
initial[i] = self.request.GET[i]
if "vlan" in initial:
vlan = get_object_or_404(Vlan.objects, pk=initial['vlan'])
try:
initial.update(vlan.get_new_address())
except ValidationError as e:
messages.error(self.request, e.message)
return initial
class HostDelete(LoginRequiredMixin, SuperuserRequiredMixin, DeleteView): class HostDelete(LoginRequiredMixin, SuperuserRequiredMixin, DeleteView):
model = Host model = Host
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment