From b99b88a30f66bc723583407cb65eb2d7dd874aeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=9F=E4=B8=96=E5=B3=B0?= <93648765@qq.com> Date: Sat, 8 Oct 2016 18:33:00 +0800 Subject: [PATCH] asset:update tag --- apps/assets/forms.py | 37 +++++- apps/assets/models.py | 2 +- .../assets/templates/assets/asset_create.html | 2 - .../templates/assets/asset_group_create.html | 2 +- apps/assets/templates/assets/asset_list.html | 5 +- .../templates/assets/asset_modal_list.html | 8 +- .../assets/templates/assets/asset_update.html | 10 +- apps/assets/urls.py | 8 +- apps/assets/views.py | 113 +++++++++++++++++- apps/templates/_nav.html | 2 +- 10 files changed, 166 insertions(+), 23 deletions(-) diff --git a/apps/assets/forms.py b/apps/assets/forms.py index 1d8990ef2..7273075f2 100644 --- a/apps/assets/forms.py +++ b/apps/assets/forms.py @@ -40,6 +40,7 @@ class AssetCreateForm(forms.ModelForm): self.instance.tags.clear() self.instance.tags.add(*tuple(tags)) + class Meta: model = Asset @@ -53,7 +54,8 @@ class AssetCreateForm(forms.ModelForm): 'groups': forms.SelectMultiple(attrs={'class': 'select2', 'data-placeholder': _('Select asset groups')}), 'tags': forms.SelectMultiple(attrs={'class': 'select2', - 'data-placeholder': _('Select asset groups')}), + 'id':'tags', + 'data-placeholder': _('Select asset tags')}), 'system_users': forms.SelectMultiple(attrs={'class': 'select2', 'data-placeholder': _('Select asset system users')}), 'admin_user': forms.Select(attrs={'class': 'select2', 'data-placeholder': _('Select asset admin user')}), @@ -263,4 +265,37 @@ class SystemUserForm(forms.ModelForm): 'username': '* required', 'auth_push': 'Auto push system user to asset', 'auth_update': 'Auto update system user ssh key', + } + +class AssetTagForm(forms.ModelForm): + assets = forms.ModelMultipleChoiceField(queryset=Asset.objects.all(), + label=_('Asset'), + required=False, + widget=forms.SelectMultiple( + attrs={'class': 'select2', 'data-placeholder': _('Select assets')}) + ) + + def __init__(self, *args, **kwargs): + if kwargs.get('instance', None): + initial = kwargs.get('initial', {}) + initial['assets'] = kwargs['instance'].asset_set.all() + super(AssetTagForm, self).__init__(*args, **kwargs) + + def _save_m2m(self): + super(AssetTagForm, self)._save_m2m() + assets = self.cleaned_data['assets'] + self.instance.asset_set.clear() + self.instance.asset_set.add(*tuple(assets)) + + class Meta: + model = Tag + fields = [ + "name", + ] + widgets = { + 'name' : forms.TextInput(attrs={}), + + } + help_texts = { + 'name': '* required', } \ No newline at end of file diff --git a/apps/assets/models.py b/apps/assets/models.py index 84b934184..b4a40fe64 100644 --- a/apps/assets/models.py +++ b/apps/assets/models.py @@ -365,7 +365,7 @@ class Asset(models.Model): class Tag(models.Model): - name = models.CharField('标签名', max_length=64) + name = models.CharField('标签名', max_length=64,unique=True) created_time = models.DateTimeField('创建时间', auto_now_add=True) created_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Created by')) diff --git a/apps/assets/templates/assets/asset_create.html b/apps/assets/templates/assets/asset_create.html index 3bfb0453f..ad8182330 100644 --- a/apps/assets/templates/assets/asset_create.html +++ b/apps/assets/templates/assets/asset_create.html @@ -25,8 +25,6 @@