jumpserver/jasset/views.py

213 lines
6.6 KiB
Python
Raw Normal View History

# coding:utf-8
2015-02-09 11:02:25 +00:00
2015-03-19 10:32:10 +00:00
import ast
2015-02-09 11:02:25 +00:00
from django.db.models import Q
from django.template import RequestContext
from django.shortcuts import get_object_or_404
2015-09-08 16:19:17 +00:00
from jasset.asset_api import *
2015-03-26 10:42:52 +00:00
from jumpserver.api import *
2014-12-22 09:18:51 +00:00
2015-09-08 16:19:17 +00:00
@require_role('admin')
def group_add(request):
"""
Add asset group
添加资产组
"""
header_title, path1, path2 = u'添加资产组', u'资产管理', u'添加资产组'
asset_all = Asset.objects.all()
2015-03-24 10:34:00 +00:00
if request.method == 'POST':
2015-09-08 16:19:17 +00:00
name = request.POST.get('name', '')
asset_select = request.POST.getlist('asset_select', [])
comment = request.POST.get('comment', '')
2015-09-08 16:19:17 +00:00
try:
if not name:
error = u'组名不能为空'
raise ServerError(error)
2015-09-08 16:19:17 +00:00
asset_group_test = get_object(AssetGroup, name=name)
if asset_group_test:
error = u"该组名 %s 已存在" % name
raise ServerError(error)
2015-04-14 09:25:56 +00:00
2015-09-08 16:19:17 +00:00
except ServerError:
pass
else:
db_add_group(name=name, comment=comment, asset_select=asset_select)
msg = u"主机组 %s 添加成功" % name
2015-04-14 09:25:56 +00:00
2015-09-08 16:19:17 +00:00
return my_render('jasset/group_add.html', locals(), request)
2015-04-14 09:25:56 +00:00
2015-09-08 16:19:17 +00:00
@require_role('admin')
def group_list(request):
"""
list asset group
列出资产组
"""
header_title, path1, path2 = u'查看资产组', u'资产管理', u'查看资产组'
keyword = request.GET.get('keyword', '')
gid = request.GET.get('gid')
sid = request.GET.get('sid')
asset_group_list = AssetGroup.objects.all()
2015-02-05 10:53:57 +00:00
2015-09-08 16:19:17 +00:00
if keyword:
2015-09-10 14:51:42 +00:00
asset_group_list = asset_group_list.filter(Q(name__contains=keyword) | Q(comment__contains=keyword))
2015-02-05 10:53:57 +00:00
2015-09-08 16:19:17 +00:00
asset_group_list, p, asset_groups, page_range, current_page, show_first, show_end = pages(asset_group_list, request)
return my_render('jasset/group_list.html', locals(), request)
2015-09-10 14:51:42 +00:00
@require_role('admin')
def group_del(request):
"""
del asset group
删除主机组
"""
group_ids = request.GET.get('id', '')
group_id_list = group_ids.split(',')
for group_id in group_id_list:
AssetGroup.objects.filter(id=group_id).delete()
return HttpResponse(u'删除成功')
2015-09-08 16:19:17 +00:00
@require_role('admin')
def asset_add(request):
"""
Asset add view
添加资产
"""
header_title, path1, path2 = u'添加资产', u'资产管理', u'添加资产'
2015-09-10 14:51:42 +00:00
asset_group_all = AssetGroup.objects.all()
if request.method == 'POST':
2015-09-08 16:19:17 +00:00
ip = request.POST.get('ip')
2015-09-10 14:51:42 +00:00
groups = request.POST.getlist('groups')
2015-10-05 15:48:03 +00:00
use_default = True if request.POST.getlist('use_default', []) else False
2015-09-08 16:19:17 +00:00
is_active = True if request.POST.get('is_active') else False
comment = request.POST.get('comment')
2015-10-05 15:48:03 +00:00
if not use_default:
2015-09-08 16:19:17 +00:00
username = request.POST.get('username')
password = request.POST.get('password')
2015-10-05 15:48:03 +00:00
port = request.POST.get('port')
2015-09-08 16:19:17 +00:00
password_encode = CRYPTOR.encrypt(password)
else:
username = None
2015-10-05 15:48:03 +00:00
port = None
2015-09-08 16:19:17 +00:00
password_encode = None
2015-04-07 11:15:45 +00:00
2015-09-08 16:19:17 +00:00
try:
if Asset.objects.filter(ip=str(ip)):
error = u'该IP %s 已存在!' % ip
raise ServerError(error)
2015-04-07 11:15:45 +00:00
2015-09-08 16:19:17 +00:00
except ServerError:
pass
else:
db_asset_add(
2015-10-05 15:48:03 +00:00
ip=ip, port=port, use_default=use_default, is_active=is_active, comment=comment,
2015-09-10 14:51:42 +00:00
groups=groups, username=username, password=password_encode
2015-09-08 16:19:17 +00:00
)
msg = u'主机 %s 添加成功' % ip
return my_render('jasset/asset_add.html', locals(), request)
@require_role(role='user')
def asset_list(request):
"""
list assets
列出资产表
"""
2015-03-04 15:14:07 +00:00
header_title, path1, path2 = u'查看主机', u'资产管理', u'查看主机'
2015-03-12 03:03:53 +00:00
keyword = request.GET.get('keyword', '')
2015-09-08 16:19:17 +00:00
gid = request.GET.get('gid', '') # asset group id
2015-04-18 08:58:15 +00:00
sid = request.GET.get('sid', '')
2015-09-08 16:19:17 +00:00
assets_list = Asset.objects.all().order_by('ip')
2015-04-05 14:52:37 +00:00
2015-09-08 16:19:17 +00:00
if keyword:
assets_list = assets_list.filter(Q(ip__contains=keyword) |
Q(comment__contains=keyword)).distinct().order_by('ip')
2015-09-08 16:19:17 +00:00
assets_list, p, assets, page_range, current_page, show_first, show_end = pages(assets_list, request)
return my_render('jasset/asset_list.html', locals(), request)
2015-04-16 11:03:02 +00:00
2015-09-08 16:19:17 +00:00
@require_role('admin')
def asset_del(request):
"""
del a asset
删除主机
"""
asset_id = request.GET.get('id', '')
if asset_id:
Asset.objects.filter(id=asset_id).delete()
return HttpResponse(u'删除成功')
return Http404
2015-09-08 16:19:17 +00:00
@require_role(role='super')
def asset_edit(request):
2015-04-16 11:03:02 +00:00
""" 修改主机 """
2015-09-08 16:19:17 +00:00
header_title, path1, path2 = u'修改资产', u'资产管理', u'修改资产'
2015-04-13 13:25:43 +00:00
2015-09-08 16:19:17 +00:00
asset_id = request.GET.get('id', '')
if not asset_id:
return HttpResponse('没有该主机')
asset = get_object(Asset, id=asset_id)
2015-01-11 11:27:21 +00:00
if request.method == 'POST':
2015-09-08 16:19:17 +00:00
ip = request.POST.get('ip')
groups = request.POST.getlist('groups')
2015-10-07 09:16:20 +00:00
use_default = True if request.POST.getlist('use_default', []) else False
2015-09-08 16:19:17 +00:00
is_active = True if request.POST.get('is_active') else False
comment = request.POST.get('comment')
2015-10-07 09:16:20 +00:00
if not use_default:
2015-09-08 16:19:17 +00:00
username = request.POST.get('username')
password = request.POST.get('password')
2015-10-07 09:16:20 +00:00
port = request.POST.get('port')
2015-09-08 16:19:17 +00:00
if password == asset.password:
password_encode = password
else:
password_encode = CRYPTOR.encrypt(password)
2015-04-11 11:56:53 +00:00
else:
2015-09-08 16:19:17 +00:00
username = None
password_encode = None
2015-10-07 09:16:20 +00:00
port = 22
2015-01-11 11:27:21 +00:00
2015-04-13 13:25:43 +00:00
try:
2015-09-08 16:19:17 +00:00
asset_test = get_object(Asset, ip=ip)
if asset_test and asset_id != str(asset_test.id):
error = u'该IP %s 已存在!' % ip
raise ServerError(error)
except ServerError:
2015-04-13 13:25:43 +00:00
pass
2015-01-11 11:27:21 +00:00
else:
2015-10-07 09:16:20 +00:00
db_asset_update(id=asset_id, ip=ip, port=port, use_default=use_default,
2015-09-08 16:19:17 +00:00
username=username, password=password_encode,
is_active=is_active, comment=comment)
msg = u'主机 %s 修改成功' % ip
return HttpResponseRedirect('/jasset/asset_detail/?id=%s' % asset_id)
return my_render('jasset/asset_edit.html', locals(), request)
@require_role('admin')
def asset_detail(request):
""" 主机详情 """
header_title, path1, path2 = u'主机详细信息', u'资产管理', u'主机详情'
asset_id = request.GET.get('id', '')
asset = get_object(Asset, id=asset_id)
return my_render('jasset/asset_detail.html', locals(), request)