Commit 126d7fd6 authored by ibuler's avatar ibuler

Add asset group add Form m2m

parent 2cb6b15b
...@@ -6,7 +6,6 @@ from django.utils.translation import gettext_lazy as _ ...@@ -6,7 +6,6 @@ from django.utils.translation import gettext_lazy as _
class AssetForm(forms.ModelForm): class AssetForm(forms.ModelForm):
class Meta: class Meta:
model = Asset model = Asset
...@@ -22,6 +21,20 @@ class AssetForm(forms.ModelForm): ...@@ -22,6 +21,20 @@ class AssetForm(forms.ModelForm):
class AssetGroupForm(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: class Meta:
model = AssetGroup model = AssetGroup
fields = [ fields = [
......
...@@ -103,7 +103,7 @@ class Asset(models.Model): ...@@ -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')) 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')) 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')) 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')) 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")) 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")) admin_user = models.ForeignKey(AdminUser, null=True, blank=True, on_delete=models.SET_NULL, verbose_name=_("Admin user"))
......
...@@ -44,18 +44,8 @@ ...@@ -44,18 +44,8 @@
<input type="text" placeholder="" name="port" class="form-control"> <input type="text" placeholder="" name="port" class="form-control">
</div> </div>
</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.type|bootstrap_horizontal }}
{{ form.comment|bootstrap_horizontal }} {{ form.comment|bootstrap_horizontal }}
<div class="hr-line-dashed"></div> <div class="hr-line-dashed"></div>
<h3>关联资产用户</h3> <h3>关联资产用户</h3>
<div class="form-group"> <div class="form-group">
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
<form method="post" id="userForm" class="form-horizontal" action="" > <form method="post" id="userForm" class="form-horizontal" action="" >
{% csrf_token %} {% csrf_token %}
{{ form.name|bootstrap_horizontal }} {{ form.name|bootstrap_horizontal }}
{# {{ form.assets|bootstrap_horizontal }}#}
<div class="form-group"> <div class="form-group">
<label for="users" class="col-sm-2 control-label">{% trans 'Asset' %}</label> <label for="users" class="col-sm-2 control-label">{% trans 'Asset' %}</label>
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
{{ assetgroup.name }} {{ assetgroup.name }}
</a> </a>
</td> </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">{{ assetgroup.comment }}</td>
<td class="text-center"> <td class="text-center">
<a href="{% url 'assets:assetgroup-edit' pk=assetgroup.id %}" class="btn btn-xs btn-info">{% trans 'Edit' %}</a> <a href="{% url 'assets:assetgroup-edit' pk=assetgroup.id %}" class="btn btn-xs btn-info">{% trans 'Edit' %}</a>
......
...@@ -2,17 +2,17 @@ ...@@ -2,17 +2,17 @@
from __future__ import absolute_import, unicode_literals from __future__ import absolute_import, unicode_literals
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.shortcuts import get_object_or_404
from django.views.generic import TemplateView, ListView from django.views.generic import TemplateView, ListView
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView
from django.views.generic import TemplateView, ListView from django.views.generic import TemplateView, ListView
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
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
from .models import Asset, AssetGroup, IDC, AssetExtend from .models import Asset, AssetGroup, IDC, AssetExtend
from .forms import AssetForm, AssetGroupForm from .forms import AssetForm, AssetGroupForm
from .utils import AdminUserRequiredMixin from .utils import AdminUserRequiredMixin
...@@ -83,7 +83,19 @@ class AssetGroupDetailView(DetailView): ...@@ -83,7 +83,19 @@ class AssetGroupDetailView(DetailView):
class AssetGroupEditView(UpdateView): 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): class AssetGroupDeleteView(DeleteView):
......
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