Commit 73f5891f authored by 江世峰's avatar 江世峰

asset:update assets_list by tag

parent b99b88a3
......@@ -51,4 +51,3 @@ class IDCViewSet(viewsets.ReadOnlyModelViewSet):
queryset = IDC.objects.all()
serializer_class = IDCSerializer
......@@ -26,12 +26,10 @@ class AssetCreateForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
instance = kwargs.get('instance', None)
if instance:
initial = kwargs.get('initial', {})
tags = instance.tags.all()
#tags = instance.tags.all()
initial['tags'] = [t.pk for t in kwargs['instance'].tags.all()]
print(kwargs.get('initial'))
super(AssetCreateForm, self).__init__(*args, **kwargs)
def _save_m2m(self):
......
{% extends 'base.html' %}
{% load i18n %}
{% load static %}
{% load bootstrap %}
{% block custom_head_css_js %}
<link href="{% static 'css/plugins/select2/select2.min.css' %}" rel="stylesheet">
<script src="{% static 'js/plugins/select2/select2.full.min.js' %}"></script>
<style>
div.dataTables_wrapper div.dataTables_filter,
.dataTables_length {
float: left;
}
</style>
{% endblock %}
{% block content %}
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row">
<div class="col-sm-10">
<div class="ibox float-e-margins">
<div id="ibox-content" class="ibox-title">
<h5> {{ action }}</h5>
<div class="ibox-tools">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
</a>
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-wrench"></i>
</a>
<a class="close-link">
<i class="fa fa-times"></i>
</a>
</div>
</div>
<div class="ibox-content">
<div class="panel blank-panel">
<div class="panel-body">
<div class="tab-content">
<div id="tab-1" class="ibox float-e-margins tab-pane active"></div>
<form id="tagForm" method="post" class="form-horizontal">
{% csrf_token %}
<h3 class="widget-head-color-box">基本信息</h3>
{{ form.name|bootstrap_horizontal }}
<div class="form-group">
<label class="col-sm-2 control-label">关联的资产</label>
<div class="col-sm-9" id="asset_sed">
<input type="text" class=" form-control" id="add_asset" value="{{ assets_count }}">
</div>
</div>
<div class="form-group">
<div class="col-sm-4 col-sm-offset-5">
<button class="btn btn-white" type="reset"> 重置 </button>
<button class="btn btn-primary" type="submit"> 提交 </button>
<div id='box2'> </div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- 模态框(Modal) -->
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content" id="box">
<!--此部分为主体内容,将远程加载进来-->
</div>
</div>
</div>
{% endblock %}
{% block custom_foot_js %}
<script type="text/javascript">
$(document).ready(function () {
$('.select2').select2();
$('.select2-system-user').select2();
})
$('#add_asset').on('click',function(){
$('#modal').modal('show');
});
$('#modal').modal({
show: false,
backdrop: 'static',
keyboard: 'false',
remote:"{% url 'assets:asset-modal-list' %}?tag_id={{ tag_id }}",
});
$('#modal').on('show.bs.modal',function(){
//alert('当调用show方法时,立即触发;')
});
$('#modal').on('shown.bs.modal',function(){
//alert('当弹窗完全加载完后,再触发;')
});
$('#modal').on('hide.bs.modal',function(){
//alert('当关闭时,立即触发;')
});
$('#modal').on('hidden.bs.modal',function(){
//alert('当关完全关闭后,再触发;')
});
$('#modal').on('loaded.bs.modal',function(){
//alert('当远程数据加载完毕后,再触发;')
});
</script>
{% endblock %}
\ No newline at end of file
This diff is collapsed.
{% extends '_base_list.html' %}
{% load i18n %}
{% load common_tags %}
{% block content_left_head %}
<a href="{% url 'assets:asset-tag-create' %}" class="btn btn-sm btn-primary "> {% trans "Create tag" %} </a>
{% endblock %}
{% block table_head %}
<th class="text-center">
<input type="checkbox" id="check_all" onclick="checkAll('check_all', 'checked')">
</th>
<th class="text-center"><a href="{% url 'assets:asset-tag-list' %}?sort=name">{% trans 'Tag Name' %}</a></th>
<th class="text-center">{% trans 'Asset num' %}</th>
<th class="text-center"></th>
{% endblock %}
{% block table_body %}
{% for asset_tag in asset_tags_list %}
<tr class="gradeX">
<td class="text-center">
<input type="checkbox" name="checked" value="{{ asset_tag.id }}">
</td>
<td class="text-center">
<a href="{% url 'assets:asset-tag-detail' pk=asset_tag.id %}">
{{ asset_tag.name }}
</a>
</td>
<td class="text-center">{{ asset_tag.asset_set.count }}</td>
<td class="text-center">
<a href="{% url 'assets:asset-tag-update' pk=asset_tag.id %}" class="btn btn-xs btn-info">{% trans 'Update' %}</a>
<a onclick="objectDelete(this,'{{ asset_tag.name }}','{% url 'assets:asset-tag-delete' asset_tag.id %}')" class="btn btn-xs btn-danger del">{% trans 'Delete' %}</a>
</td>
</tr>
{% endfor %}
{% endblock %}
{% block content_bottom_left %}
<form id="" method="get" action="" class=" mail-search">
<div class="input-group">
<select class="form-control m-b" style="width: auto">
<option>{% trans 'Delete selected' %}</option>
<option>{% trans 'Update selected' %}</option>
<option>{% trans 'Deactive selected' %}</option>
<option>{% trans 'Export selected' %}</option>
</select>
<div class="input-group-btn pull-left" style="padding-left: 5px;">
<button id='search_btn' type="submit" style="height: 32px;" class="btn btn-sm btn-primary">
{% trans 'Submit' %}
</button>
</div>
</div>
</form>
{% endblock %}
from django import template
from django.utils import timezone
from django.conf import settings
register = template.Library()
# ~*~ coding: utf-8 ~*~
#
from rest_framework import serializers
from models import Asset
from models import Tag
from django.views.generic.edit import CreateView
class CreateAssetTagsMiXin(CreateView):
def get_form_kwargs(self):
tags_list = self.request.POST.getlist('tags')
kwargs = {
'initial': self.get_initial(),
'prefix': self.get_prefix(),
}
if self.request.method in ('POST', 'PUT'):
post_data = self.request.POST.copy()
if post_data.has_key('tags'):
post_data.pop('tags')
for t in tags_list:
try:
oTag = Tag.objects.get(pk=int(t))
except (Tag.DoesNotExist,UnicodeEncodeError):
oTag = Tag(name=t,created_by=self.request.user.username or 'Admin')
oTag.save()
post_data.update({'tags':oTag.pk})
else:
post_data.update({'tags':int(t)})
kwargs.update({
'data': post_data,
'files': self.request.FILES,
})
return kwargs
class UpdateAssetTagsMiXin(CreateAssetTagsMiXin):
def get_form_kwargs(self):
kwargs = super(UpdateAssetTagsMiXin, self).get_form_kwargs()
if hasattr(self, 'object'):
kwargs.update({'instance': self.object})
return kwargs
\ No newline at end of file
......@@ -12,6 +12,7 @@ from django.views.generic.detail import DetailView, SingleObjectMixin
from django.shortcuts import get_object_or_404, reverse, redirect
from common.utils import int_seq
from .utils import CreateAssetTagsMiXin,UpdateAssetTagsMiXin
from .models import Asset, AssetGroup, IDC, AssetExtend, AdminUser, SystemUser, Tag
from .forms import *
from .hands import AdminUserRequiredMixin
......@@ -37,7 +38,7 @@ class AssetListView(AdminUserRequiredMixin, ListView):
def get_context_data(self, **kwargs):
context = {
'app': 'Assets',
'action': 'Asset list',
'action': 'asset list',
'tag_list': [(i.id,i.name,i.asset_set.all().count())for i in Tag.objects.all().order_by('name')]
}
......@@ -45,24 +46,23 @@ class AssetListView(AdminUserRequiredMixin, ListView):
return super(AssetListView, self).get_context_data(**kwargs)
class AssetCreateView(AdminUserRequiredMixin, CreateView):
class AssetCreateView(AdminUserRequiredMixin,CreateAssetTagsMiXin,CreateView):
model = Asset
tag_type = 'asset'
form_class = AssetCreateForm
template_name = 'assets/asset_create.html'
success_url = reverse_lazy('assets:asset-list')
def form_invalid(self, form):
def form_valid(self, form):
asset = form.save()
asset.created_by = self.request.user.username or 'Admin'
asset.save()
return super(AssetCreateView, self).form_valid(form)
#tags = form.cleaned_data['tags']
def form_invalid(self, form):
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
return super(AssetCreateView, self).form_valid(form)
def get_context_data(self, **kwargs):
context = {
......@@ -74,7 +74,9 @@ class AssetCreateView(AdminUserRequiredMixin, CreateView):
return super(AssetCreateView, self).get_context_data(**kwargs)
class AssetUpdateView(AdminUserRequiredMixin, UpdateView):
class AssetUpdateView(AdminUserRequiredMixin,UpdateAssetTagsMiXin,UpdateView):
model = Asset
form_class = AssetCreateForm
template_name = 'assets/asset_update.html'
......@@ -90,7 +92,6 @@ class AssetUpdateView(AdminUserRequiredMixin, UpdateView):
def form_invalid(self, form):
print(form.errors)
print "jsf"
return super(AssetUpdateView, self).form_invalid(form)
......@@ -581,7 +582,10 @@ class TagView(ListView):
return asset_list
def get_context_data(self, **kwargs):
kwargs['app'] = 'Assets'
kwargs['action']='asset list'
kwargs['tag_list'] = [(i.id,i.name,i.asset_set.all().count() )for i in Tag.objects.all().order_by('name')]
kwargs['tag_id'] = self.kwargs['tag_id']
return super(TagView, self).get_context_data(**kwargs)
......
......@@ -43,3 +43,7 @@ def join_attr(seq, attr=None, sep=None):
seq = [getattr(obj, attr) for obj in seq]
print(seq)
return sep.join(seq)
@register.filter
def IntToStr(value):
return str(value)
\ No newline at end of file
......@@ -196,3 +196,22 @@ table.dataTable tbody td.selected td i.text-navy
.m-r-30 {
margin-right: 30px !important;
}
.ydxbd {
font-size: 12px;
width: 100%;
overflow: hidden;
padding-top: 15px;
margin-bottom: 15px;
display: block;
background: #f4f4f4;
padding-left: 10px;
}
.mar {
margin-left: 2px;
line-height: 15px;
}
.mar-j {
margin-left: 3px;
margin-right: 3px;
}
\ No newline at end of file
@import url("https://fonts.useso.com/css?family=Open+Sans:300,400,600,700");
@import url("https://fonts.useso.com/css?family=Roboto:400,300,500,700");
@import url("https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700");
@import url("https://fonts.googleapis.com/css?family=Roboto:400,300,500,700");
/** {*/
/*box-sizing: border-box;*/
/*}*/
......
......@@ -14,9 +14,11 @@
<div class="col-sm-12">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5> {{ action }} </h5>
<h5>
{{ action }}
</h5>
<div class="ibox-tools">
<a class="collapise-link">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
</a>
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
......
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