Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
gjl2004yn
jumpserver
提交
2829445f
J
jumpserver
项目概览
gjl2004yn
/
jumpserver
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
jumpserver
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2829445f
编写于
10月 07, 2016
作者:
江
江世峰
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
assets:add tag
上级
05e961f2
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
53 addition
and
16 deletion
+53
-16
apps/assets/forms.py
apps/assets/forms.py
+9
-11
apps/assets/models.py
apps/assets/models.py
+7
-4
apps/assets/templates/assets/asset_create.html
apps/assets/templates/assets/asset_create.html
+7
-1
apps/assets/templates/assets/asset_list.html
apps/assets/templates/assets/asset_list.html
+8
-0
apps/assets/urls.py
apps/assets/urls.py
+1
-0
apps/assets/views.py
apps/assets/views.py
+21
-0
未找到文件。
apps/assets/forms.py
浏览文件 @
2829445f
...
...
@@ -23,8 +23,6 @@ from django.utils.translation import gettext_lazy as _
#
class
AssetCreateForm
(
forms
.
ModelForm
):
tags
=
forms
.
CharField
(
label
=
_
(
'Tags'
),
widget
=
forms
.
TextInput
(
attrs
=
{
'id'
:
'tags'
}),
required
=
False
,
help_text
=
'Use `,` split'
)
def
__init__
(
self
,
*
args
,
**
kwargs
):
instance
=
kwargs
.
get
(
'instance'
,
None
)
...
...
@@ -32,19 +30,15 @@ class AssetCreateForm(forms.ModelForm):
if
instance
:
initial
=
kwargs
.
get
(
'initial'
,
{})
tags
=
instance
.
tags
.
all
()
initial
[
'tags'
]
=
","
.
join
([
tag
.
value
for
tag
in
tags
])
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
):
super
(
AssetCreateForm
,
self
).
_save_m2m
()
tags
=
self
.
cleaned_data
[
'tags'
]
if
tags
:
value_list
=
tags
.
split
(
','
)
self
.
instance
.
tags
.
all
().
delete
()
Tag
.
objects
.
bulk_create
(
[
Tag
(
value
=
value
,
asset
=
self
.
instance
)
for
value
in
value_list
]
)
self
.
instance
.
tags
.
clear
()
self
.
instance
.
tags
.
add
(
*
tuple
(
tags
))
class
Meta
:
model
=
Asset
...
...
@@ -52,11 +46,14 @@ class AssetCreateForm(forms.ModelForm):
fields
=
[
'hostname'
,
'ip'
,
'port'
,
'type'
,
'comment'
,
'admin_user'
,
'system_users'
,
'idc'
,
'groups'
,
'other_ip'
,
'remote_card_ip'
,
'mac_address'
,
'brand'
,
'cpu'
,
'memory'
,
'disk'
,
'os'
,
'cabinet_no'
,
'cabinet_pos'
,
'number'
,
'status'
,
'env'
,
'sn'
,
'cabinet_pos'
,
'number'
,
'status'
,
'env'
,
'sn'
,
'tags'
,
]
tags
=
forms
.
ModelMultipleChoiceField
(
queryset
=
Tag
.
objects
.
all
())
widgets
=
{
'groups'
:
forms
.
SelectMultiple
(
attrs
=
{
'class'
:
'select2'
,
'data-placeholder'
:
_
(
'Select asset groups'
)}),
'tags'
:
forms
.
SelectMultiple
(
attrs
=
{
'class'
:
'select2'
,
'data-placeholder'
:
_
(
'Select asset groups'
)}),
'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'
)}),
...
...
@@ -64,6 +61,7 @@ class AssetCreateForm(forms.ModelForm):
help_texts
=
{
'hostname'
:
'* required'
,
'ip'
:
'* required'
,
'tags'
:
'最多5个标签,单个标签最长8个汉字,按回车确认'
}
...
...
apps/assets/models.py
浏览文件 @
2829445f
...
...
@@ -323,6 +323,7 @@ class Asset(models.Model):
is_active
=
models
.
BooleanField
(
default
=
True
,
verbose_name
=
_
(
'Is active'
))
date_created
=
models
.
DateTimeField
(
auto_now
=
True
,
null
=
True
,
blank
=
True
,
verbose_name
=
_
(
'Date added'
))
comment
=
models
.
TextField
(
max_length
=
128
,
null
=
True
,
blank
=
True
,
verbose_name
=
_
(
'Comment'
))
tags
=
models
.
ManyToManyField
(
'Tag'
,
verbose_name
=
'标签集合'
,
blank
=
True
)
def
__unicode__
(
self
):
return
'%(ip)s:%(port)s'
%
{
'ip'
:
self
.
ip
,
'port'
:
self
.
port
}
...
...
@@ -364,15 +365,17 @@ class Asset(models.Model):
class
Tag
(
models
.
Model
):
value
=
models
.
CharField
(
max_length
=
64
,
verbose_name
=
_
(
'VALUE'
))
asset
=
models
.
ForeignKey
(
Asset
,
related_name
=
'tags'
,
on_delete
=
models
.
CASCADE
,
verbose_name
=
_
(
'Asset'
))
name
=
models
.
CharField
(
'标签名'
,
max_length
=
64
)
created_time
=
models
.
DateTimeField
(
'创建时间'
,
auto_now_add
=
True
)
created_by
=
models
.
CharField
(
max_length
=
32
,
null
=
True
,
blank
=
True
,
verbose_name
=
_
(
'Created by'
))
def
__str__
(
self
):
return
self
.
name
def
__unicode__
(
self
):
return
self
.
valu
e
return
self
.
nam
e
class
Meta
:
db_table
=
'tag'
unique_together
=
(
'value'
,
'asset'
)
def
init_all_models
():
...
...
apps/assets/templates/assets/asset_create.html
浏览文件 @
2829445f
...
...
@@ -25,6 +25,8 @@
<div
class=
"hr-line-dashed"
></div>
<h3>
{% trans 'Other' %}
</h3>
{{ form.tags|bootstrap_horizontal }}
{{ form.comment|bootstrap_horizontal }}
<div
class=
"hr-line-dashed"
></div>
...
...
@@ -42,7 +44,11 @@
<script>
$
(
document
).
ready
(
function
()
{
$
(
'
.select2
'
).
select2
();
$
(
'
#tags
'
).
inputTags
();
$
(
"
#tags
"
).
select2
({
tags
:
true
,
maximumSelectionLength
:
8
//最多能够选择的个数
});
})
</script>
{% endblock %}
\ No newline at end of file
apps/assets/templates/assets/asset_list.html
浏览文件 @
2829445f
...
...
@@ -47,6 +47,7 @@
</td>
</tr>
{% endfor %}
{% endblock %}
{% block content_bottom_left %}
...
...
@@ -67,5 +68,12 @@
</div>
</form>
{% for tag in tag_list %}
<a
href=
"{% url 'assets:asset-tags' tag_id=tag.0%}"
>
{{ tag.1}}({{ tag.2 }})
</a>
{% endfor %}
{% endblock %}
apps/assets/urls.py
浏览文件 @
2829445f
...
...
@@ -28,6 +28,7 @@ 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'
),
# Resource idc url
url
(
r
'^idc$'
,
views
.
IDCListView
.
as_view
(),
name
=
'idc-list'
),
...
...
apps/assets/views.py
浏览文件 @
2829445f
...
...
@@ -38,6 +38,8 @@ class AssetListView(AdminUserRequiredMixin, ListView):
context
=
{
'app'
:
'Assets'
,
'action'
:
'Asset list'
,
'tag_list'
:
[(
i
.
id
,
i
.
name
,
i
.
asset_set
.
all
().
count
())
for
i
in
Tag
.
objects
.
all
().
order_by
(
'name'
)]
}
kwargs
.
update
(
context
)
return
super
(
AssetListView
,
self
).
get_context_data
(
**
kwargs
)
...
...
@@ -45,6 +47,7 @@ class AssetListView(AdminUserRequiredMixin, ListView):
class
AssetCreateView
(
AdminUserRequiredMixin
,
CreateView
):
model
=
Asset
tag_type
=
'asset'
form_class
=
AssetCreateForm
template_name
=
'assets/asset_create.html'
success_url
=
reverse_lazy
(
'assets:asset-list'
)
...
...
@@ -53,12 +56,19 @@ class AssetCreateView(AdminUserRequiredMixin, CreateView):
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
=
{
'app'
:
'Assets'
,
'action'
:
'Create asset'
,
}
kwargs
.
update
(
context
)
return
super
(
AssetCreateView
,
self
).
get_context_data
(
**
kwargs
)
...
...
@@ -122,6 +132,7 @@ class AssetGroupCreateView(AdminUserRequiredMixin, CreateView):
form_class
=
AssetGroupForm
template_name
=
'assets/asset_group_create.html'
success_url
=
reverse_lazy
(
'assets:asset-group-list'
)
#ordering = '-id'
# Todo: Asset group create template select assets so hard, need be resolve next
...
...
@@ -547,4 +558,14 @@ class SystemUserAssetView(AdminUserRequiredMixin, SingleObjectMixin, ListView):
# return super(SystemUserAssetGroupView, self).get_context_data(**kwargs)
class
TagView
(
ListView
):
context_object_name
=
'asset_list'
template_name
=
'assets/asset_list.html'
def
get_queryset
(
self
):
asset_list
=
Asset
.
objects
.
filter
(
tags
=
self
.
kwargs
[
'tag_id'
])
return
asset_list
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
)
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录