Commit 4e337898 by Őry Máté

network: fix magic at HostDetail view

parent 1838f416
...@@ -43,7 +43,6 @@ from braces.views import LoginRequiredMixin, SuperuserRequiredMixin ...@@ -43,7 +43,6 @@ from braces.views import LoginRequiredMixin, SuperuserRequiredMixin
# from django.db.models import Q # from django.db.models import Q
from operator import itemgetter from operator import itemgetter
from itertools import chain from itertools import chain
import json
from dashboard.views import AclUpdateView from dashboard.views import AclUpdateView
from dashboard.forms import AclUserOrGroupAddForm from dashboard.forms import AclUserOrGroupAddForm
...@@ -65,6 +64,16 @@ except ImportError: ...@@ -65,6 +64,16 @@ except ImportError:
content_type=content_type) content_type=content_type)
class MagicMixin(object):
def get(self, *args, **kwargs):
if self.request.is_ajax():
result = self._get_ajax(*args, **kwargs)
return JsonResponse({k: unicode(result[k] or "") for k in result})
else:
return super(MagicMixin, self).get(*args, **kwargs)
class InitialOwnerMixin(FormMixin): class InitialOwnerMixin(FormMixin):
def get_initial(self): def get_initial(self):
initial = super(InitialOwnerMixin, self).get_initial() initial = super(InitialOwnerMixin, self).get_initial()
...@@ -329,6 +338,25 @@ class GroupDelete(LoginRequiredMixin, SuperuserRequiredMixin, DeleteView): ...@@ -329,6 +338,25 @@ class GroupDelete(LoginRequiredMixin, SuperuserRequiredMixin, DeleteView):
return context return context
class HostMagicMixin(MagicMixin):
def _get_ajax(self, *args, **kwargs):
GET = self.request.GET
result = {}
vlan = get_object_or_404(Vlan.objects, pk=GET.get("vlan", ""))
if "ipv4" in GET:
try:
result["ipv6"] = vlan.convert_ipv4_to_ipv6(GET["ipv4"]) or ""
except:
result["ipv6"] = ""
else:
try:
result.update(vlan.get_new_address())
except ValidationError:
result["ipv4"] = ""
result["ipv6"] = ""
return result
class HostList(LoginRequiredMixin, SuperuserRequiredMixin, SingleTableView): class HostList(LoginRequiredMixin, SuperuserRequiredMixin, SingleTableView):
model = Host model = Host
table_class = HostTable table_class = HostTable
...@@ -350,15 +378,15 @@ class HostList(LoginRequiredMixin, SuperuserRequiredMixin, SingleTableView): ...@@ -350,15 +378,15 @@ class HostList(LoginRequiredMixin, SuperuserRequiredMixin, SingleTableView):
return data return data
class HostDetail(LoginRequiredMixin, SuperuserRequiredMixin, class HostDetail(HostMagicMixin, LoginRequiredMixin, SuperuserRequiredMixin,
SuccessMessageMixin, UpdateView): SuccessMessageMixin, UpdateView):
model = Host model = Host
template_name = "network/host-edit.html" template_name = "network/host-edit.html"
form_class = HostForm form_class = HostForm
success_message = _(u'Successfully modified host %(hostname)s!') success_message = _(u'Successfully modified host %(hostname)s!')
def get(self, request, *args, **kwargs): def _get_ajax(self, *args, **kwargs):
if request.is_ajax(): if "vlan" not in self.request.GET:
host = Host.objects.get(pk=kwargs['pk']) host = Host.objects.get(pk=kwargs['pk'])
host = { host = {
'hostname': host.hostname, 'hostname': host.hostname,
...@@ -366,9 +394,11 @@ class HostDetail(LoginRequiredMixin, SuperuserRequiredMixin, ...@@ -366,9 +394,11 @@ class HostDetail(LoginRequiredMixin, SuperuserRequiredMixin,
'ipv6': str(host.ipv6), 'ipv6': str(host.ipv6),
'fqdn': host.get_fqdn() 'fqdn': host.get_fqdn()
} }
return HttpResponse(json.dumps(host), return host
content_type="application/json")
else: else:
return super(HostDetail, self)._get_ajax(*args, **kwargs)
def get(self, request, *args, **kwargs):
self.object = self.get_object() self.object = self.get_object()
return super(HostDetail, self).get(request, *args, **kwargs) return super(HostDetail, self).get(request, *args, **kwargs)
...@@ -421,7 +451,7 @@ class HostDetail(LoginRequiredMixin, SuperuserRequiredMixin, ...@@ -421,7 +451,7 @@ class HostDetail(LoginRequiredMixin, SuperuserRequiredMixin,
return reverse_lazy('network.host', kwargs=self.kwargs) return reverse_lazy('network.host', kwargs=self.kwargs)
class HostCreate(LoginRequiredMixin, SuperuserRequiredMixin, class HostCreate(HostMagicMixin, LoginRequiredMixin, SuperuserRequiredMixin,
SuccessMessageMixin, InitialOwnerMixin, CreateView): SuccessMessageMixin, InitialOwnerMixin, CreateView):
model = Host model = Host
template_name = "network/host-create.html" template_name = "network/host-create.html"
...@@ -444,29 +474,6 @@ class HostCreate(LoginRequiredMixin, SuperuserRequiredMixin, ...@@ -444,29 +474,6 @@ class HostCreate(LoginRequiredMixin, SuperuserRequiredMixin,
messages.error(self.request, e.message) messages.error(self.request, e.message)
return initial return initial
def _get_ajax(self, *args, **kwargs):
GET = self.request.GET
result = {}
vlan = get_object_or_404(Vlan.objects, pk=GET.get("vlan", ""))
if "ipv4" in GET:
try:
result["ipv6"] = vlan.convert_ipv4_to_ipv6(GET["ipv4"])
except:
result["ipv6"] = ""
else:
try:
result.update(vlan.get_new_address())
except ValidationError:
result["ipv4"] = ""
result["ipv6"] = ""
return JsonResponse({k: unicode(result[k] or "") for k in result})
def get(self, *args, **kwargs):
if self.request.is_ajax():
return self._get_ajax(*args, **kwargs)
else:
return super(HostCreate, self).get(*args, **kwargs)
class HostDelete(LoginRequiredMixin, SuperuserRequiredMixin, DeleteView): class HostDelete(LoginRequiredMixin, SuperuserRequiredMixin, DeleteView):
model = Host model = Host
...@@ -702,7 +709,7 @@ class VlanAclUpdateView(AclUpdateView): ...@@ -702,7 +709,7 @@ class VlanAclUpdateView(AclUpdateView):
model = Vlan model = Vlan
class VlanMagicMixin(object): class VlanMagicMixin(MagicMixin):
def _get_ajax(self, *args, **kwargs): def _get_ajax(self, *args, **kwargs):
GET = self.request.GET GET = self.request.GET
result = {} result = {}
...@@ -713,13 +720,7 @@ class VlanMagicMixin(object): ...@@ -713,13 +720,7 @@ class VlanMagicMixin(object):
IPNetwork(GET['network6']))) IPNetwork(GET['network6'])))
except: except:
result["ipv6_template"] = result["host_ipv6_prefixlen"] = "" result["ipv6_template"] = result["host_ipv6_prefixlen"] = ""
return JsonResponse({k: unicode(result[k] or "") for k in result}) return result
def get(self, *args, **kwargs):
if self.request.is_ajax():
return self._get_ajax(*args, **kwargs)
else:
return super(VlanMagicMixin, self).get(*args, **kwargs)
class VlanDetail(VlanMagicMixin, LoginRequiredMixin, SuperuserRequiredMixin, class VlanDetail(VlanMagicMixin, LoginRequiredMixin, SuperuserRequiredMixin,
......
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