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
24e31a69
Commit
24e31a69
authored
Sep 19, 2016
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
finish asset
parent
d323c9df
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
153 additions
and
33 deletions
+153
-33
forms.py
apps/assets/forms.py
+10
-8
models.py
apps/assets/models.py
+20
-12
asset_create.html
apps/assets/templates/assets/asset_create.html
+0
-0
asset_detail.html
apps/assets/templates/assets/asset_detail.html
+0
-0
asset_list.html
apps/assets/templates/assets/asset_list.html
+4
-4
asset_update.html
apps/assets/templates/assets/asset_update.html
+82
-0
views.py
apps/assets/views.py
+37
-9
No files found.
apps/assets/forms.py
View file @
24e31a69
...
...
@@ -24,16 +24,16 @@ from django.utils.translation import gettext_lazy as _
class
AssetCreateForm
(
forms
.
ModelForm
):
tags
=
forms
.
CharField
(
label
=
_
(
'Tags'
),
widget
=
forms
.
TextInput
(
attrs
=
{
'id'
:
'tags'
}),
help_text
=
'Use `,` split'
)
required
=
False
,
help_text
=
'Use `,` split'
)
def
__init__
(
self
,
*
args
,
**
kwargs
):
instance
=
kwargs
.
get
(
'instance'
)
instance
=
kwargs
.
get
(
'instance'
,
None
)
if
instance
:
initial
=
kwargs
.
get
(
'initial'
,
{})
tags
=
Tag
.
objects
.
filter
(
asset
=
instance
)
tags_value
=
','
.
join
([
tag
.
value
for
tag
in
tags
])
initial
[
'tags'
]
=
tags_value
tags
=
instance
.
tags
.
all
(
)
initial
[
'tags'
]
=
","
.
join
([
tag
.
value
for
tag
in
tags
])
print
(
kwargs
.
get
(
'initial'
))
super
(
AssetCreateForm
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
def
_save_m2m
(
self
):
...
...
@@ -43,14 +43,16 @@ class AssetCreateForm(forms.ModelForm):
value_list
=
tags
.
split
(
','
)
self
.
instance
.
tags
.
all
()
.
delete
()
Tag
.
objects
.
bulk_create
(
[
Tag
(
value
=
value
)
for
value
in
value_list
]
[
Tag
(
value
=
value
,
asset
=
self
.
instance
)
for
value
in
value_list
]
)
class
Meta
:
model
=
Asset
fields
=
[
'hostname'
,
'ip'
,
'port'
,
'type'
,
'comment'
,
'admin_user'
,
'system_users'
,
'idc'
,
'groups'
'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'
,
]
widgets
=
{
'groups'
:
forms
.
SelectMultiple
(
attrs
=
{
'class'
:
'select2'
,
...
...
@@ -75,7 +77,7 @@ class AssetGroupForm(forms.ModelForm):
)
def
__init__
(
self
,
*
args
,
**
kwargs
):
if
kwargs
.
get
(
'instance'
):
if
kwargs
.
get
(
'instance'
,
None
):
initial
=
kwargs
.
get
(
'initial'
,
{})
initial
[
'assets'
]
=
kwargs
[
'instance'
]
.
assets
.
all
()
super
(
AssetGroupForm
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
...
...
apps/assets/models.py
View file @
24e31a69
...
...
@@ -277,7 +277,10 @@ class AssetGroup(models.Model):
def
get_default_extend
(
key
,
value
):
try
:
return
AssetExtend
.
objects
.
get_or_create
(
key
=
key
,
value
=
value
)[
0
]
except
:
return
None
def
get_default_idc
():
...
...
@@ -295,8 +298,8 @@ class Asset(models.Model):
on_delete
=
models
.
SET_NULL
,
verbose_name
=
_
(
"Admin user"
))
system_users
=
models
.
ManyToManyField
(
SystemUser
,
blank
=
True
,
related_name
=
'assets'
,
verbose_name
=
_
(
"System User"
))
idc
=
models
.
ForeignKey
(
IDC
,
null
=
True
,
related_name
=
'assets'
,
on_delete
=
models
.
SET_NULL
,
verbose_name
=
_
(
'IDC'
),
default
=
get_default_idc
)
on_delete
=
models
.
SET_NULL
,
verbose_name
=
_
(
'IDC'
),
)
#
default=get_default_idc)
mac_address
=
models
.
CharField
(
max_length
=
20
,
null
=
True
,
blank
=
True
,
verbose_name
=
_
(
"Mac address"
))
brand
=
models
.
CharField
(
max_length
=
64
,
null
=
True
,
blank
=
True
,
verbose_name
=
_
(
'Brand'
))
cpu
=
models
.
CharField
(
max_length
=
64
,
null
=
True
,
blank
=
True
,
verbose_name
=
_
(
'CPU'
))
...
...
@@ -307,14 +310,14 @@ class Asset(models.Model):
cabinet_pos
=
models
.
IntegerField
(
null
=
True
,
blank
=
True
,
verbose_name
=
_
(
'Cabinet position'
))
number
=
models
.
CharField
(
max_length
=
32
,
null
=
True
,
blank
=
True
,
verbose_name
=
_
(
'Asset number'
))
status
=
models
.
ForeignKey
(
AssetExtend
,
null
=
True
,
blank
=
True
,
related_name
=
"status_asset"
,
verbose_name
=
_
(
'Asset status'
),
default
=
functools
.
partial
(
get_default_extend
,
'status'
,
'In use'
))
related_name
=
"status_asset"
,
verbose_name
=
_
(
'Asset status'
),
)
#
default=functools.partial(get_default_extend, 'status', 'In use'))
type
=
models
.
ForeignKey
(
AssetExtend
,
null
=
True
,
limit_choices_to
=
{
'key'
:
'type'
},
related_name
=
"type_asset"
,
verbose_name
=
_
(
'Asset type'
),
default
=
functools
.
partial
(
get_default_extend
,
'type'
,
'Server'
))
env
=
models
.
ForeignKey
(
AssetExtend
,
null
=
True
,
limit_choices_to
=
{
'key'
:
'env'
},
related_name
=
"env_asset"
,
verbose_name
=
_
(
'Asset environment'
),
default
=
functools
.
partial
(
get_default_extend
,
'env'
,
'Production'
))
related_name
=
"type_asset"
,
verbose_name
=
_
(
'Asset type'
),
)
#
default=functools.partial(get_default_extend, 'type','Server'))
env
=
models
.
ForeignKey
(
AssetExtend
,
blank
=
True
,
null
=
True
,
limit_choices_to
=
{
'key'
:
'env'
},
related_name
=
"env_asset"
,
verbose_name
=
_
(
'Asset environment'
),
)
#
default=functools.partial(get_default_extend, 'env', 'Production'))
sn
=
models
.
CharField
(
max_length
=
128
,
null
=
True
,
blank
=
True
,
verbose_name
=
_
(
'Serial number'
))
created_by
=
models
.
CharField
(
max_length
=
32
,
null
=
True
,
blank
=
True
,
verbose_name
=
_
(
'Created by'
))
is_active
=
models
.
BooleanField
(
default
=
True
,
verbose_name
=
_
(
'Is active'
))
...
...
@@ -324,8 +327,13 @@ class Asset(models.Model):
def
__unicode__
(
self
):
return
'
%(ip)
s:
%(port)
s'
%
{
'ip'
:
self
.
ip
,
'port'
:
self
.
port
}
def
initial
(
self
):
pass
def
is_valid
(
self
):
warning
=
''
if
not
self
.
is_active
:
warning
+=
' inactive'
else
:
return
True
,
''
return
False
,
warning
class
Meta
:
db_table
=
'asset'
...
...
@@ -367,7 +375,7 @@ class Tag(models.Model):
unique_together
=
(
'value'
,
'asset'
)
def
init
ial
():
def
init
_all_models
():
for
cls
in
(
AssetExtend
,
AssetGroup
):
cls
.
initial
()
...
...
apps/assets/templates/assets/asset_create
_update
.html
→
apps/assets/templates/assets/asset_create.html
View file @
24e31a69
File moved
apps/assets/templates/assets/asset_detail.html
View file @
24e31a69
This diff is collapsed.
Click to expand it.
apps/assets/templates/assets/asset_list.html
View file @
24e31a69
...
...
@@ -25,7 +25,7 @@
<input
type=
"checkbox"
name=
"checked"
value=
"{{ asset.id }}"
>
</td>
<td
class=
"text-center"
>
<a
href=
"{% url '
users:user
-detail' pk=user.id %}"
>
<a
href=
"{% url '
assets:asset
-detail' pk=user.id %}"
>
{{ asset.hostname }}
</a>
</td>
...
...
@@ -34,10 +34,10 @@
<td
class=
"text-center"
>
{{ asset.type }}
</td>
<td
class=
"text-center"
>
{{ asset.cpu }} {{ asset.memory }} {{ asset.disk }}
</td>
<td
class=
"text-center"
>
{% if asset.is_expired %}
<i
class=
"fa fa-times text-danger"
></i>
{% else %}
{% if asset.is_valid.0 %}
<i
class=
"fa fa-check text-navy"
></i>
{% else %}
<i
class=
"fa fa-times text-danger"
></i>
{% endif %}
</td>
<td
class=
"text-center"
>
...
...
apps/assets/templates/assets/asset_update.html
0 → 100644
View file @
24e31a69
{% extends '_base_create_update.html' %}
{% load static %}
{% load bootstrap %}
{% load i18n %}
{% block custom_head_css_js_create %}
<link
href=
"{% static "
css
/
plugins
/
inputTags
.
css
"
%}"
rel=
"stylesheet"
>
<script
src=
"{% static "
js
/
plugins
/
inputTags
.
jquery
.
min
.
js
"
%}"
></script>
{% endblock %}
{% block form %}
<form
action=
""
method=
"post"
class=
"form-horizontal"
>
{% csrf_token %}
<h3>
{% trans 'Basic' %}
</h3>
{{ form.hostname|bootstrap_horizontal }}
{{ form.ip|bootstrap_horizontal }}
{{ form.port|bootstrap_horizontal }}
{{ form.type|bootstrap_horizontal }}
<div
class=
"hr-line-dashed"
></div>
<h3>
{% trans 'Group' %}
</h3>
{{ form.idc|bootstrap_horizontal }}
{{ form.groups|bootstrap_horizontal }}
<div
class=
"hr-line-dashed"
></div>
<h3>
{% trans 'Asset user' %}
</h3>
{{ form.admin_user|bootstrap_horizontal }}
{{ form.system_users|bootstrap_horizontal }}
<div
class=
"hr-line-dashed"
></div>
<h3>
{% trans 'Hardware' %}
</h3>
{{ form.sn|bootstrap_horizontal }}
{{ form.brand|bootstrap_horizontal }}
{{ form.cpu|bootstrap_horizontal }}
{{ form.memory|bootstrap_horizontal }}
{{ form.disk|bootstrap_horizontal }}
{{ form.mac_address|bootstrap_horizontal }}
<div
class=
"hr-line-dashed"
></div>
<h3>
{% trans 'Configuration' %}
</h3>
{{ form.number|bootstrap_horizontal }}
{{ form.other_ip|bootstrap_horizontal }}
{{ form.remote_card_ip|bootstrap_horizontal }}
{{ form.os|bootstrap_horizontal }}
<div
class=
"hr-line-dashed"
></div>
<h3>
{% trans 'Location' %}
</h3>
{{ form.cabinet_no|bootstrap_horizontal }}
{{ form.cabinet_pos|bootstrap_horizontal }}
<div
class=
"hr-line-dashed"
></div>
<h3>
{% trans 'Other' %}
</h3>
{{ form.status|bootstrap_horizontal }}
{{ form.env|bootstrap_horizontal }}
{{ form.tags|bootstrap_horizontal }}
{{ form.comment|bootstrap_horizontal }}
<div
class=
"hr-line-dashed"
></div>
<div
class=
"form-group"
>
<div
class=
"col-sm-4 col-sm-offset-2"
>
<button
class=
"btn btn-white"
type=
"reset"
>
{% trans 'Reset' %}
</button>
<button
id=
"submit_button"
class=
"btn btn-primary"
type=
"submit"
>
{% trans 'Submit' %}
</button>
</div>
</div>
</form>
{% endblock %}
{% block custom_foot_js %}
<script>
$
(
document
).
ready
(
function
()
{
$
(
'.select2'
).
select2
();
{
#
$
(
'#tags'
).
inputTags
({
#
}
{
#
tags
:
[{
%
for
tag
in
form
.
tags
.
value
%
}
{{
tag
|
safe
}},
{
%
endfor
%
}]
#
}
{
#
});
#
}
$
(
'#tags'
).
inputTags
(
);
})
</script>
{% endblock %}
\ No newline at end of file
apps/assets/views.py
View file @
24e31a69
...
...
@@ -17,7 +17,7 @@ from .forms import AssetCreateForm, AssetGroupForm, IDCForm, AdminUserForm, Syst
from
.hands
import
AdminUserRequiredMixin
class
AssetListView
(
ListView
):
class
AssetListView
(
AdminUserRequiredMixin
,
ListView
):
paginate_by
=
settings
.
CONFIG
.
DISPLAY_PER_PAGE
model
=
Asset
context_object_name
=
'asset_list'
...
...
@@ -25,9 +25,15 @@ class AssetListView(ListView):
def
get_queryset
(
self
):
queryset
=
super
(
AssetListView
,
self
)
.
get_queryset
()
queryset
=
sorted
(
queryset
,
key
=
lambda
asset
:
int_seq
(
asset
.
ip
.
split
(
'.'
))
)
queryset
=
sorted
(
queryset
,
key
=
self
.
sorted_by_valid_and_ip
)
return
queryset
@staticmethod
def
sorted_by_valid_and_ip
(
asset
):
ip_list
=
int_seq
(
asset
.
ip
.
split
(
'.'
))
ip_list
.
insert
(
0
,
asset
.
is_valid
()[
0
])
return
ip_list
def
get_context_data
(
self
,
**
kwargs
):
context
=
{
'app'
:
'Assets'
,
...
...
@@ -40,13 +46,12 @@ class AssetListView(ListView):
class
AssetCreateView
(
AdminUserRequiredMixin
,
CreateView
):
model
=
Asset
form_class
=
AssetCreateForm
template_name
=
'assets/asset_create
_update
.html'
template_name
=
'assets/asset_create.html'
success_url
=
reverse_lazy
(
'assets:asset-list'
)
# def form_valid(self, form):
# asset = form.save()
# print(self.request.POST.get('tags'))
# return super(AssetCreateView, self).form_valid(form)
def
form_invalid
(
self
,
form
):
print
(
form
.
errors
)
return
super
(
AssetCreateView
,
self
)
.
form_invalid
(
form
)
def
get_context_data
(
self
,
**
kwargs
):
context
=
{
...
...
@@ -57,8 +62,19 @@ class AssetCreateView(AdminUserRequiredMixin, CreateView):
return
super
(
AssetCreateView
,
self
)
.
get_context_data
(
**
kwargs
)
class
AssetUpdateView
(
UpdateView
):
pass
class
AssetUpdateView
(
AdminUserRequiredMixin
,
UpdateView
):
model
=
Asset
form_class
=
AssetCreateForm
template_name
=
'assets/asset_update.html'
success_url
=
reverse_lazy
(
'assets:asset-list'
)
def
get_context_data
(
self
,
**
kwargs
):
context
=
{
'app'
:
'Assets'
,
'action'
:
'Update asset'
,
}
kwargs
.
update
(
context
)
return
super
(
AssetUpdateView
,
self
)
.
get_context_data
(
**
kwargs
)
class
AssetDeleteView
(
DeleteView
):
...
...
@@ -72,6 +88,18 @@ class AssetDetailView(DetailView):
context_object_name
=
'asset'
template_name
=
'assets/asset_detail.html'
def
get_context_data
(
self
,
**
kwargs
):
asset_groups
=
self
.
object
.
groups
.
all
()
context
=
{
'app'
:
'Assets'
,
'action'
:
'Asset detail'
,
'asset_groups_remain'
:
[
asset_group
for
asset_group
in
AssetGroup
.
objects
.
all
()
if
asset_group
not
in
asset_groups
],
'asset_groups'
:
asset_groups
,
}
kwargs
.
update
(
context
)
return
super
(
AssetDetailView
,
self
)
.
get_context_data
(
**
kwargs
)
class
AssetGroupCreateView
(
AdminUserRequiredMixin
,
CreateView
):
model
=
AssetGroup
...
...
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