From f1dde180fbf6c3db4cecf96cc3297293cab35141 Mon Sep 17 00:00:00 2001 From: RobbieHan Date: Tue, 12 Feb 2019 16:37:24 +0800 Subject: [PATCH] device2connection --- apps/cmdb/forms.py | 28 +++++- apps/cmdb/urls.py | 2 +- apps/cmdb/views_eam.py | 46 ++++++++- templates/cmdb/deviceinfo.html | 14 +++ templates/cmdb/deviceinfo2connection.html | 108 ++++++++++++++++++++++ 5 files changed, 192 insertions(+), 6 deletions(-) create mode 100644 templates/cmdb/deviceinfo2connection.html diff --git a/apps/cmdb/forms.py b/apps/cmdb/forms.py index 6a2cd55..bd17f75 100644 --- a/apps/cmdb/forms.py +++ b/apps/cmdb/forms.py @@ -4,7 +4,7 @@ from django import forms -from .models import Code, DeviceInfo +from .models import Code, DeviceInfo, ConnectionInfo class CodeCreateForm(forms.ModelForm): @@ -72,4 +72,28 @@ class DeviceUpdateForm(DeviceCreateForm): if self.instance: matching_device = DeviceInfo.objects.exclude(pk=self.instance.pk) if matching_device.filter(hostname=hostname).exists(): - raise forms.ValidationError('设备地址:{}已存在'.format(hostname)) \ No newline at end of file + raise forms.ValidationError('设备地址:{}已存在'.format(hostname)) + + +class ConnectionInfoForm(forms.ModelForm): + + class Meta: + model = ConnectionInfo + fields = '__all__' + + error_messages = { + 'port': {'required': '端口不能为空'}, + } + + def clean(self): + cleaned_data = self.cleaned_data + username = cleaned_data.get('username') + password = cleaned_data.get('password') + private_key = cleaned_data.get('private_key') + auth_type = cleaned_data.get('auth_type') + if len(username) == 0: + raise forms.ValidationError('用户名不能为空!') + if auth_type == 'password' and len(password) == 0: + raise forms.ValidationError('认证类型为[密码]时,必须设置密码信息!') + if auth_type == 'private_key' and len(private_key) == 0: + raise forms.ValidationError('认证类型为[密钥]时,必须设置密钥信息!') diff --git a/apps/cmdb/urls.py b/apps/cmdb/urls.py index bc68639..4b94c76 100644 --- a/apps/cmdb/urls.py +++ b/apps/cmdb/urls.py @@ -32,5 +32,5 @@ urlpatterns = [ path('eam/device/update/', views_eam.DeviceUpdateView.as_view(), name='eam-device-update'), path('eam/device/list/', views_eam.DeviceListView.as_view(), name='eam-device-list'), path('eam/device/delete/', views_eam.DeviceDeleteView.as_view(), name='eam-device-delete'), - + path('eam/device/device2connection/', views_eam.Device2ConnectionView.as_view(), name='eam-device-device2connection'), ] diff --git a/apps/cmdb/views_eam.py b/apps/cmdb/views_eam.py index c522681..557a269 100644 --- a/apps/cmdb/views_eam.py +++ b/apps/cmdb/views_eam.py @@ -1,12 +1,16 @@ -from django.views.generic import TemplateView +import re + +from django.views.generic import TemplateView, View from django.contrib.auth import get_user_model from django.shortcuts import get_object_or_404 +from django.http import JsonResponse +from django.shortcuts import render from system.mixin import LoginRequiredMixin from custom import (BreadcrumbMixin, SandboxDeleteView, SandboxListView, SandboxUpdateView, SandboxCreateView) -from .models import Cabinet, DeviceInfo, Code -from .forms import DeviceCreateForm, DeviceUpdateForm +from .models import Cabinet, DeviceInfo, Code, ConnectionInfo +from .forms import DeviceCreateForm, DeviceUpdateForm, ConnectionInfoForm User = get_user_model() @@ -118,3 +122,39 @@ class DeviceUpdateView(SandboxUpdateView): class DeviceDeleteView(SandboxDeleteView): model = DeviceInfo + + +class Device2ConnectionView(LoginRequiredMixin, View): + + def get(self, request): + ret = dict() + if 'id' in request.GET and request.GET['id']: + device = get_object_or_404(DeviceInfo, pk=int(request.GET['id'])) + ret['device'] = device + dev_connection = device.dev_connection + if dev_connection: + connection_info = get_object_or_404( + ConnectionInfo, pk=int(dev_connection) + ) + ret['connection_info'] = connection_info + return render(request, 'cmdb/deviceinfo2connection.html', ret) + + def post(self, request): + res = dict(result=False) + con_info = ConnectionInfo() + if 'id' in request.POST and request.POST['id']: + con_info = get_object_or_404(ConnectionInfo, pk=request.POST['id']) + form = ConnectionInfoForm(request.POST, instance=con_info) + if form.is_valid(): + instance = form.save() + con_id = getattr(instance, 'id') + device = get_object_or_404(DeviceInfo, hostname=request.POST['hostname']) + device.dev_connection = con_id + device.save() + res['result'] = True + else: + pattern = '
  • .*?