Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
J
jumpserver
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ops
jumpserver
Commits
2829445f
Commit
2829445f
authored
Oct 07, 2016
by
江世峰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
assets:add tag
parent
05e961f2
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
54 additions
and
16 deletions
+54
-16
forms.py
apps/assets/forms.py
+9
-11
models.py
apps/assets/models.py
+7
-4
asset_create.html
apps/assets/templates/assets/asset_create.html
+8
-1
asset_list.html
apps/assets/templates/assets/asset_list.html
+8
-0
urls.py
apps/assets/urls.py
+1
-0
views.py
apps/assets/views.py
+21
-0
No files found.
apps/assets/forms.py
View file @
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
View file @
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
View file @
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
View file @
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
View file @
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
View file @
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment