Commit 5a5d5bdd authored by ibuler's avatar ibuler

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

parent 992af0f1
......@@ -2,6 +2,7 @@
from rest_framework import viewsets, generics, mixins
from rest_framework.response import Response
from rest_framework.views import APIView
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 .models import AssetGroup, Asset, IDC, SystemUser, AdminUser
from . import serializers
from .tasks import update_assets_hardware_info
class AssetViewSet(IDInFilterMixin, BulkModelViewSet):
......@@ -54,29 +56,25 @@ class AssetUpdateGroupApi(generics.RetrieveUpdateAPIView):
permission_classes = (IsSuperUser,)
## update the asset group, and add or delete the asset to the group
class AssetGroupUpdateApi(generics.RetrieveUpdateAPIView):
queryset = AssetGroup.objects.all()
serializer_class = serializers.AssetGroupUpdateSerializer
permission_classes = (IsSuperUser,)
## update the asset group, and add or delete the system_user to the group
class AssetGroupUpdateSystemUserApi(generics.RetrieveUpdateAPIView):
queryset = AssetGroup.objects.all()
serializer_class = serializers.AssetGroupUpdateSystemUserSerializer
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()
serializer_class = serializers.IDCUpdateAssetsSerializer
permission_classes = (IsSuperUser,)
class IDCViewSet(IDInFilterMixin, BulkModelViewSet):
"""API endpoint that allows IDC to be viewed or edited."""
queryset = IDC.objects.all()
serializer_class = serializers.IDCSerializer
permission_classes = (IsSuperUser,)
......@@ -147,3 +145,14 @@ class SystemUserAuthInfoApi(generics.RetrieveAPIView):
}
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)
......@@ -188,7 +188,7 @@
<td>{% trans 'Rrefresh hardware' %}:</td>
<td>
<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>
</td>
</tr>
......@@ -353,18 +353,28 @@ 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 () {
$('.select2').select2()
.on('select2:select', function(evt) {
$('.select2').select2().on('select2:select', function(evt) {
var data = evt.params.data;
jumpserver.groups_selected[data.id] = data.text;
}).on('select2:unselect', function(evt) {
var data = evt.params.data;
delete jumpserver.groups_selected[data.id]
})
})
.on('click', '#is_active', function () {
}).on('click', '#is_active', function () {
var the_url = '{% url "api-assets:asset-detail" pk=asset.id %}';
var checked = $(this).prop('checked');
var body = {
......@@ -382,8 +392,7 @@ $(document).ready(function () {
}else{
$(".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) {
return false;
}
......@@ -395,8 +404,7 @@ $(document).ready(function () {
$('#opt_' + index).remove();
});
updateAssetGroups(groups)
})
.on('click', '.btn_leave_group', function() {
}).on('click', '.btn_leave_group', function() {
var $this = $(this);
var $tr = $this.closest('tr');
var $badge = $tr.find('.bdg_group');
......@@ -410,8 +418,7 @@ $(document).ready(function () {
return $(this).data('gid');
}).get();
updateAssetGroups(groups)
})
.on('click', '.btn-system-user', function () {
}).on('click', '.btn-system-user', function () {
if (Object.keys(jumpserver.groups_selected).length === 0) {
return false;
}
......@@ -424,8 +431,7 @@ $(document).ready(function () {
});
updateAssetSystem(system_users)
})
.on('click', '.btn_leave_system', function () {
}).on('click', '.btn_leave_system', function () {
var $this = $(this);
var $tr = $this.closest('tr');
var $badge = $tr.find('.bdg_group');
......@@ -447,6 +453,8 @@ $(document).ready(function () {
var the_url = '{% url "api-assets:asset-detail" pk=99991937 %}'.replace('99991937', uid);
var redirect_url = "{% url 'assets:asset-list' %}";
objectDelete($this, name, the_url, redirect_url);
}).on('click', '.btn-refresh-asset', function () {
refreshAssetHardware()
})
......
......@@ -22,6 +22,9 @@ urlpatterns = [
url(r'^v1/assets/(?P<pk>\d+)/groups/$',
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/$',
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
url(r'^v1/idc/(?P<pk>\d+)/assets/$',
api.IDCupdateAssetsApi.as_view(), name='idc-update-assets'),
api.IDCUpdateAssetsApi.as_view(), name='idc-update-assets'),
]
......
......@@ -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]+)/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]+)/refresh/$', views.AssetRefreshHardwareView.as_view(), name='asset-refresh'),
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'),
......
......@@ -8,6 +8,7 @@ from openpyxl.writer.excel import save_virtual_workbook
from openpyxl import load_workbook
from django.conf import settings
from django.db import IntegrityError
from django.urls import reverse
from django.views.generic import TemplateView, ListView, View
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView
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.utils import timezone
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import get_object_or_404, redirect
from common.mixins import JSONResponseMixin
from common.utils import get_object_or_none
......@@ -30,7 +32,7 @@ from ..tasks import update_assets_hardware_info
__all__ = ['AssetListView', 'AssetCreateView', 'AssetUpdateView',
'UserAssetListView', 'AssetModalCreateView', 'AssetDetailView',
'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))
}
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}))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment