mirror of
https://github.com/RobbieHan/sandboxMP.git
synced 2026-04-24 19:00:22 +08:00
upload&auto updatee
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
from django import forms
|
||||
|
||||
from .models import Code, DeviceInfo, ConnectionInfo
|
||||
from .models import Code, DeviceInfo, ConnectionInfo, DeviceFile
|
||||
|
||||
|
||||
class CodeCreateForm(forms.ModelForm):
|
||||
@@ -97,3 +97,9 @@ class ConnectionInfoForm(forms.ModelForm):
|
||||
raise forms.ValidationError('认证类型为[密码]时,必须设置密码信息!')
|
||||
if auth_type == 'private_key' and len(private_key) == 0:
|
||||
raise forms.ValidationError('认证类型为[密钥]时,必须设置密钥信息!')
|
||||
|
||||
|
||||
class DeviceFileUploadForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = DeviceFile
|
||||
fields = '__all__'
|
||||
@@ -3,7 +3,7 @@ import os
|
||||
from django.dispatch import receiver
|
||||
from django.db.models.signals import post_delete, post_save
|
||||
|
||||
from .models import DeviceFile, DeviceInfo
|
||||
from .models import DeviceFile, DeviceInfo, ConnectionInfo
|
||||
from utils.db_utils import MongodbDriver
|
||||
|
||||
|
||||
@@ -37,3 +37,10 @@ def auto_compare_diff(sender, instance, **kwargs):
|
||||
mongo.insert(compare_result)
|
||||
except Exception as e:
|
||||
pass
|
||||
|
||||
|
||||
@receiver(post_delete, sender=DeviceInfo)
|
||||
def auto_delete_connection(sender, instance, **kwargs):
|
||||
dev_connection = getattr(instance, 'dev_connection')
|
||||
if dev_connection:
|
||||
ConnectionInfo.objects.filter(id=dev_connection).delete()
|
||||
@@ -34,4 +34,8 @@ urlpatterns = [
|
||||
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'),
|
||||
path('eam/device/detail/', views_eam.DeviceDetailView.as_view(), name='eam-device-detail'),
|
||||
path('eam/device/upload/', views_eam.DeviceFileUploadView.as_view(), name='eam-device-upload'),
|
||||
path('eam/device/file_delete/', views_eam.DeviceFileDeleteView.as_view(), name='eam-device-file_delete'),
|
||||
path('eam/device/auto_update_device_info/', views_eam.AutoUpdateDeviceInfo.as_view(),
|
||||
name='eam-device-auto_update_device_info'),
|
||||
]
|
||||
|
||||
@@ -5,13 +5,15 @@ 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 django.forms.models import model_to_dict
|
||||
|
||||
from system.mixin import LoginRequiredMixin
|
||||
from custom import (BreadcrumbMixin, SandboxDeleteView,
|
||||
SandboxListView, SandboxUpdateView, SandboxCreateView)
|
||||
from .models import Cabinet, DeviceInfo, Code, ConnectionInfo
|
||||
from .forms import DeviceCreateForm, DeviceUpdateForm, ConnectionInfoForm
|
||||
from .models import Cabinet, DeviceInfo, Code, ConnectionInfo, DeviceFile
|
||||
from .forms import DeviceCreateForm, DeviceUpdateForm, ConnectionInfoForm, DeviceFileUploadForm
|
||||
from utils.db_utils import MongodbDriver
|
||||
from utils.sandbox_utils import LoginExecution
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
@@ -174,4 +176,60 @@ class DeviceDetailView(LoginRequiredMixin, BreadcrumbMixin, TemplateView):
|
||||
kwargs['logs'] = logs
|
||||
kwargs['all_file'] = all_file
|
||||
kwargs.update(device_public)
|
||||
return super().get_context_data(**kwargs)
|
||||
return super().get_context_data(**kwargs)
|
||||
|
||||
|
||||
class DeviceFileUploadView(LoginRequiredMixin, View):
|
||||
|
||||
def get(self, request):
|
||||
ret = dict()
|
||||
device = get_object_or_404(DeviceInfo, pk=request.GET['id'])
|
||||
ret['device'] = device
|
||||
return render(request, 'cmdb/deviceinfo_upload.html', ret)
|
||||
|
||||
def post(self, request):
|
||||
res = dict(result=False)
|
||||
device_file = DeviceFile()
|
||||
upload_form = DeviceFileUploadForm(
|
||||
request.POST, request.FILES, instance=device_file
|
||||
)
|
||||
if upload_form.is_valid():
|
||||
upload_form.save()
|
||||
res['result'] = True
|
||||
return JsonResponse(res)
|
||||
|
||||
|
||||
class DeviceFileDeleteView(SandboxDeleteView):
|
||||
model = DeviceFile
|
||||
|
||||
|
||||
class AutoUpdateDeviceInfo(LoginRequiredMixin, View):
|
||||
|
||||
def post(self, request):
|
||||
res = dict(status='fail')
|
||||
if 'id' in request.POST and request.POST['id']:
|
||||
device = get_object_or_404(DeviceInfo, pk=int(request.POST['id']))
|
||||
con_id = device.dev_connection
|
||||
conn = ConnectionInfo.objects.filter(id=con_id)
|
||||
if con_id and conn:
|
||||
try:
|
||||
conn_info = conn.get()
|
||||
kwargs = model_to_dict(conn_info, exclude=['id', 'auth_type'])
|
||||
auth_type = conn_info.auth_type
|
||||
le = LoginExecution()
|
||||
data = le.login_execution(auth_type=auth_type, **kwargs)
|
||||
conn_info.status = data['status']
|
||||
conn_info.save()
|
||||
if data['status'] == 'succeed':
|
||||
device.sys_hostname = data['sys_hostname']
|
||||
device.mac_address = data['mac_address']
|
||||
device.sn_number = data['sn_number']
|
||||
device.os_type = data['os_type']
|
||||
device.device_type = data['device_type']
|
||||
device.save()
|
||||
res['status'] = 'success'
|
||||
except conn.model.DoesNotExist:
|
||||
res['status'] = 'con_empty'
|
||||
else:
|
||||
res['status'] = 'con_empty'
|
||||
return JsonResponse(res)
|
||||
|
||||
Reference in New Issue
Block a user