diff --git a/jasset/urls.py b/jasset/urls.py index e60b47f71..280ed090c 100644 --- a/jasset/urls.py +++ b/jasset/urls.py @@ -4,11 +4,16 @@ from jasset.views import * urlpatterns = patterns('', url(r'^$', index), - url(r'host_add/$', jadd_host), - url(r'host_list/$', jlist_host), - url(r"(\d+.\d+.\d+.\d+)/$",jlist_ip), - url(r'idc_add/$', jadd_idc), - url(r'idc_list/$', jlist_idc), - url(r'group_add/$', jadd_group), - url(r'group_list/$', jlist_group), + url(r'^host_add/$', jadd_host), + url(r'^host_list/$', jlist_host), + url(r"^(\d+.\d+.\d+.\d+)/$",jlist_ip), + url(r'^idc_add/$', jadd_idc), + url(r'^idc_list/$', jlist_idc), + url(r'^idc_del/(\d+)/$', idc_del), + url(r'^group_add/$', jadd_group), + url(r'^group_list/$', jlist_group), + url(r'^group_del/(\d+)/$', group_del), + url(r'^host_del/(\d+.\d+.\d+.\d+)/$', host_del), + url(r'^host_edit/(\d+.\d+.\d+.\d+)/$', host_edit), + url(r'^test/$', test), ) \ No newline at end of file diff --git a/jasset/views.py b/jasset/views.py index 8472d3d3f..02f887b69 100644 --- a/jasset/views.py +++ b/jasset/views.py @@ -1,26 +1,25 @@ # coding:utf-8 +from django.http import HttpResponseRedirect from django.template import RequestContext from django.shortcuts import render_to_response from django.core.paginator import Paginator, EmptyPage from models import IDC, Asset, BisGroup -from models import IDC, Asset, UserGroup from connect import PyCrypt, KEY +cryptor = PyCrypt(KEY) + def index(request): return render_to_response('jasset/jasset.html', ) def jadd_host(request): - header_title, path1, path2 = '添加主机 | Add Host', '资产管理', '添加主机' + login_types = {'L': 'LDAP', 'S': 'SSH_KEY', 'P': 'PASSWORD', 'M': 'MAP'} + header_title, path1, path2 = u'添加主机 | Add Host', u'资产管理', u'添加主机' groups = [] - cryptor = PyCrypt(KEY) eidc = IDC.objects.all() egroup = BisGroup.objects.all() - egroup = UserGroup.objects.all() - is_actived = {'active': 1, 'no_active': 0} - login_typed = {'LDAP': 'L', 'SSH_KEY': 'S', 'PASSWORD': 'P', 'MAP': 'M'} if request.method == 'POST': j_ip = request.POST.get('j_ip') @@ -30,26 +29,23 @@ def jadd_host(request): j_group = request.POST.getlist('j_group') j_active = request.POST.get('j_active') j_comment = request.POST.get('j_comment') - j_idc = IDC.objects.get(name=j_idc) + for group in j_group: c = BisGroup.objects.get(name=group) - c = UserGroup.objects.get(name=group) groups.append(c) if Asset.objects.filter(ip=str(j_ip)): emg = u'该IP已存在!' - return render_to_response('jasset/jadd.html', locals(), context_instance=RequestContext(request)) + return render_to_response('jasset/host_add.html', locals(), context_instance=RequestContext(request)) - if j_type == 'MAP': + if j_type == 'M': j_user = request.POST.get('j_user') j_password = cryptor.encrypt(request.POST.get('j_password')) j_root = request.POST.get('j_root') j_passwd = cryptor.encrypt(request.POST.get('j_passwd')) - a = Asset(ip=j_ip, - port=j_port, - login_type=j_type, - idc=j_idc, + a = Asset(ip=j_ip, port=j_port, + login_type=j_type, idc=j_idc, is_active=int(j_active), comment=j_comment, username_common=j_user, @@ -57,21 +53,21 @@ def jadd_host(request): username_super=j_root, password_super=j_passwd,) else: - a = Asset(ip=j_ip, - port=j_port, - login_type=j_type, - idc=j_idc, + a = Asset(ip=j_ip, port=j_port, + login_type=j_type, idc=j_idc, is_active=int(j_active), comment=j_comment) a.save() + print 'ok' a.bis_group = groups a.save() - - return render_to_response('jasset/jadd.html', locals(), context_instance=RequestContext(request)) + smg = u'主机 %s 添加成功' %j_ip + return render_to_response('jasset/host_add.html', locals(), context_instance=RequestContext(request)) def jlist_host(request): - header_title, path1, path2 = '查看主机 | List Host', '资产管理', '查看主机' + header_title, path1, path2 = u'查看主机 | List Host', u'资产管理', u'查看主机' + login_types = {'L': 'LDAP', 'S': 'SSH_KEY', 'P': 'PASSWORD', 'M': 'MAP'} posts = contact_list = Asset.objects.all().order_by('ip') print posts p = paginator = Paginator(contact_list, 5) @@ -85,16 +81,77 @@ def jlist_host(request): except (EmptyPage, InvalidPage): contacts = paginator.page(paginator.num_pages) - return render_to_response('jasset/jlist.html', locals(), context_instance=RequestContext(request)) + return render_to_response('jasset/host_list.html', locals(), context_instance=RequestContext(request)) + +def host_del(request, offset): + Asset.objects.filter(ip=str(offset)).delete() + return HttpResponseRedirect('/jasset/host_list/') + +def host_edit(request, offset): + actives = {1: u'激活', 0: u'禁用'} + login_types = {'L': 'LDAP', 'S': 'SSH_KEY', 'P': 'PASSWORD', 'M': 'MAP'} + header_title, path1, path2 = u'修改主机 | Edit Host', u'资产管理', u'修改主机' + groups, e_group = [], [] + eidc = IDC.objects.all() + egroup = BisGroup.objects.all() + for g in Asset.objects.get(ip=offset).bis_group.all(): + e_group.append(g) + post = Asset.objects.get(ip = str(offset)) + if request.method == 'POST': + j_ip = request.POST.get('j_ip') + j_idc = request.POST.get('j_idc') + j_port = request.POST.get('j_port') + j_type = request.POST.get('j_type') + j_group = request.POST.getlist('j_group') + j_active = request.POST.get('j_active') + j_comment = request.POST.get('j_comment') + j_idc = IDC.objects.get(name=j_idc) + for group in j_group: + c = BisGroup.objects.get(name=group) + groups.append(c) + + a = Asset.objects.get(ip=str(offset)) + + if j_type == 'M': + j_user = request.POST.get('j_user') + j_password = cryptor.encrypt(request.POST.get('j_password')) + j_root = request.POST.get('j_root') + j_passwd = cryptor.encrypt(request.POST.get('j_passwd')) + a.ip = j_ip + a.port = j_port + a.login_type = j_type + a.idc = j_idc + a.is_active = j_active + a.comment = j_comment + a.username_common = j_user + a.password_common = j_password + a.username_super = j_root + a.password_super = j_passwd + else: + a.ip = j_ip + a.port = j_port + a.idc = j_idc + a.login_type = j_type + a.is_active = j_active + a.comment = j_comment + + a.save() + a.bis_group = groups + a.save() + smg = u'主机 %s 修改成功' %j_ip + return HttpResponseRedirect('/jasset/host_list') + + return render_to_response('jasset/host_edit.html', locals(), context_instance=RequestContext(request)) + def jlist_ip(request, offset): - header_title, path1, path2 = '主机详细信息 | Host Detail.', '资产管理', '主机详情' + header_title, path1, path2 = u'主机详细信息 | Host Detail.', u'资产管理', u'主机详情' print offset post = contact_list = Asset.objects.get(ip = str(offset)) return render_to_response('jasset/jlist_ip.html', locals(), context_instance=RequestContext(request)) def jadd_idc(request): - header_title, path1, path2 = '添加IDC | Add IDC', '资产管理', '添加IDC' + header_title, path1, path2 = u'添加IDC | Add IDC', u'资产管理', u'添加IDC' if request.method == 'POST': j_idc = request.POST.get('j_idc') j_comment = request.POST.get('j_comment') @@ -102,37 +159,47 @@ def jadd_idc(request): if IDC.objects.filter(name=j_idc): emg = u'该IDC已存在!' - return render_to_response('jasset/jadd_idc.html', - {'emg': emg, 'j_idc': j_idc, 'j_comment': j_comment,}, - context_instance=RequestContext(request)) + return render_to_response('jasset/idc_add.html', locals(), context_instance=RequestContext(request)) else: + smg = u'IDC:%s添加成功' %j_idc IDC.objects.create(name=j_idc, comment=j_comment) - return render_to_response('jasset/jadd_idc.html', locals(), context_instance=RequestContext(request)) + return render_to_response('jasset/idc_add.html', locals(), context_instance=RequestContext(request)) def jlist_idc(request): - header_title, path1, path2 = '查看IDC | List Host', '资产管理', '查看IDC' + header_title, path1, path2 = u'查看IDC | List Host', u'资产管理', u'查看IDC' posts = IDC.objects.all().order_by('id') - return render_to_response('jasset/jlist_idc.html', locals(), context_instance=RequestContext(request)) + return render_to_response('jasset/idc_list.html', locals(), context_instance=RequestContext(request)) +def idc_del(request, offset): + IDC.objects.filter(id=offset).delete() + return HttpResponseRedirect('/jasset/idc_list/') def jadd_group(request): - header_title, path1, path2 = '添加业务组 | Add Group', '资产管理', '添加业务组' + header_title, path1, path2 = u'添加业务组 | Add Group', u'资产管理', u'添加业务组' if request.method == 'POST': j_group = request.POST.get('j_group') j_comment = request.POST.get('j_comment') if BisGroup.objects.filter(name=j_group): emg = u'该业务组已存在!' - return render_to_response('jasset/jadd_group.html', locals(), context_instance=RequestContext(request)) + return render_to_response('jasset/group_add.html', locals(), context_instance=RequestContext(request)) else: + smg = u'业务组%s添加成功' %j_group BisGroup.objects.create(name=j_group, comment=j_comment) - return render_to_response('jasset/jadd_group.html', locals(), context_instance=RequestContext(request)) + return render_to_response('jasset/group_add.html', locals(), context_instance=RequestContext(request)) def jlist_group(request): - header_title, path1, path2 = '添加业务组 | Add Group', '资产管理', '查看业务组' + header_title, path1, path2 = u'查看业务组 | Add Group', u'资产管理', u'查看业务组' posts = BisGroup.objects.all().order_by('id') - return render_to_response('jasset/jlist_group.html', locals(), context_instance=RequestContext(request)) + return render_to_response('jasset/group_list.html', locals(), context_instance=RequestContext(request)) + +def group_del(request, offset): + BisGroup.objects.filter(id=offset).delete() + return HttpResponseRedirect('/jasset/group_list/') + +def test(request): + return render_to_response('jasset/test.html', locals()) \ No newline at end of file diff --git a/jumpserver/templatetags/mytags.py b/jumpserver/templatetags/mytags.py index 3ef015a5c..19224af43 100644 --- a/jumpserver/templatetags/mytags.py +++ b/jumpserver/templatetags/mytags.py @@ -15,3 +15,7 @@ def stamp2str(value): @register.filter(name='int2str') def int2str(value): return str(value) + +@register.filter(name='get_item') +def get_item(dictionary, key): + return dictionary.get(key) diff --git a/juser/views.py b/juser/views.py index 09e262b2c..8387e5914 100644 --- a/juser/views.py +++ b/juser/views.py @@ -158,7 +158,6 @@ def user_list(request): def db_add_user(**kwargs): groups_post = kwargs.pop('groups') user = User(**kwargs) - group_select = [] for group_id in groups_post: group = UserGroup.objects.filter(id=group_id) group_select.extend(group) diff --git a/templates/jasset/jadd_group.html b/templates/jasset/group_add.html similarity index 60% rename from templates/jasset/jadd_group.html rename to templates/jasset/group_add.html index 179794ebb..f610d740f 100644 --- a/templates/jasset/jadd_group.html +++ b/templates/jasset/group_add.html @@ -30,14 +30,17 @@ {% if emg %}