Browse Source

asset:update tag

pull/530/head
江世峰 8 years ago
parent
commit
b99b88a30f
  1. 37
      apps/assets/forms.py
  2. 2
      apps/assets/models.py
  3. 2
      apps/assets/templates/assets/asset_create.html
  4. 2
      apps/assets/templates/assets/asset_group_create.html
  5. 5
      apps/assets/templates/assets/asset_list.html
  6. 8
      apps/assets/templates/assets/asset_modal_list.html
  7. 10
      apps/assets/templates/assets/asset_update.html
  8. 8
      apps/assets/urls.py
  9. 113
      apps/assets/views.py
  10. 2
      apps/templates/_nav.html

37
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',
}

2
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'))

2
apps/assets/templates/assets/asset_create.html

@ -25,8 +25,6 @@
<div class="hr-line-dashed"></div>
<h3>{% trans 'Other' %}</h3>
{{ form.tags|bootstrap_horizontal }}
{{ form.comment|bootstrap_horizontal }}
<div class="hr-line-dashed"></div>

2
apps/assets/templates/assets/asset_group_create.html

@ -37,7 +37,7 @@ div.dataTables_wrapper div.dataTables_filter,
<div class="panel-body">
<div class="tab-content">
<div id="tab-1" class="ibox float-e-margins tab-pane active"></div>
<form id="IDCForm" method="post" class="form-horizontal">
<form id="groupForm" method="post" class="form-horizontal">
{% csrf_token %}
<h3 class="widget-head-color-box">资产组信息</h3>
{{ form.name|bootstrap_horizontal }}

5
apps/assets/templates/assets/asset_list.html

@ -42,8 +42,7 @@
</td>
<td class="text-center">
<a href="{% url 'assets:asset-update' pk=asset.id %}" class="btn btn-xs btn-info">{% trans 'Update' %}</a>
<a href="{% url 'assets:asset-delete' pk=asset.id %}" class="btn btn-xs btn-danger del">{% trans 'Delete' %}</a>-
{# <a href="{% url '' %}">{% trans 'Delete' %}</a>#}
<a href="{% url 'assets:asset-delete' pk=asset.id %}" class="btn btn-xs btn-danger del">{% trans 'Delete' %}</a>
</td>
</tr>
{% endfor %}
@ -72,7 +71,7 @@
<a href="{% url 'assets:asset-tags' tag_id=tag.0%}">
{{ tag.1}}({{ tag.2 }})
</a>
{% endfor %}
{% endfor %}
{% endblock %}

8
apps/assets/templates/assets/asset_modal_list.html

@ -21,7 +21,7 @@
</thead>
<tbody>
{% for asset in asset_modal_list %}
{% if asset.id in group_assets %}
{% if asset.id in all_assets %}
<tr name="oAssets" class="odd selected">
<td class="text-center" ><input type="checkbox" name="checked" value="{{ asset.id }}" checked="checked" ></td>
{% else %}
@ -47,7 +47,6 @@
<script type="text/javascript">
//$(window).load(function (){
$(document).ready(function(){
var table = $('#editable').DataTable({
@ -68,7 +67,7 @@ $(document).ready(function(){
{data: "dp"}
]
});
//ID列隐藏
//ID列隐藏
table.column('1').visible(false);
$('#editable tbody').on( 'click', 'tr', function () {
@ -82,6 +81,9 @@ $(document).ready(function(){
};
});
$('#close-btn').on('click',function(){
$('#modal').modal('hide');
});
$('#save-btn').on('click',function(){
// alert( table.rows('.selected').data().length +' row(s) selected' );
var d = table.rows('.selected').data();

10
apps/assets/templates/assets/asset_update.html

@ -70,12 +70,10 @@
<script>
$(document).ready(function () {
$('.select2').select2();
{# $('#tags').inputTags({#}
{# tags: [{% for tag in form.tags.value %} {{ tag|safe }}, {% endfor %}]#}
{# });#}
$('#tags').inputTags(
);
$("#tags").select2({
tags: true,
maximumSelectionLength: 8 //最多能够选择的个数
});
})
</script>
{% endblock %}

8
apps/assets/urls.py

@ -28,7 +28,13 @@ urlpatterns = [
url(r'^asset-group/(?P<pk>[0-9]+)$', views.AssetGroupDetailView.as_view(), name='asset-group-detail'),
url(r'^asset-group/(?P<pk>[0-9]+)/update$', views.AssetGroupUpdateView.as_view(), name='asset-group-update'),
url(r'^asset-group/(?P<pk>[0-9]+)/delete$', views.AssetGroupDeleteView.as_view(), name='asset-group-delete'),
url(r'^asset-tag/(?P<tag_id>[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<tag_id>[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<pk>[0-9]+)$', views.AssetTagDetailView.as_view(), name='asset-tag-detail'),
url(r'^asset-tag/(?P<pk>[0-9]+)/update$', views.AssetTagUpdateView.as_view(), name='asset-tag-update'),
url(r'^asset-tag/(?P<pk>[0-9]+)/delete$', views.AssetTagDeleteView.as_view(), name='asset-tag-delete'),
# Resource idc url
url(r'^idc$', views.IDCListView.as_view(), name='idc-list'),

113
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')

2
apps/templates/_nav.html

@ -23,7 +23,7 @@
<li id="idc"><a href="{% url 'assets:idc-list' %}">{% trans 'IDC' %}</a></li>
<li id="admin-user"><a href="{% url 'assets:admin-user-list' %}">{% trans 'Admin user' %}</a></li>
<li id="system-user"><a href="{% url 'assets:system-user-list' %}">{% trans 'System user' %}</a></li>
<li id=""><a href="">{% trans 'Label' %}</a></li>
<li id="asset-tag"><a href="{% url 'assets:asset-tag-list' %}">{% trans 'Label' %}</a></li>
</ul>
</li>
<li id="perms">

Loading…
Cancel
Save