[Fixture] 添加更新硬件信息api

pull/417/head
ibuler 2017-04-04 19:16:34 +08:00
parent 992af0f1cb
commit 5a5d5bdd51
5 changed files with 97 additions and 64 deletions

View File

@ -2,6 +2,7 @@
from rest_framework import viewsets, generics, mixins from rest_framework import viewsets, generics, mixins
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework.views import APIView from rest_framework.views import APIView
from rest_framework_bulk import BulkModelViewSet, BulkDestroyAPIView from rest_framework_bulk import BulkModelViewSet, BulkDestroyAPIView
@ -14,6 +15,7 @@ from common.utils import get_object_or_none, signer
from .hands import IsSuperUser, IsAppUser, IsValidUser, get_user_granted_assets from .hands import IsSuperUser, IsAppUser, IsValidUser, get_user_granted_assets
from .models import AssetGroup, Asset, IDC, SystemUser, AdminUser from .models import AssetGroup, Asset, IDC, SystemUser, AdminUser
from . import serializers from . import serializers
from .tasks import update_assets_hardware_info
class AssetViewSet(IDInFilterMixin, BulkModelViewSet): class AssetViewSet(IDInFilterMixin, BulkModelViewSet):
@ -54,29 +56,25 @@ class AssetUpdateGroupApi(generics.RetrieveUpdateAPIView):
permission_classes = (IsSuperUser,) permission_classes = (IsSuperUser,)
## update the asset group, and add or delete the asset to the group
class AssetGroupUpdateApi(generics.RetrieveUpdateAPIView): class AssetGroupUpdateApi(generics.RetrieveUpdateAPIView):
queryset = AssetGroup.objects.all() queryset = AssetGroup.objects.all()
serializer_class = serializers.AssetGroupUpdateSerializer serializer_class = serializers.AssetGroupUpdateSerializer
permission_classes = (IsSuperUser,) permission_classes = (IsSuperUser,)
## update the asset group, and add or delete the system_user to the group
class AssetGroupUpdateSystemUserApi(generics.RetrieveUpdateAPIView): class AssetGroupUpdateSystemUserApi(generics.RetrieveUpdateAPIView):
queryset = AssetGroup.objects.all() queryset = AssetGroup.objects.all()
serializer_class = serializers.AssetGroupUpdateSystemUserSerializer serializer_class = serializers.AssetGroupUpdateSystemUserSerializer
permission_classes = (IsSuperUser,) permission_classes = (IsSuperUser,)
## update the IDC, and add or delete the assets to the IDC class IDCUpdateAssetsApi(generics.RetrieveUpdateAPIView):
class IDCupdateAssetsApi(generics.RetrieveUpdateAPIView):
queryset = IDC.objects.all() queryset = IDC.objects.all()
serializer_class = serializers.IDCUpdateAssetsSerializer serializer_class = serializers.IDCUpdateAssetsSerializer
permission_classes = (IsSuperUser,) permission_classes = (IsSuperUser,)
class IDCViewSet(IDInFilterMixin, BulkModelViewSet): class IDCViewSet(IDInFilterMixin, BulkModelViewSet):
"""API endpoint that allows IDC to be viewed or edited."""
queryset = IDC.objects.all() queryset = IDC.objects.all()
serializer_class = serializers.IDCSerializer serializer_class = serializers.IDCSerializer
permission_classes = (IsSuperUser,) permission_classes = (IsSuperUser,)
@ -147,3 +145,14 @@ class SystemUserAuthInfoApi(generics.RetrieveAPIView):
} }
return Response(data) return Response(data)
class AssetRefreshHardwareView(generics.RetrieveAPIView):
queryset = Asset.objects.all()
serializer_class = serializers.AssetSerializer
permission_classes = (IsSuperUser,)
def retrieve(self, request, *args, **kwargs):
asset_id = kwargs.get('pk')
asset = get_object_or_404(Asset, pk=asset_id)
update_assets_hardware_info([asset])
return super(AssetRefreshHardwareView, self).retrieve(request, *args, **kwargs)

View File

