mirror of https://github.com/jumpserver/jumpserver
asset:add assets_bulk_update
parent
f88c149076
commit
3d4f79ca59
|
@ -29,29 +29,26 @@ class IDCSerializer(serializers.ModelSerializer):
|
|||
|
||||
class AssetGroupViewSet(viewsets.ModelViewSet):
|
||||
""" API endpoint that allows AssetGroup to be viewed or edited.
|
||||
|
||||
some other comment
|
||||
|
||||
some other comment
|
||||
"""
|
||||
queryset = AssetGroup.objects.all()
|
||||
serializer_class = AssetGroupSerializer
|
||||
|
||||
|
||||
class AssetViewSet(viewsets.ModelViewSet):
|
||||
"""
|
||||
API endpoint that allows Asset to be viewed or edited.
|
||||
"""
|
||||
"""API endpoint that allows Asset to be viewed or edited."""
|
||||
queryset = Asset.objects.all()
|
||||
serializer_class = AssetSerializer
|
||||
|
||||
|
||||
class IDCViewSet(viewsets.ReadOnlyModelViewSet):
|
||||
"""
|
||||
API endpoint that allows IDC to be viewed or edited.
|
||||
"""
|
||||
"""API endpoint that allows IDC to be viewed or edited."""
|
||||
queryset = IDC.objects.all()
|
||||
serializer_class = IDCSerializer
|
||||
|
||||
|
||||
class AssetListUpdateApi(BulkDeleteApiMixin, ListBulkCreateUpdateDestroyAPIView):
|
||||
queryset = Asset.objects.all()
|
||||
serializer_class = AssetBulkUpdateSerializer
|
||||
serializer_class = AssetBulkUpdateSerializer
|
||||
|
||||
|
||||
|
|
|
@ -45,7 +45,7 @@ class AssetCreateForm(forms.ModelForm):
|
|||
fields = [
|
||||
'hostname', 'ip', 'port', 'type', 'comment', 'admin_user', 'system_users', 'idc', 'groups',
|
||||
'other_ip', 'remote_card_ip', 'mac_address', 'brand', 'cpu', 'memory', 'disk', 'os', 'cabinet_no',
|
||||
'cabinet_pos', 'number', 'status', 'env', 'sn','tags',
|
||||
'cabinet_pos', 'number', 'status', 'env', 'sn', 'tags',
|
||||
]
|
||||
tags = forms.ModelMultipleChoiceField(queryset=Tag.objects.all())
|
||||
widgets = {
|
||||
|
|
|
@ -13,7 +13,7 @@ class AssetBulkUpdateSerializer(BulkSerializerMixin, serializers.ModelSerializer
|
|||
class Meta(object):
|
||||
model = Asset
|
||||
list_serializer_class = BulkListSerializer
|
||||
fields = ['id', 'is_active', 'hostname']
|
||||
fields = ['id', 'port', 'idc']
|
||||
|
||||
# def get_group_display(self, obj):
|
||||
# return " ".join([group.name for group in obj.groups.all()])
|
||||
|
|
|
@ -136,7 +136,7 @@ div.dataTables_wrapper div.dataTables_filter,
|
|||
show: false,
|
||||
backdrop: 'static',
|
||||
keyboard: 'false',
|
||||
remote:"{% url 'assets:asset-modal-update' %}",
|
||||
//remote:"{% url 'assets:asset-modal-update' %}",
|
||||
});
|
||||
|
||||
$('#asset_bulk_update_modal').on('shown.bs.modal',function(){
|
||||
|
|
|
@ -30,6 +30,9 @@
|
|||
<label class="control-label col-sm-2 col-lg-2 " id="asset_on_count">已选主机({{ assets_count }})</label>
|
||||
<div class="col-sm-9" id="asset_sed">
|
||||
<div class="form-asset-on" id="add_asset">
|
||||
{% for asset in assets_on_list %}
|
||||
<input type='hidden' name='assets' value='{{ asset.id }}'>
|
||||
{% endfor %}
|
||||
<p id="asset_on_p">
|
||||
{% for asset in assets_on_list %}
|
||||
<button name='asset_hostname' title='{{ asset.ip }}' type='button' class='btn btn-default btn-xs'>{{ asset.hostname }}</button>
|
||||
|
@ -42,7 +45,7 @@
|
|||
<div class="hr-line-dashed"></div>
|
||||
|
||||
<div class="ydxbd" id="formlists" style="display: block;">
|
||||
<p id="tags_p" class="mgl-5 c02">选择属性</p>
|
||||
<p id="tags_p" class="mgl-5 c02">选择需要修改属性</p>
|
||||
<div class="tagBtnList">
|
||||
<a onclick="AddAllForm(this)" class="tagBtn2 label label-primary" id="changeall">全选</a>
|
||||
<a onclick="AddForm(this,'id_port')" class="tagBtn2 label label-default" name="changebtn">端口</a>
|
||||
|
@ -65,14 +68,17 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<input name="assets_ids" type="hidden" value="111" >
|
||||
<input name="assets_ids" type="hidden" value="112" >
|
||||
<div class="hr-line-dashed"></div>
|
||||
<form action="" method="post" class="form-horizontal" id="add_form">
|
||||
<form action="/assets/asset/27/update" method="post" class="form-horizontal" id="add_form">
|
||||
{% csrf_token %}
|
||||
|
||||
<input name="ip" required="" type="hidden" value="1.0.0.0" >
|
||||
<div class="form-group" name="formbtn" id="formbtn">
|
||||
<div class="col-sm-4 col-sm-offset-2">
|
||||
<button class="btn btn-white" type="reset">{% trans 'Reset' %}</button>
|
||||
<button id="submit_button" class="btn btn-primary" type="submit">{% trans 'Submit' %}</button>
|
||||
<button id="submit_button" class="btn btn-primary" type="button" onclick="fsubmit()">{% trans 'Submit' %}</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
@ -173,5 +179,34 @@
|
|||
SetSelect2();
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
function fsubmit(){
|
||||
var assets_id = document.getElementsByName("assets");
|
||||
var oForm = document.getElementById('add_form');
|
||||
var parentElem = document.getElementById("add_form");
|
||||
var aDiv = parentElem.getElementsByClassName('form-group');
|
||||
if (assets_id.length === 0) {
|
||||
swal({
|
||||
title: "未选择需要修改的主机",
|
||||
text: "请点击选择"
|
||||
});
|
||||
}else if (aDiv.length === 1) {
|
||||
swal({
|
||||
title: "未选需要修改的属性",
|
||||
text: "请点击选择"
|
||||
});
|
||||
}else{
|
||||
var m = document.getElementsByName('assets_ids');
|
||||
alert(m.length);
|
||||
for(var i=0;i<m.length;i++){
|
||||
alert(m[0].value);
|
||||
oForm.appendChild(m[0]);
|
||||
};
|
||||
action="/assets/asset/"+assets_id[0].value+"/update";
|
||||
oForm.action=action;
|
||||
oForm.submit();
|
||||
};
|
||||
}
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
|
|
@ -18,8 +18,8 @@ class CreateAssetTagsMiXin(CreateView):
|
|||
for t in tags_list:
|
||||
try:
|
||||
oTag = Tag.objects.get(pk=int(t))
|
||||
except (Tag.DoesNotExist,UnicodeEncodeError):
|
||||
oTag = Tag(name=t,created_by=self.request.user.username or 'Admin')
|
||||
except (Tag.DoesNotExist, UnicodeEncodeError):
|
||||
oTag = Tag(name=t, created_by=self.request.user.username or 'Admin')
|
||||
oTag.save()
|
||||
post_data.update({'tags':oTag.pk})
|
||||
else:
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
# coding:utf-8
|
||||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
from django.conf import settings
|
||||
from django.db.models import Q
|
||||
|
@ -10,7 +9,6 @@ from django.urls import reverse_lazy
|
|||
from django.contrib.messages.views import SuccessMessageMixin
|
||||
from django.views.generic.detail import DetailView, SingleObjectMixin
|
||||
from django.shortcuts import get_object_or_404, reverse, redirect
|
||||
|
||||
from common.utils import int_seq
|
||||
from .utils import CreateAssetTagsMiXin,UpdateAssetTagsMiXin
|
||||
from .models import Asset, AssetGroup, IDC, AssetExtend, AdminUser, SystemUser, Tag
|
||||
|
@ -65,22 +63,22 @@ class AssetCreateView(AdminUserRequiredMixin,CreateAssetTagsMiXin,CreateView):
|
|||
print(form.errors)
|
||||
return super(AssetCreateView, self).form_invalid(form)
|
||||
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = {
|
||||
'app': 'Assets',
|
||||
'action': 'Create asset',
|
||||
}
|
||||
kwargs.update(context)
|
||||
|
||||
return super(AssetCreateView, self).get_context_data(**kwargs)
|
||||
|
||||
|
||||
class AssetModalCreateView(AdminUserRequiredMixin,CreateAssetTagsMiXin,ListView):
|
||||
model = Asset
|
||||
# tag_type = 'asset'
|
||||
form_class = AssetCreateForm
|
||||
template_name = 'assets/asset_modal_update.html'
|
||||
success_url = reverse_lazy('assets:asset-list')
|
||||
|
||||
def get_queryset(self):
|
||||
self.queryset = super(AssetModalCreateView,self).get_queryset()
|
||||
self.s = self.request.GET.get('plain_id_lists')
|
||||
|
@ -88,10 +86,10 @@ class AssetModalCreateView(AdminUserRequiredMixin,CreateAssetTagsMiXin,ListView)
|
|||
self.plain_id_lists = [int(x) for x in self.s.split(',')]
|
||||
else:
|
||||
self.plain_id_lists = [self.s]
|
||||
|
||||
return self.queryset
|
||||
def get_context_data(self, **kwargs):
|
||||
asset_on_list = Asset.objects.filter(id__in = self.plain_id_lists)
|
||||
|
||||
def get_context_data(self,**kwargs):
|
||||
asset_on_list = Asset.objects.filter(id__in=self.plain_id_lists)
|
||||
context = {
|
||||
'app': 'Assets',
|
||||
'action': 'Bulk Update asset',
|
||||
|
@ -102,11 +100,38 @@ class AssetModalCreateView(AdminUserRequiredMixin,CreateAssetTagsMiXin,ListView)
|
|||
kwargs.update(context)
|
||||
return super(AssetModalCreateView, self).get_context_data(**kwargs)
|
||||
|
||||
|
||||
class AssetUpdateView(AdminUserRequiredMixin,UpdateAssetTagsMiXin,UpdateView):
|
||||
model = Asset
|
||||
form_class = AssetCreateForm
|
||||
template_name = 'assets/asset_update.html'
|
||||
success_url = reverse_lazy('assets:asset-list')
|
||||
new_form = ''
|
||||
assets_ids = ''
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
default_keys = [
|
||||
'csrfmiddlewaretoken',
|
||||
'assets_ids',
|
||||
'ip',
|
||||
'number',
|
||||
'hostname',
|
||||
'system_users',
|
||||
'admin_user',
|
||||
]
|
||||
self.assets_ids = self.request.POST.getlist('assets_ids')
|
||||
print self.assets_ids
|
||||
self.new_form = self.request.POST.copy()
|
||||
print len(self.new_form)
|
||||
print type(self.new_form)
|
||||
for i in default_keys:
|
||||
if self.new_form.has_key(i):
|
||||
self.new_form.pop(i)
|
||||
print self.new_form.items()
|
||||
for i in self.new_form:
|
||||
print i
|
||||
|
||||
return super(AssetUpdateView, self).post(request, *args, **kwargs)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = {
|
||||
|
@ -120,6 +145,29 @@ class AssetUpdateView(AdminUserRequiredMixin,UpdateAssetTagsMiXin,UpdateView):
|
|||
print(form.errors)
|
||||
return super(AssetUpdateView, self).form_invalid(form)
|
||||
|
||||
def form_valid(self, form):
|
||||
print self.new_form.keys()
|
||||
print form.clean().keys()
|
||||
asset = form.save(commit=False)
|
||||
print "1111"
|
||||
|
||||
def prn_obj_key(obj_form):
|
||||
return obj_form.clean().keys()
|
||||
|
||||
for i in prn_obj_key(form):
|
||||
if i not in self.new_form.keys():
|
||||
print i
|
||||
|
||||
#delattr(asset, '"%s" % i')
|
||||
#del asset.i
|
||||
asset.save()
|
||||
asset.id = 27
|
||||
# asset.created_by = self.request.user.username or 'Admin'
|
||||
asset.save()
|
||||
asset.id = 28
|
||||
asset.save()
|
||||
return super(AssetUpdateView, self).form_valid(form)
|
||||
|
||||
|
||||
class AssetDeleteView(DeleteView):
|
||||
model = Asset
|
||||
|
@ -144,27 +192,27 @@ class AssetDetailView(DetailView):
|
|||
kwargs.update(context)
|
||||
return super(AssetDetailView, self).get_context_data(**kwargs)
|
||||
|
||||
|
||||
class AssetModalListView(AdminUserRequiredMixin, ListView):
|
||||
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
|
||||
model = Asset
|
||||
context_object_name = 'asset_modal_list'
|
||||
template_name = 'assets/asset_modal_list.html'
|
||||
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
group_id = self.request.GET.get('group_id')
|
||||
tag_id = self.request.GET.get('tag_id')
|
||||
plain_id_lists = self.request.GET.get('plain_id_lists')
|
||||
self.s = self.request.GET.get('plain_id_lists')
|
||||
if "," in str(self.s):
|
||||
self.plain_id_lists = [int(x) for x in self.s.split(',')]
|
||||
else:
|
||||
self.plain_id_lists = [self.s]
|
||||
print plain_id_lists
|
||||
if plain_id_lists:
|
||||
if "," in str(self.s):
|
||||
plain_id_lists = [int(x) for x in self.s.split(',')]
|
||||
else:
|
||||
plain_id_lists = [int(self.s)]
|
||||
context = {
|
||||
'all_assets':plain_id_lists
|
||||
}
|
||||
kwargs.update(context)
|
||||
if group_id:
|
||||
group = AssetGroup.objects.get(id=group_id)
|
||||
context = {
|
||||
|
@ -179,6 +227,7 @@ class AssetModalListView(AdminUserRequiredMixin, ListView):
|
|||
kwargs.update(context)
|
||||
return super(AssetModalListView, self).get_context_data(**kwargs)
|
||||
|
||||
|
||||
class AssetGroupCreateView(AdminUserRequiredMixin, CreateView):
|
||||
model = AssetGroup
|
||||
form_class = AssetGroupForm
|
||||
|
@ -197,7 +246,6 @@ class AssetGroupCreateView(AdminUserRequiredMixin, CreateView):
|
|||
kwargs.update(context)
|
||||
return super(AssetGroupCreateView, self).get_context_data(**kwargs)
|
||||
|
||||
|
||||
def form_valid(self, form):
|
||||
asset_group = form.save()
|
||||
assets_id_list = self.request.POST.getlist('assets', [])
|
||||
|
@ -207,7 +255,8 @@ class AssetGroupCreateView(AdminUserRequiredMixin, CreateView):
|
|||
asset_group.save()
|
||||
return super(AssetGroupCreateView, self).form_valid(form)
|
||||
|
||||
class AssetGroupListView(AdminUserRequiredMixin, ListView):
|
||||
|
||||
class AssetGroupListView(AdminUserRequiredMixin,ListView):
|
||||
model = AssetGroup
|
||||
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
|
||||
context_object_name = 'asset_group_list'
|
||||
|
@ -255,6 +304,7 @@ class AssetGroupDetailView(SingleObjectMixin, AdminUserRequiredMixin, ListView):
|
|||
kwargs.update(context)
|
||||
return super(AssetGroupDetailView, self).get_context_data(**kwargs)
|
||||
|
||||
|
||||
class AssetGroupUpdateView(AdminUserRequiredMixin, UpdateView):
|
||||
model = AssetGroup
|
||||
form_class = AssetGroupForm
|
||||
|
@ -334,6 +384,7 @@ class IDCCreateView(AdminUserRequiredMixin, CreateView):
|
|||
# IDC_add_success_next(user)
|
||||
return super(IDCCreateView, self).form_valid(form)
|
||||
|
||||
|
||||
class IDCUpdateView(AdminUserRequiredMixin, UpdateView):
|
||||
model = IDC
|
||||
form_class = IDCForm
|
||||
|
@ -365,7 +416,6 @@ class IDCDeleteView(AdminUserRequiredMixin, DeleteView):
|
|||
success_url = reverse_lazy('assets:idc-list')
|
||||
|
||||
|
||||
|
||||
class AdminUserListView(AdminUserRequiredMixin, ListView):
|
||||
model = AdminUser
|
||||
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
|
||||
|
@ -588,29 +638,6 @@ class SystemUserAssetView(AdminUserRequiredMixin, SingleObjectMixin, ListView):
|
|||
return super(SystemUserAssetView, self).get_context_data(**kwargs)
|
||||
|
||||
|
||||
# class SystemUserAssetGroupView(AdminUserRequiredMixin, SingleObjectMixin, ListView):
|
||||
# paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
|
||||
# template_name = 'assets/system_user_asset_group.html'
|
||||
# context_object_name = 'system_user'
|
||||
#
|
||||
# def get(self, request, *args, **kwargs):
|
||||
# self.object = self.get_object(queryset=SystemUser.objects.all())
|
||||
# return super(SystemUserAssetGroupView, self).get(request, *args, **kwargs)
|
||||
#
|
||||
# Todo: queryset default order by connectivity, need ops support
|
||||
# def get_queryset(self):
|
||||
# return self.object.asset_groups.all()
|
||||
#
|
||||
# def get_context_data(self, **kwargs):
|
||||
# context = {
|
||||
# 'app': 'assets',
|
||||
# 'action': 'System user asset group',
|
||||
# 'asset_groups': self.get_queryset(),
|
||||
# }
|
||||
# kwargs.update(context)
|
||||
# return super(SystemUserAssetGroupView, self).get_context_data(**kwargs)
|
||||
|
||||
|
||||
class TagView(ListView):
|
||||
context_object_name = 'asset_list'
|
||||
template_name = 'assets/asset_list.html'
|
||||
|
@ -643,6 +670,7 @@ class TagsListView(AdminUserRequiredMixin, ListView):
|
|||
kwargs.update(context)
|
||||
return super(TagsListView, self).get_context_data(**kwargs)
|
||||
|
||||
|
||||
class AssetTagCreateView(AdminUserRequiredMixin, CreateView):
|
||||
model = Tag
|
||||
form_class = AssetTagForm
|
||||
|
@ -670,6 +698,7 @@ class AssetTagCreateView(AdminUserRequiredMixin, CreateView):
|
|||
asset_tag.save()
|
||||
return super(AssetTagCreateView, self).form_valid(form)
|
||||
|
||||
|
||||
class AssetTagDetailView(SingleObjectMixin, AdminUserRequiredMixin, ListView):
|
||||
template_name = 'assets/asset_tag_detail.html'
|
||||
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
|
||||
|
@ -690,6 +719,7 @@ class AssetTagDetailView(SingleObjectMixin, AdminUserRequiredMixin, ListView):
|
|||
kwargs.update(context)
|
||||
return super(AssetTagDetailView, self).get_context_data(**kwargs)
|
||||
|
||||
|
||||
class AssetTagUpdateView(AdminUserRequiredMixin, UpdateView):
|
||||
model = Tag
|
||||
form_class = AssetTagForm
|
||||
|
|
Loading…
Reference in New Issue