mirror of https://github.com/jumpserver/jumpserver
Add asset group add Form m2m
parent
2cb6b15bc3
commit
126d7fd62c
|
@ -6,7 +6,6 @@ from django.utils.translation import gettext_lazy as _
|
|||
|
||||
|
||||
class AssetForm(forms.ModelForm):
|
||||
|
||||
class Meta:
|
||||
model = Asset
|
||||
|
||||
|
@ -22,6 +21,20 @@ class AssetForm(forms.ModelForm):
|
|||
|
||||
|
||||
class AssetGroupForm(forms.ModelForm):
|
||||
assets = forms.ModelMultipleChoiceField(queryset=Asset.objects.all())
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
if kwargs.get('instance'):
|
||||
initial = kwargs.get('initial', {})
|
||||
initial['assets'] = kwargs['instance'].assets.all()
|
||||
super(AssetGroupForm, self).__init__(*args, **kwargs)
|
||||
|
||||
def _save_m2m(self):
|
||||
super(AssetGroupForm, self)._save_m2m()
|
||||
assets = self.cleaned_data['assets']
|
||||
self.instance.assets.clear()
|
||||
self.instance.assets.add(*tuple(assets))
|
||||
|
||||
class Meta:
|
||||
model = AssetGroup
|
||||
fields = [
|
||||
|
|
|
@ -103,7 +103,7 @@ class Asset(models.Model):
|
|||
remote_card_ip = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('Remote card IP'))
|
||||
hostname = models.CharField(max_length=128, unique=True, null=True, blank=True, verbose_name=_('Hostname'))
|
||||
port = models.IntegerField(null=True, blank=True, verbose_name=_('Port'))
|
||||
groups = models.ManyToManyField(AssetGroup, null=True, blank=True, verbose_name=_('Asset groups'))
|
||||
groups = models.ManyToManyField(AssetGroup, related_name='assets', verbose_name=_('Asset groups'))
|
||||
username = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('Admin user'))
|
||||
password = models.CharField(max_length=256, null=True, blank=True, verbose_name=_("Admin password"))
|
||||
admin_user = models.ForeignKey(AdminUser, null=True, blank=True, on_delete=models.SET_NULL, verbose_name=_("Admin user"))
|
||||
|
|
|
@ -44,18 +44,8 @@
|
|||
<input type="text" placeholder="" name="port" class="form-control">
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{# <div class="form-group" id="id_type">#}
|
||||
{# <label class="col-sm-2 control-label">资产类型</label>#}
|
||||
{# <div class="col-sm-9">#}
|
||||
{# <input type="text" placeholder="" name="type" class="form-control">#}
|
||||
{# </div>#}
|
||||
{# </div>#}
|
||||
|
||||
{{ form.type|bootstrap_horizontal }}
|
||||
|
||||
{{ form.comment|bootstrap_horizontal }}
|
||||
|
||||
<div class="hr-line-dashed"></div>
|
||||
<h3>关联资产用户</h3>
|
||||
<div class="form-group">
|
||||
|
|
|
@ -30,6 +30,7 @@
|
|||
<form method="post" id="userForm" class="form-horizontal" action="" >
|
||||
{% csrf_token %}
|
||||
{{ form.name|bootstrap_horizontal }}
|
||||
{# {{ form.assets|bootstrap_horizontal }}#}
|
||||
|
||||
<div class="form-group">
|
||||
<label for="users" class="col-sm-2 control-label">{% trans 'Asset' %}</label>
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
{{ assetgroup.name }}
|
||||
</a>
|
||||
</td>
|
||||
<td class="text-center">{{ assetgroup.asset_set.count }}</td>
|
||||
<td class="text-center">{{ assetgroup.assets.count }}</td>
|
||||
<td class="text-center">{{ assetgroup.comment }}</td>
|
||||
<td class="text-center">
|
||||
<a href="{% url 'assets:assetgroup-edit' pk=assetgroup.id %}" class="btn btn-xs btn-info">{% trans 'Edit' %}</a>
|
||||
|
|
|
@ -2,17 +2,17 @@
|
|||
from __future__ import absolute_import, unicode_literals
|
||||
|
||||
from django.utils.translation import ugettext as _
|
||||
from django.shortcuts import get_object_or_404
|
||||
from django.views.generic import TemplateView, ListView
|
||||
from django.urls import reverse_lazy
|
||||
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView
|
||||
|
||||
from django.views.generic import TemplateView, ListView
|
||||
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView
|
||||
from django.urls import reverse_lazy
|
||||
from django.views.generic.detail import DetailView
|
||||
|
||||
from .models import Asset, AssetGroup, IDC, AssetExtend
|
||||
from .forms import AssetForm, AssetGroupForm
|
||||
|
||||
from .utils import AdminUserRequiredMixin
|
||||
|
||||
|
||||
|
@ -83,7 +83,19 @@ class AssetGroupDetailView(DetailView):
|
|||
|
||||
|
||||
class AssetGroupEditView(UpdateView):
|
||||
pass
|
||||
model = AssetGroup
|
||||
form_class = AssetGroupForm
|
||||
template_name = 'assets/assetgroup_add.html'
|
||||
success_url = reverse_lazy('assets:assetgroup-list')
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = {
|
||||
'app': _('Assets'),
|
||||
'action': _('Create asset group'),
|
||||
'assets': Asset.objects.all(),
|
||||
}
|
||||
kwargs.update(context)
|
||||
return super(AssetGroupEditView, self).get_context_data(**kwargs)
|
||||
|
||||
|
||||
class AssetGroupDeleteView(DeleteView):
|
||||
|
|
Loading…
Reference in New Issue