@ -188,7 +188,7 @@
<td>{% trans 'Rrefresh hardware' %}:</td> <td>{% trans 'Rrefresh hardware' %}:</td>
<td> <td>
<span class="pull-right"> <span class="pull-right">
<button type="button" class="btn btn-primary btn-xs" style="width: 54px">{% trans 'Refresh' %}</button> <button type="button" class="btn btn-primary btn-xs btn-refresh-asset" style="width: 54px">{% trans 'Refresh' %}</button>
</span> </span>
</td> </td>
</tr> </tr>
@ -353,20 +353,30 @@ function updateAssetSystem(system_users) {
}); });
} }
function refreshAssetHardware() {
var the_url = "{% url 'api-assets:asset-refresh' pk=asset.id %}";
var success = function (data) {
location.reload();
};
APIUpdateAttr({
url: the_url,
success: success,
method: 'GET'
})
}
$(document).ready(function () { $(document).ready(function () {
$('.select2').select2() $('.select2').select2().on('select2:select', function(evt) {
.on('select2:select', function(evt) { var data = evt.params.data;
var data = evt.params.data; jumpserver.groups_selected[data.id] = data.text;
jumpserver.groups_selected[data.id] = data.text; }).on('select2:unselect', function(evt) {
}).on('select2:unselect', function(evt) { var data = evt.params.data;
var data = evt.params.data; delete jumpserver.groups_selected[data.id]
delete jumpserver.groups_selected[data.id] })
}) }).on('click', '#is_active', function () {
}) var the_url = '{% url "api-assets:asset-detail" pk=asset.id %}';
.on('click', '#is_active', function () { var checked = $(this).prop('checked');
var the_url = '{% url "api-assets:asset-detail" pk=asset.id %}';
var checked = $(this).prop('checked');
var body = { var body = {
'is_active': checked 'is_active': checked
}; };
@ -382,21 +392,19 @@ $(document).ready(function () {
}else{ }else{
$(".ibox-content > table > tbody > tr:nth-child(13) > td:last >b").html('False'); $(".ibox-content > table > tbody > tr:nth-child(13) > td:last >b").html('False');
} }
}) }).on('click', '#btn_add_user_group', function () {
.on('click', '#btn_add_user_group', function () { if (Object.keys(jumpserver.groups_selected).length === 0) {
if (Object.keys(jumpserver.groups_selected).length === 0) { return false;
return false; }
} var groups = $('.bdg_group').map(function() {
var groups = $('.bdg_group').map(function() { return $(this).data('gid');
return $(this).data('gid'); }).get();
}).get(); $.map(jumpserver.groups_selected, function(value, index) {
$.map(jumpserver.groups_selected, function(value, index) { groups.push(parseInt(index));
groups.push(parseInt(index)); $('#opt_' + index).remove();
$('#opt_' + index).remove(); });
}); updateAssetGroups(groups)
updateAssetGroups(groups) }).on('click', '.btn_leave_group', function() {
})
.on('click', '.btn_leave_group', function() {
var $this = $(this); var $this = $(this);
var $tr = $this.closest('tr'); var $tr = $this.closest('tr');
var $badge = $tr.find('.bdg_group'); var $badge = $tr.find('.bdg_group');
@ -410,35 +418,33 @@ $(document).ready(function () {
return $(this).data('gid'); return $(this).data('gid');
}).get(); }).get();
updateAssetGroups(groups) updateAssetGroups(groups)
}) }).on('click', '.btn-system-user', function () {
.on('click', '.btn-system-user', function () { if (Object.keys(jumpserver.groups_selected).length === 0) {
if (Object.keys(jumpserver.groups_selected).length === 0) { return false;
return false; }
} var system_users = $('.bdg_group').map(function() {
var system_users = $('.bdg_group').map(function() { return $(this).data('sid');
return $(this).data('sid'); }).get();
}).get(); $.map(jumpserver.groups_selected, function(value, index) {
$.map(jumpserver.groups_selected, function(value, index) { system_users.push(parseInt(index));
system_users.push(parseInt(index)); $('#opt_' + index).remove();
$('#opt_' + index).remove(); });
}); updateAssetSystem(system_users)
updateAssetSystem(system_users)
}) }).on('click', '.btn_leave_system', function () {
.on('click', '.btn_leave_system', function () { var $this = $(this);
var $this = $(this); var $tr = $this.closest('tr');
var $tr = $this.closest('tr'); var $badge = $tr.find('.bdg_group');
var $badge = $tr.find('.bdg_group'); var sid = $badge.data('sid');
var sid = $badge.data('sid'); var name = $badge.html() || $badge.text();
var name = $badge.html() || $badge.text(); $('#groups_selected').append(
$('#groups_selected').append( '<option value="' + sid + '" id="opt_' + sid + '">' + name + '</option>'
'<option value="' + sid + '" id="opt_' + sid + '">' + name + '</option>' );
); $tr.remove();
$tr.remove(); var system_users = $('.bdg_group').map(function () {
var system_users = $('.bdg_group').map(function () { return $(this).data('sid');
return $(this).data('sid'); }).get();
}).get(); updateAssetSystem(system_users)
updateAssetSystem(system_users)
}).on('click', '.btn-delete-asset', function () { }).on('click', '.btn-delete-asset', function () {
var $this = $(this); var $this = $(this);
@ -447,6 +453,8 @@ $(document).ready(function () {
var the_url = '{% url "api-assets:asset-detail" pk=99991937 %}'.replace('99991937', uid); var the_url = '{% url "api-assets:asset-detail" pk=99991937 %}'.replace('99991937', uid);
var redirect_url = "{% url 'assets:asset-list' %}"; var redirect_url = "{% url 'assets:asset-list' %}";
objectDelete($this, name, the_url, redirect_url); objectDelete($this, name, the_url, redirect_url);
}).on('click', '.btn-refresh-asset', function () {
refreshAssetHardware()
}) })

View File

@ -22,6 +22,9 @@ urlpatterns = [
url(r'^v1/assets/(?P<pk>\d+)/groups/$', url(r'^v1/assets/(?P<pk>\d+)/groups/$',
api.AssetUpdateGroupApi.as_view(), name='asset-update-group'), api.AssetUpdateGroupApi.as_view(), name='asset-update-group'),
url(r'^v1/assets/(?P<pk>\d+)/refresh/$',
api.AssetRefreshHardwareView.as_view(), name='asset-refresh'),
url(r'^v1/assets/(?P<pk>\d+)/system-users/$', url(r'^v1/assets/(?P<pk>\d+)/system-users/$',
api.SystemUserUpdateApi.as_view(), name='asset-update-system-users'), api.SystemUserUpdateApi.as_view(), name='asset-update-system-users'),
@ -42,7 +45,7 @@ urlpatterns = [
# update the IDC, and add or delete the assets to the IDC # update the IDC, and add or delete the assets to the IDC
url(r'^v1/idc/(?P<pk>\d+)/assets/$', url(r'^v1/idc/(?P<pk>\d+)/assets/$',
api.IDCupdateAssetsApi.as_view(), name='idc-update-assets'), api.IDCUpdateAssetsApi.as_view(), name='idc-update-assets'),
] ]

View File

@ -14,6 +14,7 @@ urlpatterns = [
url(r'^asset/(?P<pk>[0-9]+)/$', views.AssetDetailView.as_view(), name='asset-detail'), url(r'^asset/(?P<pk>[0-9]+)/$', views.AssetDetailView.as_view(), name='asset-detail'),
url(r'^asset/(?P<pk>[0-9]+)/update/$', views.AssetUpdateView.as_view(), name='asset-update'), url(r'^asset/(?P<pk>[0-9]+)/update/$', views.AssetUpdateView.as_view(), name='asset-update'),
url(r'^asset/(?P<pk>[0-9]+)/delete/$', views.AssetDeleteView.as_view(), name='asset-delete'), url(r'^asset/(?P<pk>[0-9]+)/delete/$', views.AssetDeleteView.as_view(), name='asset-delete'),
url(r'^asset/(?P<pk>[0-9]+)/refresh/$', views.AssetRefreshHardwareView.as_view(), name='asset-refresh'),
url(r'^asset-modal$', views.AssetModalListView.as_view(), name='asset-modal-list'), url(r'^asset-modal$', views.AssetModalListView.as_view(), name='asset-modal-list'),
url(r'^asset-modal-update$', views.AssetModalCreateView.as_view(), name='asset-modal-update'), url(r'^asset-modal-update$', views.AssetModalCreateView.as_view(), name='asset-modal-update'),

View File

@ -8,6 +8,7 @@ from openpyxl.writer.excel import save_virtual_workbook
from openpyxl import load_workbook from openpyxl import load_workbook
from django.conf import settings from django.conf import settings
from django.db import IntegrityError from django.db import IntegrityError
from django.urls import reverse
from django.views.generic import TemplateView, ListView, View from django.views.generic import TemplateView, ListView, View
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView
from django.urls import reverse_lazy from django.urls import reverse_lazy
@ -18,6 +19,7 @@ from django.utils.decorators import method_decorator
from django.core.cache import cache from django.core.cache import cache
from django.utils import timezone from django.utils import timezone
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import get_object_or_404, redirect
from common.mixins import JSONResponseMixin from common.mixins import JSONResponseMixin
from common.utils import get_object_or_none from common.utils import get_object_or_none
@ -30,7 +32,7 @@ from ..tasks import update_assets_hardware_info
__all__ = ['AssetListView', 'AssetCreateView', 'AssetUpdateView', __all__ = ['AssetListView', 'AssetCreateView', 'AssetUpdateView',
'UserAssetListView', 'AssetModalCreateView', 'AssetDetailView', 'UserAssetListView', 'AssetModalCreateView', 'AssetDetailView',
'AssetModalListView', 'AssetDeleteView', 'AssetExportView', 'AssetModalListView', 'AssetDeleteView', 'AssetExportView',
'BulkImportAssetView', 'BulkImportAssetView', 'AssetRefreshHardwareView',
] ]
@ -325,3 +327,13 @@ class BulkImportAssetView(AdminUserRequiredMixin, JSONResponseMixin, FormView):
'msg': 'Created: {}. Updated: {}, Error: {}'.format(len(created), len(updated), len(failed)) 'msg': 'Created: {}. Updated: {}, Error: {}'.format(len(created), len(updated), len(failed))
} }
return self.render_json_response(data) return self.render_json_response(data)
class AssetRefreshHardwareView(AdminUserRequiredMixin, View):
def get(self, request, *args, **kwargs):
asset_id = kwargs.get('pk')
asset = get_object_or_404(Asset, pk=asset_id)
update_assets_hardware_info([asset])
return redirect(reverse('assets:asset-detail', kwargs={'pk': asset_id}))