mirror of
https://github.com/RobbieHan/sandboxMP.git
synced 2026-04-14 10:31:14 +08:00
device2connection
This commit is contained in:
@@ -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))
|
||||
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('认证类型为[密钥]时,必须设置密钥信息!')
|
||||
|
||||
@@ -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'),
|
||||
]
|
||||
|
||||
@@ -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 = '<li>.*?<ul class=.*?><li>(.*?)</li>'
|
||||
form_errors = str(form.errors)
|
||||
errors = re.findall(pattern, form_errors)
|
||||
res['error'] = errors[0]
|
||||
return JsonResponse(res)
|
||||
Reference in New Issue
Block a user