mirror of
https://github.com/RobbieHan/sandboxMP.git
synced 2026-02-02 18:38:53 +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)
|
||||
|
||||
2462
static/plugins/masonry/masonry.js
Normal file
2462
static/plugins/masonry/masonry.js
Normal file
File diff suppressed because it is too large
Load Diff
@@ -185,8 +185,132 @@
|
||||
history.back();
|
||||
});
|
||||
|
||||
// 资产文件瀑布流
|
||||
|
||||
$('#imageContainer').masonry({
|
||||
columnWidth: 10,
|
||||
itemSelector: '.imageItem'
|
||||
});
|
||||
|
||||
//上传资料
|
||||
function doUpload(id) {
|
||||
var div = layer.open({
|
||||
type: 2,
|
||||
title: '上传设备文件',
|
||||
shadeClose: false,
|
||||
maxmin: true,
|
||||
area: ['770px', '400px'],
|
||||
content: ["/cmdb/eam/device/upload/" + '?id=' + id],
|
||||
end: function () {
|
||||
window.location.reload();
|
||||
|
||||
}
|
||||
});
|
||||
layer.full(div)
|
||||
}
|
||||
|
||||
//删除文件
|
||||
function doDelete(id) {
|
||||
layer.alert('确定删除吗?', {
|
||||
title: '提示'
|
||||
, icon: 3
|
||||
, time: 0
|
||||
, btn: ['YES', 'NO']
|
||||
, yes: function (index) {
|
||||
layer.close(index);
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
url: "{% url 'cmdb:eam-device-file_delete' %}",
|
||||
data: {"id": id, csrfmiddlewaretoken: '{{ csrf_token }}'},
|
||||
cache: false,
|
||||
success: function (msg) {
|
||||
if (msg.result) {
|
||||
layer.alert('删除成功', {icon: 1}, function () {
|
||||
parent.location.reload()
|
||||
});
|
||||
} else {
|
||||
//alert(msg.message);
|
||||
layer.alert('删除失败', {icon: 2});
|
||||
}
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
//自动更新设备信息
|
||||
function doAutoUpdate(id) {
|
||||
layer.alert('确定自动更新设备信息?', {
|
||||
title: '提示'
|
||||
, icon: 3 //0:感叹号 1:对号 2:差号 3:问号 4:小锁 5:哭脸 6:笑脸
|
||||
, time: 0 //不自动关闭
|
||||
, btn: ['YES', 'NO']
|
||||
, yes: function (index) {
|
||||
layer.close(index);
|
||||
$.ajax({
|
||||
type: "POST",
|
||||
data: {"id": id, csrfmiddlewaretoken: '{{ csrf_token }}'},
|
||||
url: "{% url 'cmdb:eam-device-auto_update_device_info' %}",
|
||||
cache: false,
|
||||
beforeSend:function(){
|
||||
this.layerIndex = layer.load(2, {
|
||||
shade: [0.1,'#fff']
|
||||
});
|
||||
},
|
||||
success: function (msg) {
|
||||
layer.closeAll('loading');
|
||||
if (msg.status == 'success') {
|
||||
layer.alert('设备信息更新成功!', {icon: 1}, function(){
|
||||
parent.location.reload()
|
||||
});
|
||||
}
|
||||
else if (msg.status == 'con_empty') {
|
||||
layer.alert('请先添加认证信息!', {icon: 4});
|
||||
|
||||
}
|
||||
else {
|
||||
//alert(msg.message);
|
||||
layer.alert('设备信息更新失败!', {icon: 2});
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
//修改数据
|
||||
function doUpdate(id) {
|
||||
var div=layer.open({
|
||||
type: 2,
|
||||
title: '编辑',
|
||||
shadeClose: false,
|
||||
maxmin: true,
|
||||
area: ['800px', '400px'],
|
||||
content: ["{% url 'cmdb:eam-device-update' %}" + '?id=' + id, 'no'],
|
||||
end: function () {
|
||||
window.location.reload();
|
||||
}
|
||||
});
|
||||
layer.full(div )
|
||||
}
|
||||
|
||||
function doDevice2Connection(id) {
|
||||
layer.open({
|
||||
type: 2,
|
||||
title: '认证管理',
|
||||
shadeClose: false,
|
||||
maxmin: true,
|
||||
area: ['800px', '400px'],
|
||||
content: ["{% url 'cmdb:eam-device-device2connection' %}" + '?id=' + id, 'no'],
|
||||
end: function () {
|
||||
oDataTable.ajax.reload();
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
{% endblock %}
|
||||
65
templates/cmdb/deviceinfo_upload.html
Normal file
65
templates/cmdb/deviceinfo_upload.html
Normal file
@@ -0,0 +1,65 @@
|
||||
{% extends 'base-layer.html' %}
|
||||
{% load staticfiles %}
|
||||
|
||||
{% block css %}
|
||||
<link rel="stylesheet" href="{%static 'plugins/select2/select2.min.css' %}">
|
||||
<link rel="stylesheet" href="{% static 'js/plugins/layer/skin/layer.css' %}">
|
||||
<link rel="stylesheet" href="{% static 'bootstrap/css/bootstrap-datetimepicker.min.css' %}">
|
||||
<link rel="stylesheet" href="{% static 'plugins/fileinput/fileinput.css' %}">
|
||||
|
||||
{% endblock %}
|
||||
{% block main %}
|
||||
<div class="box box-danger">
|
||||
<form class="form-horizontal" id="addForm" method="post">
|
||||
{% csrf_token %}
|
||||
<div class="box-body">
|
||||
<fieldset>
|
||||
<div class="form-group has-feedback">
|
||||
<div class="col-sm-12">
|
||||
<div class="file-loading">
|
||||
<input id="file_content" name="file_content" type="file" multiple="multiple"/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<div class="form-group has-feedback">
|
||||
<label class="col-sm-2 control-label"></label>
|
||||
<div class="col-sm-12">
|
||||
<p class="text-red">同时最多可上传4个文件,支持文件格式:png", "jpg", "gif", "zip", "rar",大小不得超过10M</p>
|
||||
</div>
|
||||
</div>
|
||||
</fieldset>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block javascripts %}
|
||||
<script src="{% static 'plugins/fileinput/fileinput.js' %}"></script>
|
||||
<script src="{% static 'plugins/fileinput/zh.js' %}"></script>
|
||||
<script type="text/javascript">
|
||||
|
||||
//上传文件
|
||||
|
||||
$(document).on('ready', function() {
|
||||
$("#file_content").fileinput({
|
||||
language: "zh",
|
||||
showUpload: true,
|
||||
allowedFileExtensions: ["png", "jpg", "gif", "zip", "rar"],
|
||||
uploadUrl: "{% url 'cmdb:eam-device-upload' %}",
|
||||
uploadExtraData: {
|
||||
'csrfmiddlewaretoken': '{{ csrf_token }}',
|
||||
'device': '{{ device.id }}',
|
||||
'upload_user': '{{ request.user.name }}',
|
||||
},
|
||||
maxFileCount: 4,
|
||||
autoReplace: true,
|
||||
maxFileSize: 10240,
|
||||
});
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user