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
95e64d78
Commit
95e64d78
authored
Apr 09, 2017
by
ibuler
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Change] 修改导出和更新逻辑
parent
5418d0b4
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
55 additions
and
29 deletions
+55
-29
forms.py
apps/assets/forms.py
+5
-5
asset.py
apps/assets/views/asset.py
+6
-2
user.py
apps/users/views/user.py
+44
-22
No files found.
apps/assets/forms.py
View file @
95e64d78
...
...
@@ -10,11 +10,6 @@ logger = get_logger(__file__)
class
AssetCreateForm
(
forms
.
ModelForm
):
def
clean_admin_user
(
self
):
if
not
self
.
cleaned_data
[
'admin_user'
]:
raise
forms
.
ValidationError
(
_
(
'Select admin user'
))
return
self
.
cleaned_data
[
'admin_user'
]
class
Meta
:
model
=
Asset
fields
=
[
...
...
@@ -33,6 +28,11 @@ class AssetCreateForm(forms.ModelForm):
'admin_user'
:
_
(
'Admin user should be exist on asset already, And have sudo ALL permission'
),
}
def
clean_admin_user
(
self
):
if
not
self
.
cleaned_data
[
'admin_user'
]:
raise
forms
.
ValidationError
(
_
(
'Select admin user'
))
return
self
.
cleaned_data
[
'admin_user'
]
class
AssetUpdateForm
(
AssetCreateForm
):
class
Meta
:
...
...
apps/assets/views/asset.py
View file @
95e64d78
...
...
@@ -85,7 +85,7 @@ class AssetCreateView(AdminUserRequiredMixin, CreateView):
return
super
(
AssetCreateView
,
self
)
.
get_context_data
(
**
kwargs
)
def
get_success_url
(
self
):
update_assets_hardware_info
.
delay
([
self
.
asset
])
update_assets_hardware_info
.
delay
([
self
.
asset
.
_to_secret_json
()
])
return
super
(
AssetCreateView
,
self
)
.
get_success_url
()
...
...
@@ -209,7 +209,6 @@ class AssetExportView(View):
def
get
(
self
,
request
,
*
args
,
**
kwargs
):
spm
=
request
.
GET
.
get
(
'spm'
,
''
)
assets_id
=
cache
.
get
(
spm
,
[
Asset
.
objects
.
first
()
.
id
])
print
(
assets_id
)
fields
=
[
field
for
field
in
Asset
.
_meta
.
fields
if
field
.
name
not
in
[
...
...
@@ -274,6 +273,7 @@ class BulkImportAssetView(AdminUserRequiredMixin, JSONResponseMixin, FormView):
return
self
.
render_json_response
(
data
)
created
,
updated
,
failed
=
[],
[],
[]
assets
=
[]
for
row
in
csv_data
[
1
:]:
if
set
(
row
)
==
{
''
}:
continue
...
...
@@ -305,6 +305,7 @@ class BulkImportAssetView(AdminUserRequiredMixin, JSONResponseMixin, FormView):
asset
=
Asset
.
objects
.
create
(
**
asset_dict
)
asset
.
groups
.
set
(
groups
)
created
.
append
(
asset_dict
[
'hostname'
])
assets
.
append
(
asset
)
except
IndexError
as
e
:
failed
.
append
(
'
%
s:
%
s'
%
(
asset_dict
[
'hostname'
],
str
(
e
)))
else
:
...
...
@@ -320,6 +321,9 @@ class BulkImportAssetView(AdminUserRequiredMixin, JSONResponseMixin, FormView):
except
Exception
as
e
:
failed
.
append
(
'
%
s:
%
s'
%
(
asset_dict
[
'hostname'
],
str
(
e
)))
if
assets
:
update_assets_hardware_info
.
delay
(
assets
)
data
=
{
'created'
:
created
,
'created_info'
:
'Created {}'
.
format
(
len
(
created
)),
...
...
apps/users/views/user.py
View file @
95e64d78
...
...
@@ -137,8 +137,12 @@ USER_ATTR_MAPPING = (
@method_decorator
(
csrf_exempt
,
name
=
'dispatch'
)
class
UserExportView
(
View
):
def
get
(
self
,
request
):
mapping
=
[
(
k
,
_
(
v
))
for
k
,
v
in
USER_ATTR_MAPPING
fields
=
[
User
.
_meta
.
get_field
(
name
)
for
name
in
[
'id'
,
'name'
,
'username'
,
'email'
,
'role'
,
'wechat'
,
'phone'
,
'enable_otp'
,
'is_active'
,
'comment'
,
]
]
spm
=
request
.
GET
.
get
(
'spm'
,
''
)
users_id
=
cache
.
get
(
spm
,
[
'1'
])
...
...
@@ -150,15 +154,15 @@ class UserExportView(View):
users
=
User
.
objects
.
filter
(
id__in
=
users_id
)
writer
=
csv
.
writer
(
response
,
dialect
=
'excel'
,
quoting
=
csv
.
QUOTE_MINIMAL
)
header
=
[
v
for
k
,
v
in
mapping
]
header
=
[
field
.
verbose_name
for
field
in
fields
]
header
.
append
(
_
(
'User groups'
))
writer
.
writerow
(
header
)
for
user
in
users
:
groups
=
','
.
join
([
group
.
name
for
group
in
user
.
groups
.
all
()])
writer
.
writerow
([
user
.
name
,
user
.
username
,
user
.
email
,
groups
,
user
.
role
,
user
.
phone
,
user
.
wechat
,
user
.
comment
])
data
=
[
getattr
(
user
,
field
.
name
)
for
field
in
fields
]
data
.
append
(
groups
)
writer
.
writerow
(
data
)
return
response
...
...
@@ -194,9 +198,18 @@ class UserBulkImportView(AdminUserRequiredMixin, JSONResponseMixin, FormView):
reader
=
csv
.
reader
(
csv_file
)
csv_data
=
[
row
for
row
in
reader
]
header_
=
csv_data
[
0
]
mapping_reverse
=
{
_
(
v
):
k
for
k
,
v
in
USER_ATTR_MAPPING
}
user_attr
=
[
mapping_reverse
.
get
(
n
,
None
)
for
n
in
header_
]
if
None
in
user_attr
:
fields
=
[
User
.
_meta
.
get_field
(
name
)
for
name
in
[
'id'
,
'name'
,
'username'
,
'email'
,
'role'
,
'wechat'
,
'phone'
,
'enable_otp'
,
'is_active'
,
'comment'
,
]
]
mapping_reverse
=
{
field
.
verbose_name
:
field
.
name
for
field
in
fields
}
mapping_reverse
[
_
(
'User groups'
)]
=
'groups'
attr
=
[
mapping_reverse
.
get
(
n
,
None
)
for
n
in
header_
]
print
(
attr
)
if
None
in
attr
:
data
=
{
'valid'
:
False
,
'msg'
:
'Must be same format as '
'template or export file'
}
...
...
@@ -206,24 +219,36 @@ class UserBulkImportView(AdminUserRequiredMixin, JSONResponseMixin, FormView):
for
row
in
csv_data
[
1
:]:
if
set
(
row
)
==
{
''
}:
continue
user_dict
=
dict
(
zip
(
user_attr
,
row
))
groups_name
=
user_dict
.
pop
(
'groups'
)
if
groups_name
:
groups_name
=
groups_name
.
split
(
','
)
groups
=
UserGroup
.
objects
.
filter
(
name__in
=
groups_name
)
else
:
groups
=
None
username
=
user_dict
[
'username'
]
user
=
get_object_or_none
(
User
,
username
=
username
)
user_dict
=
dict
(
zip
(
attr
,
row
))
id_
=
user_dict
.
pop
(
'id'
,
0
)
user
=
get_object_or_none
(
User
,
id
=
id_
)
for
k
,
v
in
user_dict
.
items
():
if
k
in
[
'enable_otp'
,
'is_active'
]:
if
v
.
lower
()
==
'false'
:
v
=
False
else
:
v
=
bool
(
v
)
elif
k
==
'groups'
:
groups_name
=
v
.
split
(
','
)
v
=
UserGroup
.
objects
.
filter
(
name__in
=
groups_name
)
else
:
continue
user_dict
[
k
]
=
v
if
not
user
:
try
:
groups
=
user_dict
.
pop
(
'groups'
)
user
=
User
.
objects
.
create
(
**
user_dict
)
user
.
groups
.
set
(
groups
)
created
.
append
(
user_dict
[
'username'
])
user_add_success_next
(
user
)
except
Exception
as
e
:
failed
.
append
(
'
%
s:
%
s'
%
(
user_dict
[
'username'
],
str
(
e
)))
else
:
for
k
,
v
in
user_dict
.
items
():
if
k
==
'groups'
:
user
.
groups
.
set
(
v
)
continue
if
v
:
setattr
(
user
,
k
,
v
)
try
:
...
...
@@ -231,9 +256,6 @@ class UserBulkImportView(AdminUserRequiredMixin, JSONResponseMixin, FormView):
updated
.
append
(
user_dict
[
'username'
])
except
Exception
as
e
:
failed
.
append
(
'
%
s:
%
s'
%
(
user_dict
[
'username'
],
str
(
e
)))
if
user
and
groups
:
user
.
groups
=
groups
user
.
save
()
data
=
{
'created'
:
created
,
...
...
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