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 @@

{% trans 'Other' %}

{{ form.tags|bootstrap_horizontal }} - - {{ form.comment|bootstrap_horizontal }}
diff --git a/apps/assets/templates/assets/asset_group_create.html b/apps/assets/templates/assets/asset_group_create.html index 2d22e997e..14674c33e 100644 --- a/apps/assets/templates/assets/asset_group_create.html +++ b/apps/assets/templates/assets/asset_group_create.html @@ -37,7 +37,7 @@ div.dataTables_wrapper div.dataTables_filter,
-
+ {% csrf_token %}

资产组信息

{{ form.name|bootstrap_horizontal }} diff --git a/apps/assets/templates/assets/asset_list.html b/apps/assets/templates/assets/asset_list.html index eccebe5da..90ec813cc 100644 --- a/apps/assets/templates/assets/asset_list.html +++ b/apps/assets/templates/assets/asset_list.html @@ -42,8 +42,7 @@ {% trans 'Update' %} - {% trans 'Delete' %}- -{# {% trans 'Delete' %}#} + {% trans 'Delete' %} {% endfor %} @@ -72,7 +71,7 @@ {{ tag.1}}({{ tag.2 }}) -{% endfor %} + {% endfor %} {% endblock %} diff --git a/apps/assets/templates/assets/asset_modal_list.html b/apps/assets/templates/assets/asset_modal_list.html index 228152626..b41beecb0 100644 --- a/apps/assets/templates/assets/asset_modal_list.html +++ b/apps/assets/templates/assets/asset_modal_list.html @@ -21,7 +21,7 @@ {% for asset in asset_modal_list %} - {% if asset.id in group_assets %} + {% if asset.id in all_assets %} {% else %} @@ -47,7 +47,6 @@ {% endblock %} \ No newline at end of file diff --git a/apps/assets/urls.py b/apps/assets/urls.py index 5aa6b45f8..ca6493cf7 100644 --- a/apps/assets/urls.py +++ b/apps/assets/urls.py @@ -28,7 +28,13 @@ urlpatterns = [ url(r'^asset-group/(?P[0-9]+)$', views.AssetGroupDetailView.as_view(), name='asset-group-detail'), url(r'^asset-group/(?P[0-9]+)/update$', views.AssetGroupUpdateView.as_view(), name='asset-group-update'), url(r'^asset-group/(?P[0-9]+)/delete$', views.AssetGroupDeleteView.as_view(), name='asset-group-delete'), - url(r'^asset-tag/(?P[0-9]+)$', views.TagView.as_view(), name='asset-tags'), + + url(r'^tags$', views.TagsListView.as_view(), name='asset-tag-list'), + url(r'^asset-by-tag/(?P[0-9]+)$', views.TagView.as_view(), name='asset-tags'), + url(r'^tags/create$', views.AssetTagCreateView.as_view(), name='asset-tag-create'), + url(r'^asset-tag/(?P[0-9]+)$', views.AssetTagDetailView.as_view(), name='asset-tag-detail'), + url(r'^asset-tag/(?P[0-9]+)/update$', views.AssetTagUpdateView.as_view(), name='asset-tag-update'), + url(r'^asset-tag/(?P[0-9]+)/delete$', views.AssetTagDeleteView.as_view(), name='asset-tag-delete'), # Resource idc url url(r'^idc$', views.IDCListView.as_view(), name='idc-list'), diff --git a/apps/assets/views.py b/apps/assets/views.py index 3417eabc5..357459ced 100644 --- a/apps/assets/views.py +++ b/apps/assets/views.py @@ -13,7 +13,7 @@ from django.shortcuts import get_object_or_404, reverse, redirect from common.utils import int_seq from .models import Asset, AssetGroup, IDC, AssetExtend, AdminUser, SystemUser, Tag -from .forms import AssetCreateForm, AssetGroupForm, IDCForm, AdminUserForm, SystemUserForm +from .forms import * from .hands import AdminUserRequiredMixin @@ -53,12 +53,14 @@ class AssetCreateView(AdminUserRequiredMixin, CreateView): success_url = reverse_lazy('assets:asset-list') def form_invalid(self, form): + + #tags = form.cleaned_data['tags'] print(form.errors) return super(AssetCreateView, self).form_invalid(form) def form_valid(self, form): tag_name_list = form.cleaned_data['tags'] - print self.tag_type,tag_name_list + # print self.tag_type,tag_name_list return super(AssetCreateView, self).form_valid(form) @@ -86,6 +88,11 @@ class AssetUpdateView(AdminUserRequiredMixin, UpdateView): kwargs.update(context) return super(AssetUpdateView, self).get_context_data(**kwargs) + def form_invalid(self, form): + print(form.errors) + print "jsf" + return super(AssetUpdateView, self).form_invalid(form) + class AssetDeleteView(DeleteView): model = Asset @@ -119,10 +126,17 @@ class AssetModalListView(AdminUserRequiredMixin, ListView): def get_context_data(self, **kwargs): group_id = self.request.GET.get('group_id') + tag_id = self.request.GET.get('tag_id') if group_id: group = AssetGroup.objects.get(id=group_id) context = { - 'group_assets':[x.id for x in group.assets.all()] + 'all_assets':[x.id for x in group.assets.all()] + } + kwargs.update(context) + if tag_id: + tag = Tag.objects.get(id=tag_id) + context = { + 'all_assets':[x.id for x in tag.asset_set.all()] } kwargs.update(context) return super(AssetModalListView, self).get_context_data(**kwargs) @@ -150,7 +164,6 @@ class AssetGroupCreateView(AdminUserRequiredMixin, CreateView): asset_group = form.save() assets_id_list = self.request.POST.getlist('assets', []) assets = [get_object_or_404(Asset, id=int(asset_id)) for asset_id in assets_id_list] - print assets asset_group.created_by = self.request.user.username or 'Admin' asset_group.assets.add(*tuple(assets)) asset_group.save() @@ -161,6 +174,7 @@ class AssetGroupListView(AdminUserRequiredMixin, ListView): paginate_by = settings.CONFIG.DISPLAY_PER_PAGE context_object_name = 'asset_group_list' template_name = 'assets/asset_group_list.html' + ordering = '-id' def get_context_data(self, **kwargs): context = { @@ -569,3 +583,94 @@ class TagView(ListView): def get_context_data(self, **kwargs): kwargs['tag_list'] = [(i.id,i.name,i.asset_set.all().count() )for i in Tag.objects.all().order_by('name')] return super(TagView, self).get_context_data(**kwargs) + + +class TagsListView(AdminUserRequiredMixin, ListView): + model = Tag + paginate_by = settings.CONFIG.DISPLAY_PER_PAGE + context_object_name = 'asset_tags_list' + template_name = 'assets/asset_tags_list.html' + ordering = '-id' + + def get_context_data(self, **kwargs): + context = { + 'app': _('Tag'), + 'action': _('Asset Tags list'), + 'keyword': self.request.GET.get('keyword', '') + } + kwargs.update(context) + return super(TagsListView, self).get_context_data(**kwargs) + +class AssetTagCreateView(AdminUserRequiredMixin, CreateView): + model = Tag + form_class = AssetTagForm + template_name = 'assets/asset_tag_create.html' + success_url = reverse_lazy('assets:asset-tag-list') + #ordering = '-id' + + # Todo: Asset group create template select assets so hard, need be resolve next + + def get_context_data(self, **kwargs): + context = { + 'app': _('Tag'), + 'action': _('Asset Tags list'), + 'assets_count': 0, + } + kwargs.update(context) + return super(AssetTagCreateView, self).get_context_data(**kwargs) + + def form_valid(self, form): + asset_tag = form.save() + assets_id_list = self.request.POST.getlist('assets', []) + assets = [get_object_or_404(Asset, id=int(asset_id)) for asset_id in assets_id_list] + asset_tag.created_by = self.request.user.username or 'Admin' + asset_tag.asset_set.add(*tuple(assets)) + 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 + + def get(self, request, *args, **kwargs): + self.object = self.get_object(queryset=Tag.objects.all()) + return super(AssetTagDetailView, self).get(request, *args, **kwargs) + + def get_queryset(self): + return self.object.asset_set.all() + + def get_context_data(self, **kwargs): + context = { + 'app': _('Tag'), + 'action': _('Asset Tags detail'), + 'asset_tag': self.object, + } + kwargs.update(context) + return super(AssetTagDetailView, self).get_context_data(**kwargs) + +class AssetTagUpdateView(AdminUserRequiredMixin, UpdateView): + model = Tag + form_class = AssetTagForm + template_name = 'assets/asset_tag_create.html' + success_url = reverse_lazy('assets:asset-tag-list') + + def get(self, request, *args, **kwargs): + self.object = self.get_object(queryset=Tag.objects.all()) + return super(AssetTagUpdateView, self).get(request, *args, **kwargs) + + def get_context_data(self, **kwargs): + context = { + 'app': _('Tag'), + 'action': _('Asset Tags detail'), + 'assets_count': self.object.asset_set.all().count(), + 'tag_id':self.object.id, + } + kwargs.update(context) + return super(AssetTagUpdateView, self).get_context_data(**kwargs) + + +class AssetTagDeleteView(AdminUserRequiredMixin, DeleteView): + template_name = 'assets/delete_confirm.html' + model = Tag + success_url = reverse_lazy('assets:asset-tag-list') + diff --git a/apps/templates/_nav.html b/apps/templates/_nav.html index b8ee3e775..a0b9e82f8 100644 --- a/apps/templates/_nav.html +++ b/apps/templates/_nav.html @@ -23,7 +23,7 @@
  • {% trans 'IDC' %}
  • {% trans 'Admin user' %}
  • {% trans 'System user' %}
  • -
  • {% trans 'Label' %}
  • +
  • {% trans 'Label' %}