diff --git a/docs/AddUserAsset.py b/docs/AddUserAsset.py index 283f838f9..bfe44d27e 100644 --- a/docs/AddUserAsset.py +++ b/docs/AddUserAsset.py @@ -127,14 +127,14 @@ def test_add_log(): if __name__ == '__main__': - install() - test_add_dept() - test_add_group() - test_add_user() - test_add_idc() - test_add_asset_group() + # install() + # test_add_dept() + # test_add_group() + # test_add_user() + # test_add_idc() + # test_add_asset_group() test_add_asset() - test_add_log() + # test_add_log() diff --git a/jasset/models.py b/jasset/models.py index 6bb5a7f21..8f51f8989 100644 --- a/jasset/models.py +++ b/jasset/models.py @@ -3,15 +3,7 @@ from django.db import models from juser.models import User, UserGroup -class IDC(models.Model): - name = models.CharField(max_length=40, unique=True) - comment = models.CharField(max_length=80, blank=True, null=True) - - def __unicode__(self): - return self.name - - -class BisGroup(models.Model): +class AssetGroup(models.Model): GROUP_TYPE = ( ('P', 'PRIVATE'), ('A', 'ASSET'), @@ -67,17 +59,12 @@ class BisGroup(models.Model): class Asset(models.Model): - LOGIN_TYPE_CHOICES = ( - ('L', 'LDAP'), - ('M', 'MAP'), - ) ip = models.IPAddressField(unique=True) port = models.IntegerField(max_length=6) - idc = models.ForeignKey(IDC) - bis_group = models.ManyToManyField(BisGroup) - login_type = models.CharField(max_length=1, choices=LOGIN_TYPE_CHOICES, default='L') + group = models.ManyToManyField(AssetGroup) username = models.CharField(max_length=20, blank=True, null=True) password = models.CharField(max_length=80, blank=True, null=True) + use_default_auth = models.BooleanField(default=True) date_added = models.DateTimeField(auto_now=True, default=datetime.datetime.now(), null=True) is_active = models.BooleanField(default=True) comment = models.CharField(max_length=100, blank=True, null=True) @@ -108,4 +95,4 @@ class AssetAlias(models.Model): alias = models.CharField(max_length=100, blank=True, null=True) def __unicode__(self): - return self.comment \ No newline at end of file + return self.alias diff --git a/jasset/urls.py b/jasset/urls.py index da52529b6..536478c52 100644 --- a/jasset/urls.py +++ b/jasset/urls.py @@ -3,26 +3,23 @@ from django.conf.urls import patterns, include, url from jasset.views import * urlpatterns = patterns('', - url(r'^host_add/$', host_add), - url(r"^host_add_multi/$", host_add_batch), - url(r'^host_list/$', host_list), - url(r'^search/$', host_search), - url(r"^host_detail/$", host_detail), - url(r"^dept_host_ajax/$", dept_host_ajax), - url(r"^show_all_ajax/$", show_all_ajax), - url(r'^idc_add/$', idc_add), - url(r'^idc_list/$', idc_list), - url(r'^idc_edit/$', idc_edit), - url(r'^idc_detail/$', idc_detail), - url(r'^idc_del/$', idc_del), + url(r'^asset_add/$', asset_add), + # url(r"^host_add_multi/$", host_add_batch), url(r'^group_add/$', group_add), - url(r'^group_edit/$', group_edit), url(r'^group_list/$', group_list), - url(r'^group_detail/$', group_detail), - url(r'^group_del_host/$', group_del_host), - url(r'^group_del/$', group_del), - url(r'^host_del/(\w+)/$', host_del), - url(r'^host_edit/$', view_splitter, {'su': host_edit, 'adm': host_edit_adm}), - url(r'^host_edit/batch/$', host_edit_batch), - url(r'^host_edit_common/batch/$', host_edit_common_batch), + url(r'^asset_list/$', asset_list), + url(r'^asset_del/$', asset_del), + url(r"^asset_detail/$", asset_detail), + url(r'^asset_edit/$', asset_edit), + # url(r'^search/$', host_search), + # url(r"^host_detail/$", host_detail), + # url(r"^dept_host_ajax/$", dept_host_ajax), + # url(r"^show_all_ajax/$", show_all_ajax), + # url(r'^group_edit/$', group_edit), + # url(r'^group_list/$', group_list), + # url(r'^group_detail/$', group_detail), + # url(r'^group_del_host/$', group_del_host), + # url(r'^group_del/$', group_del), + # url(r'^host_edit/batch/$', host_edit_batch), + # url(r'^host_edit_common/batch/$', host_edit_common_batch), ) \ No newline at end of file diff --git a/jasset/views.py b/jasset/views.py index 57401f909..6340eea4b 100644 --- a/jasset/views.py +++ b/jasset/views.py @@ -7,917 +7,512 @@ from django.template import RequestContext from django.shortcuts import get_object_or_404 from jperm.models import Perm +from jasset.asset_api import * from jumpserver.api import * - - - -def get_host_groups(groups): - """ 获取主机所属的组类 """ - ret = [] - for group_id in groups: - group = BisGroup.objects.filter(id=group_id) - if group: - group = group[0] - ret.append(group) - group_all = get_object_or_404(BisGroup, name='ALL') - ret.append(group_all) - return ret - - -def get_host_depts(depts): - """ 获取主机所属的部门类 """ - ret = [] - for dept_id in depts: - dept = DEPT.objects.filter(id=dept_id) - if dept: - dept = dept[0] - ret.append(dept) - return ret - - -def db_host_insert(host_info, username='', password=''): - """ 添加主机时数据库操作函数 """ - ip, port, idc, jtype, group, dept, active, comment = host_info - idc = IDC.objects.filter(id=idc) - if idc: - idc = idc[0] - if jtype == 'M': - password = cryptor.encrypt(password) - a = Asset(ip=ip, port=port, - login_type=jtype, idc=idc, - is_active=int(active), - comment=comment, - username=username, - password=password) - else: - a = Asset(ip=ip, port=port, - login_type=jtype, idc=idc, - is_active=int(active), - comment=comment) - a.save() - - all_group = BisGroup.objects.get(name='ALL') - groups = get_host_groups(group) - groups.append(all_group) - - depts = get_host_depts(dept) - - a.bis_group = groups - a.dept = depts - a.save() - - -def db_host_update(host_info, username='', password=''): - """ 修改主机时数据库操作函数 """ - ip, port, idc, jtype, group, dept, active, comment, host = host_info - idc = IDC.objects.filter(id=idc) - if idc: - idc = idc[0] - groups = get_host_groups(group) - depts = get_host_depts(dept) - host.ip = ip - host.port = port - host.login_type = jtype - host.idc = idc - host.is_active = int(active) - host.comment = comment - - if jtype == 'M': - if password != host.password: - password = cryptor.encrypt(password) - host.password = password - host.username = username - host.password = password - host.save() - host.bis_group = groups - host.dept = depts - host.save() - - -def batch_host_edit(host_info, j_user='', j_password=''): - """ 批量修改主机函数 """ - j_id, j_ip, j_idc, j_port, j_type, j_group, j_dept, j_active, j_comment = host_info - groups, depts = [], [] - is_active = {u'是': '1', u'否': '2'} - login_types = {'LDAP': 'L', 'MAP': 'M'} - a = Asset.objects.get(id=j_id) - if '...' in j_group[0].split(): - groups = a.bis_group.all() - else: - for group in j_group[0].split(): - c = BisGroup.objects.get(name=group.strip()) - groups.append(c) - - if '...' in j_dept[0].split(): - depts = a.dept.all() - else: - for d in j_dept[0].split(): - p = DEPT.objects.get(name=d.strip()) - depts.append(p) - - j_type = login_types[j_type] - j_idc = IDC.objects.get(name=j_idc) - if j_type == 'M': - if a.password != j_password: - j_password = cryptor.decrypt(j_password) - 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 = j_user - a.password = j_password - else: - a.ip = j_ip - a.port = j_port - a.idc = j_idc - a.login_type = j_type - a.is_active = is_active[j_active] - a.comment = j_comment - a.save() - a.bis_group = groups - a.dept = depts - a.save() - - -def db_host_delete(request, host_id): - """ 删除主机操作 """ - if is_group_admin(request) and not validate(request, asset=[host_id]): - return httperror(request, '删除失败, 您无权删除!') - - asset = Asset.objects.filter(id=host_id) - if asset: - asset.delete() - else: - return httperror(request, '删除失败, 没有此主机!') - - -def db_idc_delete(request, idc_id): - """ 删除IDC操作 """ - if idc_id == 1: - return httperror(request, '删除失败, 默认IDC不能删除!') - - default_idc = IDC.objects.get(id=1) - - idc = IDC.objects.filter(id=idc_id) - if idc: - idc_class = idc[0] - idc_class.asset_set.update(idc=default_idc) - idc.delete() - else: - return httperror(request, '删除失败, 没有这个IDC!') - - -@require_admin -def host_add(request): - """ 添加主机 """ - header_title, path1, path2 = u'添加主机', u'资产管理', u'添加主机' - login_types = {'L': 'LDAP', 'M': 'MAP'} - eidc = IDC.objects.exclude(name='ALL') - if is_super_user(request): - edept = DEPT.objects.all() - egroup = BisGroup.objects.exclude(name='ALL') - elif is_group_admin(request): - dept = get_session_user_info(request)[5] - egroup = dept.bisgroup_set.all() - - 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') - - if is_super_user(request): - j_dept = request.POST.getlist('j_dept') - host_info = [j_ip, j_port, j_idc, j_type, j_group, j_dept, j_active, j_comment] - elif is_group_admin(request): - j_dept = request.POST.get('j_dept') - host_info = [j_ip, j_port, j_idc, j_type, j_group, [j_dept], j_active, j_comment] - - if is_group_admin(request) and not validate(request, asset_group=j_group, edept=[j_dept]): - return httperror(request, u'添加失败,您无权操作!') - - if Asset.objects.filter(ip=str(j_ip)): - emg = u'该IP %s 已存在!' % j_ip - return my_render('jasset/host_add.html', locals(), request) - if j_type == 'M': - j_user = request.POST.get('j_user') - j_password = request.POST.get('j_password', '') - db_host_insert(host_info, j_user, j_password) - else: - db_host_insert(host_info) - smg = u'主机 %s 添加成功' % j_ip - - return my_render('jasset/host_add.html', locals(), request) - - -@require_admin -def host_add_batch(request): - """ 批量添加主机 """ - header_title, path1, path2 = u'批量添加主机', u'资产管理', u'批量添加主机' - login_types = {'LDAP': 'L', 'MAP': 'M'} - active_types = {'激活': 1, '禁用': 0} - dept_id = get_user_dept(request) - if request.method == 'POST': - multi_hosts = request.POST.get('j_multi').split('\n') - for host in multi_hosts: - if host == '': - break - j_ip, j_port, j_type, j_idc, j_groups, j_depts, j_active, j_comment = host.split() - j_active = active_types[str(j_active)] - j_group = ast.literal_eval(j_groups) - j_dept = ast.literal_eval(j_depts) - - if j_type not in ['LDAP', 'MAP']: - return httperror(request, u'没有%s这种登录方式!' %j_type) - - j_type = login_types[j_type] - idc = IDC.objects.filter(name=j_idc) - if idc: - j_idc = idc[0].id - else: - return httperror(request, '添加失败, 没有%s这个IDC' % j_idc) - - group_ids, dept_ids = [], [] - for group_name in j_group: - group = BisGroup.objects.filter(name=group_name) - if group: - group_id = group[0].id - else: - return httperror(request, '添加失败, 没有%s这个主机组' % group_name) - group_ids.append(group_id) - - for dept_name in j_dept: - dept = DEPT.objects.filter(name=dept_name) - if dept: - dept_id = dept[0].id - else: - return httperror(request, '添加失败, 没有%s这个部门' % dept_name) - dept_ids.append(dept_id) - - if is_group_admin(request) and not validate(request, asset_group=group_ids, edept=dept_ids): - return httperror(request, '添加失败, 没有%s这个主机组' % group_name) - - if Asset.objects.filter(ip=str(j_ip)): - return httperror(request, '添加失败, 改IP%s已存在' % j_ip) - - host_info = [j_ip, j_port, j_idc, j_type, group_ids, dept_ids, j_active, j_comment] - db_host_insert(host_info) - - smg = u'批量添加添加成功' - return my_render('jasset/host_add_multi.html', locals(), request) - - return my_render('jasset/host_add_multi.html', locals(), request) - - -@require_admin -def host_edit_batch(request): - """ 批量修改主机 """ - if request.method == 'POST': - len_table = request.POST.get('len_table') - for i in range(int(len_table)): - j_id = "editable[" + str(i) + "][j_id]" - j_ip = "editable[" + str(i) + "][j_ip]" - j_port = "editable[" + str(i) + "][j_port]" - j_dept = "editable[" + str(i) + "][j_dept]" - j_idc = "editable[" + str(i) + "][j_idc]" - j_type = "editable[" + str(i) + "][j_type]" - j_group = "editable[" + str(i) + "][j_group]" - j_active = "editable[" + str(i) + "][j_active]" - j_comment = "editable[" + str(i) + "][j_comment]" - - j_id = request.POST.get(j_id).strip() - j_ip = request.POST.get(j_ip).strip() - j_port = request.POST.get(j_port).strip() - j_dept = request.POST.getlist(j_dept) - j_idc = request.POST.get(j_idc).strip() - j_type = request.POST.get(j_type).strip() - j_group = request.POST.getlist(j_group) - j_active = request.POST.get(j_active).strip() - j_comment = request.POST.get(j_comment).strip() - - host_info = [j_id, j_ip, j_idc, j_port, j_type, j_group, j_dept, j_active, j_comment] - batch_host_edit(host_info) - - return HttpResponseRedirect('/jasset/host_list/') - - -@require_login -def host_edit_common_batch(request): - """ 普通用户批量修改主机别名 """ - u = get_session_user_info(request)[2] - if request.method == 'POST': - len_table = request.POST.get('len_table') - for i in range(int(len_table)): - j_id = "editable[" + str(i) + "][j_id]" - j_alias = "editable[" + str(i) + "][j_alias]" - j_id = request.POST.get(j_id, '').strip() - j_alias = request.POST.get(j_alias, '').strip() - a = Asset.objects.get(id=j_id) - asset_alias = AssetAlias.objects.filter(user=u, host=a) - if asset_alias: - asset_alias = asset_alias[0] - asset_alias.alias = j_alias - asset_alias.save() - else: - AssetAlias.objects.create(user=u, host=a, alias=j_alias) - return my_render('jasset/host_list_common.html', locals(), request) - - -@require_login -def host_list(request): - """ 列出主机 """ - header_title, path1, path2 = u'查看主机', u'资产管理', u'查看主机' - keyword = request.GET.get('keyword', '') - dept_id = get_session_user_info(request)[3] - dept = DEPT.objects.get(id=dept_id) - did = request.GET.get('did', '') - gid = request.GET.get('gid', '') - sid = request.GET.get('sid', '') - user_id = get_session_user_info(request)[0] - - post_all = Asset.objects.all().order_by('ip') - post_keyword_all = Asset.objects.filter(Q(ip__contains=keyword) | - Q(idc__name__contains=keyword) | - Q(bis_group__name__contains=keyword) | - Q(comment__contains=keyword)).distinct().order_by('ip') - if did: - if is_common_user(request): - return httperror(request, u'您无权查看!') - - if is_group_admin(request): - user, dept = get_session_user_dept(request) - else: - dept = DEPT.objects.get(id=did) - posts = dept.asset_set.all() - return my_render('jasset/host_list_nop.html', locals(), request) - - elif gid: - if is_common_user(request): - return httperror(request, u'您无权查看!') - - elif is_group_admin(request) and not validate(request, user_group=[gid]): - return httperror(request, u'您无权查看!') - - posts = [] - user_group = UserGroup.objects.filter(id=gid) - if user_group: - perms = Perm.objects.filter(user_group=user_group) - for perm in perms: - for post in perm.asset_group.asset_set.all(): - posts.append(post) - posts = list(set(posts)) - else: - return httperror(request, u'没有这个小组!') - return my_render('jasset/host_list_nop.html', locals(), request) - - elif sid: - if is_common_user(request): - return httperror(request, u'您无权查看!') - - elif is_group_admin(request) and not validate(request, user_group=[sid]): - return httperror(request, u'您无权查看!') - - posts, asset_groups = [], [] - user_group = UserGroup.objects.filter(id=int(sid)) - if user_group: - user_group = user_group[0] - for perm in user_group.sudoperm_set.all(): - asset_groups.extend(perm.asset_group.all()) - - for asset_group in asset_groups: - posts.extend(asset_group.asset_set.all()) - posts = list(set(posts)) - else: - return httperror(request, u'没有这个sudo授权!') - return my_render('jasset/host_list_nop.html', locals(), request) - - else: - if is_super_user(request): - if keyword: - posts = post_keyword_all - else: - posts = post_all - contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) - return my_render('jasset/host_list.html', locals(), request) - - elif is_group_admin(request): - if keyword: - posts = post_keyword_all.filter(dept=dept) - else: - posts = post_all.filter(dept=dept) - - contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) - return my_render('jasset/host_list.html', locals(), request) - - elif is_common_user(request): - user_id, username = get_session_user_info(request)[0:2] - posts = user_perm_asset_api(username) - contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) - return my_render('jasset/host_list_common.html', locals(), request) - - -@require_admin -def host_del(request, offset): - """ 删除主机 """ - if offset == 'multi': - len_list = request.POST.get("len_list") - for i in range(int(len_list)): - key = "id_list[" + str(i) + "]" - host_id = request.POST.get(key) - db_host_delete(request, host_id) - else: - db_host_delete(request, offset) - - return HttpResponseRedirect('/jasset/host_list/') - - -@require_super_user -def host_edit(request): - """ 修改主机 """ - header_title, path1, path2 = u'修改主机', u'资产管理', u'修改主机' - actives = {1: u'激活', 0: u'禁用'} - login_types = {'L': 'LDAP', 'M': 'MAP'} - eidc = IDC.objects.all() - egroup = BisGroup.objects.exclude(name='ALL') - edept = DEPT.objects.all() - host_id = request.GET.get('id', '') - post = Asset.objects.filter(id=int(host_id)) - if post: - post = post[0] - else: - return httperror(request, '没有此主机!') - - e_group = post.bis_group.all() - e_dept = post.dept.all() - - 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_dept = request.POST.getlist('j_dept', '') - j_group = request.POST.getlist('j_group', '') - j_active = request.POST.get('j_active', '') - j_comment = request.POST.get('j_comment', '') - - host_info = [j_ip, j_port, j_idc, j_type, j_group, j_dept, j_active, j_comment, post] - if j_type == 'M': - j_user = request.POST.get('j_user') - j_password = request.POST.get('j_password') - db_host_update(host_info, j_user, j_password) - else: - db_host_update(host_info) - - smg = u'主机 %s 修改成功' % j_ip - return HttpResponseRedirect('/jasset/host_detail/?id=%s' % host_id) - - return my_render('jasset/host_edit.html', locals(), request) - - -@require_admin -def host_edit_adm(request): - """ 部门管理员修改主机 """ - header_title, path1, path2 = u'修改主机', u'资产管理', u'修改主机' - actives = {1: u'激活', 0: u'禁用'} - login_types = {'L': 'LDAP', 'M': 'MAP'} - eidc = IDC.objects.all() - dept = get_session_user_info(request)[5] - egroup = BisGroup.objects.exclude(name='ALL').filter(dept=dept) - host_id = request.GET.get('id', '') - post = Asset.objects.filter(id=int(host_id)) - if post: - post = post[0] - else: - return httperror(request, '没有此主机!') - - e_group = post.bis_group.all() - - 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_dept = request.POST.getlist('j_dept') - j_group = request.POST.getlist('j_group') - j_active = request.POST.get('j_active') - j_comment = request.POST.get('j_comment') - - host_info = [j_ip, j_port, j_idc, j_type, j_group, j_dept, j_active, j_comment] - - if not validate(request, asset_group=j_group, edept=j_dept): - emg = u'修改失败,您无权操作!' - return my_render('jasset/host_edit.html', locals(), request) - - if j_type == 'M': - j_user = request.POST.get('j_user') - j_password = request.POST.get('j_password') - db_host_update(host_info, j_user, j_password, post) - else: - db_host_update(host_info, post) - - smg = u'主机 %s 修改成功' % j_ip - return HttpResponseRedirect('/jasset/host_detail/?id=%s' % host_id) - - return my_render('jasset/host_edit.html', locals(), request) - - -@require_login -def host_detail(request): - """ 主机详情 """ - header_title, path1, path2 = u'主机详细信息', u'资产管理', u'主机详情' - host_id = request.GET.get('id', '') - post = Asset.objects.filter(id=host_id) - if not post: - return httperror(request, '没有此主机!') - post = post[0] - - if is_group_admin(request) and not validate(request, asset=[host_id]): - return httperror(request, '您无权查看!') - - elif is_common_user(request): - username = get_session_user_info(request)[1] - user_permed_hosts = user_perm_asset_api(username) - if post not in user_permed_hosts: - return httperror(request, '您无权查看!') - else: - log_all = Log.objects.filter(host=post.ip) - log, log_more = log_all[:10], log_all[10:] - user_permed_list = asset_perm_api(post) - - return my_render('jasset/host_detail.html', locals(), request) - - -@require_super_user -def idc_add(request): - """ 添加IDC """ - header_title, path1, path2 = u'添加IDC', u'资产管理', u'添加IDC' - if request.method == 'POST': - j_idc = request.POST.get('j_idc') - j_comment = request.POST.get('j_comment') - if IDC.objects.filter(name=j_idc): - emg = u'该IDC已存在!' - return my_render('jasset/idc_add.html', locals(), request) - else: - smg = u'IDC:%s添加成功' % j_idc - IDC.objects.create(name=j_idc, comment=j_comment) - - return my_render('jasset/idc_add.html', locals(), request) - - -@require_admin -def idc_list(request): - """ 列出IDC """ - header_title, path1, path2 = u'查看IDC', u'资产管理', u'查看IDC' - dept_id = get_user_dept(request) - dept = DEPT.objects.get(id=dept_id) - keyword = request.GET.get('keyword', '') - if keyword: - posts = IDC.objects.filter(Q(name__contains=keyword) | Q(comment__contains=keyword)) - else: - posts = IDC.objects.exclude(name='ALL').order_by('id') - contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) - return my_render('jasset/idc_list.html', locals(), request) - - -@require_super_user -def idc_edit(request): - """ 修改IDC """ - header_title, path1, path2 = u'编辑IDC', u'资产管理', u'编辑IDC' - idc_id = request.GET.get('id', '') - idc = IDC.objects.filter(id=idc_id) - if int(idc_id) == 1: - return httperror(request, u'默认IDC不能编辑!') - if idc: - idc = idc[0] - default = IDC.objects.get(id=1).asset_set.all() - eposts = Asset.objects.filter(idc=idc).order_by('ip') - posts = [g for g in default if g not in eposts] - else: - return httperror(request, u'此IDC不存在') - - if request.method == 'POST': - idc_id = request.POST.get('id') - j_idc = request.POST.get('j_idc') - j_hosts = request.POST.getlist('j_hosts') - j_comment = request.POST.get('j_comment') - idc_default = request.POST.getlist('idc_default') - - idc = IDC.objects.filter(id=idc_id) - if idc: - idc.update(name=j_idc, comment=j_comment) - for host_id in j_hosts: - Asset.objects.filter(id=host_id).update(idc=idc[0]) - - i = IDC.objects.get(id=1) - for host in idc_default: - g = Asset.objects.filter(id=host).update(idc=i) - else: - return httperror(request, u'此IDC不存在') - - return HttpResponseRedirect('/jasset/idc_list/?id=%s' % idc_id) - - return my_render('jasset/idc_edit.html', locals(), request) - - -@require_admin -def idc_detail(request): - """ IDC详情 """ - header_title, path1, path2 = u'IDC详情', u'资产管理', u'IDC详情' - login_types = {'L': 'LDAP', 'M': 'MAP'} - idc_id = request.GET.get('id', '') - idc_filter = IDC.objects.filter(id=idc_id) - if idc_filter: - idc = idc_filter[0] - else: - return httperror(request, '没有此IDC') - dept = get_session_user_info(request)[5] - if is_super_user(request): - posts = Asset.objects.filter(idc=idc).order_by('ip') - elif is_group_admin(request): - posts = Asset.objects.filter(idc=idc, dept=dept).order_by('ip') - contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) - - return my_render('jasset/idc_detail.html', locals(), request) - - -@require_super_user -def idc_del(request): - """ 删除IDC """ - offset = request.GET.get('id', '') - if offset == 'multi': - len_list = request.POST.get("len_list") - for i in range(int(len_list)): - key = "id_list[" + str(i) + "]" - idc_id = request.POST.get(key) - db_idc_delete(request, int(idc_id)) - else: - db_idc_delete(request, int(offset)) - return HttpResponseRedirect('/jasset/idc_list/') - - -@require_admin +@require_role('admin') def group_add(request): - """ 添加主机组 """ - header_title, path1, path2 = u'添加主机组', u'资产管理', u'添加主机组' - if is_super_user(request): - posts = Asset.objects.all() - edept = DEPT.objects.all() - elif is_group_admin(request): - dept_id = get_user_dept(request) - dept = DEPT.objects.get(id=dept_id) - posts = Asset.objects.filter(dept=dept) - edept = get_session_user_info(request)[5] + """ + Add asset group + 添加资产组 + """ + header_title, path1, path2 = u'添加资产组', u'资产管理', u'添加资产组' + asset_all = Asset.objects.all() if request.method == 'POST': - j_group = request.POST.get('j_group', '') - j_dept = request.POST.get('j_dept', '') - j_hosts = request.POST.getlist('j_hosts', '') - j_comment = request.POST.get('j_comment', '') + name = request.POST.get('name', '') + asset_select = request.POST.getlist('asset_select', []) + comment = request.POST.get('comment', '') try: - if is_group_admin(request) and not validate(request, asset=j_hosts, edept=[j_dept]): - emg = u'添加失败, 您无权操作!' - raise RaiseError + if not name: + error = u'组名不能为空' + raise ServerError(error) - elif BisGroup.objects.filter(name=j_group): - emg = u'添加失败, 该主机组已存在!' - raise RaiseError + asset_group_test = get_object(AssetGroup, name=name) + if asset_group_test: + error = u"该组名 %s 已存在" % name + raise ServerError(error) - except RaiseError: + except ServerError: pass - else: - j_dept = DEPT.objects.filter(id=j_dept)[0] - group = BisGroup.objects.create(name=j_group, dept=j_dept, comment=j_comment) - for host in j_hosts: - g = Asset.objects.get(id=host) - group.asset_set.add(g) - smg = u'主机组 %s 添加成功' % j_group + db_add_group(name=name, comment=comment, asset_select=asset_select) + msg = u"主机组 %s 添加成功" % name return my_render('jasset/group_add.html', locals(), request) -@require_admin +@require_role('admin') def group_list(request): - """ 列出主机组 """ - header_title, path1, path2 = u'查看主机组', u'资产管理', u'查看主机组' - dept_id = get_user_dept(request) - dept = DEPT.objects.get(id=dept_id) + """ + 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') - if gid: - if is_common_user(request): - return httperror(request, u'您无权查看!') + asset_group_list = AssetGroup.objects.all() - elif is_group_admin(request) and not validate(request, user_group=[gid]): - return httperror(request, u'您无权查看!') + if keyword: + asset_groups = asset_group_list.filter(Q(name__contains=keyword) | Q(comment__contains=keyword)) - posts = [] - user_group = UserGroup.objects.filter(id=gid) - if user_group: - user_group = user_group[0] - perms = Perm.objects.filter(user_group=user_group) - for perm in perms: - posts.append(perm.asset_group) - - elif sid: - if is_common_user(request): - return httperror(request, u'您无权查看!') - - elif is_group_admin(request) and not validate(request, user_group=[sid]): - return httperror(request, u'您无权查看!') - - posts = [] - user_group = UserGroup.objects.filter(id=sid) - if user_group: - user_group = user_group[0] - for perm in user_group.sudoperm_set.all(): - posts.extend(perm.asset_group.all()) - posts = list(set(posts)) - else: - return httperror(request, u'没有此sudo授权!') - - else: - if is_super_user(request): - if keyword: - posts = BisGroup.objects.exclude(name='ALL').filter( - Q(name__contains=keyword) | Q(comment__contains=keyword)) - else: - posts = BisGroup.objects.exclude(name='ALL').order_by('id') - elif is_group_admin(request): - if keyword: - posts = BisGroup.objects.filter(Q(name__contains=keyword) | Q(comment__contains=keyword)).filter( - dept=dept) - else: - posts = BisGroup.objects.filter(dept=dept).order_by('id') - contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) + 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) -@require_admin -def group_edit(request): - """ 修改主机组 """ - header_title, path1, path2 = u'编辑主机组', u'资产管理', u'编辑主机组' - group_id = request.GET.get('id', '') - group = BisGroup.objects.filter(id=group_id) - if group: - group = group[0] - else: - httperror(request, u'没有这个主机组!') +@require_role('admin') +def asset_add(request): + """ + Asset add view + 添加资产 + """ + header_title, path1, path2 = u'添加资产', u'资产管理', u'添加资产' + if request.method == 'POST': + ip = request.POST.get('ip') + port = request.POST.get('port') + group = request.POST.getlist('group') + use_default_auth = True if request.POST.getlist('use_default_auth', []) else False + is_active = True if request.POST.get('is_active') else False + comment = request.POST.get('comment') - host_all = Asset.objects.all() - dept_id = get_session_user_info(request)[3] - eposts = Asset.objects.filter(bis_group=group) + if not use_default_auth: + username = request.POST.get('username') + password = request.POST.get('password') + password_encode = CRYPTOR.encrypt(password) + else: + username = None + password_encode = None - if is_group_admin(request) and not validate(request, asset_group=[group_id]): - return httperror(request, '编辑失败, 您无权操作!') - dept = DEPT.objects.filter(id=group.dept.id) - if dept: - dept = dept[0] - else: - return httperror(request, u'没有这个部门!') + try: + if Asset.objects.filter(ip=str(ip)): + error = u'该IP %s 已存在!' % ip + raise ServerError(error) - all_dept = dept.asset_set.all() - posts = [g for g in all_dept if g not in eposts] + except ServerError: + pass + else: + db_asset_add( + ip=ip, port=port, use_default_auth=use_default_auth, is_active=is_active, comment=comment, + username=username, password=password_encode + ) + + msg = u'主机 %s 添加成功' % ip + + return my_render('jasset/asset_add.html', locals(), request) +# +# +# @require_admin +# def host_add_batch(request): +# """ 批量添加主机 """ +# header_title, path1, path2 = u'批量添加主机', u'资产管理', u'批量添加主机' +# login_types = {'LDAP': 'L', 'MAP': 'M'} +# active_types = {'激活': 1, '禁用': 0} +# dept_id = get_user_dept(request) +# if request.method == 'POST': +# multi_hosts = request.POST.get('j_multi').split('\n') +# for host in multi_hosts: +# if host == '': +# break +# j_ip, j_port, j_type, j_idc, j_groups, j_depts, j_active, j_comment = host.split() +# j_active = active_types[str(j_active)] +# j_group = ast.literal_eval(j_groups) +# j_dept = ast.literal_eval(j_depts) +# +# if j_type not in ['LDAP', 'MAP']: +# return httperror(request, u'没有%s这种登录方式!' %j_type) +# +# j_type = login_types[j_type] +# idc = IDC.objects.filter(name=j_idc) +# if idc: +# j_idc = idc[0].id +# else: +# return httperror(request, '添加失败, 没有%s这个IDC' % j_idc) +# +# group_ids, dept_ids = [], [] +# for group_name in j_group: +# group = BisGroup.objects.filter(name=group_name) +# if group: +# group_id = group[0].id +# else: +# return httperror(request, '添加失败, 没有%s这个主机组' % group_name) +# group_ids.append(group_id) +# +# for dept_name in j_dept: +# dept = DEPT.objects.filter(name=dept_name) +# if dept: +# dept_id = dept[0].id +# else: +# return httperror(request, '添加失败, 没有%s这个部门' % dept_name) +# dept_ids.append(dept_id) +# +# if is_group_admin(request) and not validate(request, asset_group=group_ids, edept=dept_ids): +# return httperror(request, '添加失败, 没有%s这个主机组' % group_name) +# +# if Asset.objects.filter(ip=str(j_ip)): +# return httperror(request, '添加失败, 改IP%s已存在' % j_ip) +# +# host_info = [j_ip, j_port, j_idc, j_type, group_ids, dept_ids, j_active, j_comment] +# db_host_insert(host_info) +# +# smg = u'批量添加添加成功' +# return my_render('jasset/host_add_multi.html', locals(), request) +# +# return my_render('jasset/host_add_multi.html', locals(), request) +# +# +# @require_admin +# def host_edit_batch(request): +# """ 批量修改主机 """ +# if request.method == 'POST': +# len_table = request.POST.get('len_table') +# for i in range(int(len_table)): +# j_id = "editable[" + str(i) + "][j_id]" +# j_ip = "editable[" + str(i) + "][j_ip]" +# j_port = "editable[" + str(i) + "][j_port]" +# j_dept = "editable[" + str(i) + "][j_dept]" +# j_idc = "editable[" + str(i) + "][j_idc]" +# j_type = "editable[" + str(i) + "][j_type]" +# j_group = "editable[" + str(i) + "][j_group]" +# j_active = "editable[" + str(i) + "][j_active]" +# j_comment = "editable[" + str(i) + "][j_comment]" +# +# j_id = request.POST.get(j_id).strip() +# j_ip = request.POST.get(j_ip).strip() +# j_port = request.POST.get(j_port).strip() +# j_dept = request.POST.getlist(j_dept) +# j_idc = request.POST.get(j_idc).strip() +# j_type = request.POST.get(j_type).strip() +# j_group = request.POST.getlist(j_group) +# j_active = request.POST.get(j_active).strip() +# j_comment = request.POST.get(j_comment).strip() +# +# host_info = [j_id, j_ip, j_idc, j_port, j_type, j_group, j_dept, j_active, j_comment] +# batch_host_edit(host_info) +# +# return HttpResponseRedirect('/jasset/host_list/') +# +# +# @require_role(role='user') +# def host_edit_common_batch(request): +# """ 普通用户批量修改主机别名 """ +# u = get_session_user_info(request)[2] +# if request.method == 'POST': +# len_table = request.POST.get('len_table') +# for i in range(int(len_table)): +# j_id = "editable[" + str(i) + "][j_id]" +# j_alias = "editable[" + str(i) + "][j_alias]" +# j_id = request.POST.get(j_id, '').strip() +# j_alias = request.POST.get(j_alias, '').strip() +# a = Asset.objects.get(id=j_id) +# asset_alias = AssetAlias.objects.filter(user=u, host=a) +# if asset_alias: +# asset_alias = asset_alias[0] +# asset_alias.alias = j_alias +# asset_alias.save() +# else: +# AssetAlias.objects.create(user=u, host=a, alias=j_alias) +# return my_render('jasset/host_list_common.html', locals(), request) + + +@require_role(role='user') +def asset_list(request): + """ + list assets + 列出资产表 + """ + header_title, path1, path2 = u'查看主机', u'资产管理', u'查看主机' + keyword = request.GET.get('keyword', '') + gid = request.GET.get('gid', '') # asset group id + sid = request.GET.get('sid', '') + assets_list = Asset.objects.all().order_by('ip') + + if keyword: + assets_list = assets_list.filter(Q(ip__contains=keyword) | + Q(comment__contains=keyword)).distinct().order_by('ip') + + 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) + + +@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 + + +@require_role(role='super') +def asset_edit(request): + """ 修改主机 """ + header_title, path1, path2 = u'修改资产', u'资产管理', u'修改资产' + + asset_id = request.GET.get('id', '') + if not asset_id: + return HttpResponse('没有该主机') + asset = get_object(Asset, id=asset_id) if request.method == 'POST': - j_group = request.POST.get('j_group', '') - j_hosts = request.POST.getlist('j_hosts', '') - j_dept = request.POST.get('j_dept', '') - j_comment = request.POST.get('j_comment', '') + ip = request.POST.get('ip') + port = request.POST.get('port') + groups = request.POST.getlist('groups') + use_default_auth = True if request.POST.getlist('use_default_auth', []) else False + is_active = True if request.POST.get('is_active') else False + comment = request.POST.get('comment') - j_dept = DEPT.objects.filter(id=int(j_dept)) - j_dept = j_dept[0] + if not use_default_auth: + username = request.POST.get('username') + password = request.POST.get('password') + if password == asset.password: + password_encode = password + else: + password_encode = CRYPTOR.encrypt(password) + else: + username = None + password_encode = None - group.asset_set.clear() - for host in j_hosts: - g = Asset.objects.get(id=host) - group.asset_set.add(g) - BisGroup.objects.filter(id=group_id).update(name=j_group, dept=j_dept, comment=j_comment) - smg = u'主机组%s修改成功' % j_group - return HttpResponseRedirect('/jasset/group_list') + try: + 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: + pass + else: + db_asset_update(id=asset_id, ip=ip, port=port, use_default_auth=use_default_auth, + 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/group_edit.html', locals(), request) + return my_render('jasset/asset_edit.html', locals(), request) -@require_admin -def group_detail(request): - """ 主机组详情 """ - header_title, path1, path2 = u'主机组详情', u'资产管理', u'主机组详情' - login_types = {'L': 'LDAP', 'M': 'MAP'} - dept = get_session_user_info(request)[5] - group_id = request.GET.get('id', '') - group = BisGroup.objects.get(id=group_id) - if is_super_user(request): - posts = Asset.objects.filter(bis_group=group).order_by('ip') - - elif is_group_admin(request): - if not validate(request, asset_group=[group_id]): - return httperror(request, u'您无权查看!') - posts = Asset.objects.filter(bis_group=group).filter(dept=dept).order_by('ip') - - contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) - return my_render('jasset/group_detail.html', locals(), request) +# @require_role(role='admin') +# def host_edit_adm(request): +# """ 部门管理员修改主机 """ +# header_title, path1, path2 = u'修改主机', u'资产管理', u'修改主机' +# actives = {1: u'激活', 0: u'禁用'} +# login_types = {'L': 'LDAP', 'M': 'MAP'} +# eidc = IDC.objects.all() +# dept = get_session_user_info(request)[5] +# egroup = BisGroup.objects.exclude(name='ALL').filter(dept=dept) +# host_id = request.GET.get('id', '') +# post = Asset.objects.filter(id=int(host_id)) +# if post: +# post = post[0] +# else: +# return httperror(request, '没有此主机!') +# +# e_group = post.bis_group.all() +# +# 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_dept = request.POST.getlist('j_dept') +# j_group = request.POST.getlist('j_group') +# j_active = request.POST.get('j_active') +# j_comment = request.POST.get('j_comment') +# +# host_info = [j_ip, j_port, j_idc, j_type, j_group, j_dept, j_active, j_comment] +# +# if not validate(request, asset_group=j_group, edept=j_dept): +# emg = u'修改失败,您无权操作!' +# return my_render('jasset/asset_edit.html', locals(), request) +# +# if j_type == 'M': +# j_user = request.POST.get('j_user') +# j_password = request.POST.get('j_password') +# db_host_update(host_info, j_user, j_password, post) +# else: +# db_host_update(host_info, post) +# +# smg = u'主机 %s 修改成功' % j_ip +# return HttpResponseRedirect('/jasset/host_detail/?id=%s' % host_id) +# +# return my_render('jasset/asset_edit.html', locals(), request) -@require_admin -def group_del_host(request): - """ 主机组中剔除主机, 并不删除真实主机 """ - if request.method == 'POST': - group_id = request.POST.get('group_id') - offset = request.GET.get('id', '') - group = BisGroup.objects.get(id=group_id) - if offset == 'group': - len_list = request.POST.get("len_list") - for i in range(int(len_list)): - key = "id_list[" + str(i) + "]" - jid = request.POST.get(key) - g = Asset.objects.get(id=jid) - group.asset_set.remove(g) +@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) - else: - offset = request.GET.get('id', '') - group_id = request.GET.get('gid', '') - group = BisGroup.objects.get(id=group_id) - g = Asset.objects.get(id=offset) - group.asset_set.remove(g) - - return HttpResponseRedirect('/jasset/group_detail/?id=%s' % group.id) + return my_render('jasset/asset_detail.html', locals(), request) -@require_admin -def group_del(request): - """ 删除主机组 """ - offset = request.GET.get('id', '') - if offset == 'multi': - len_list = request.POST.get("len_list") - for i in range(int(len_list)): - key = "id_list[" + str(i) + "]" - gid = request.POST.get(key) - if is_group_admin(request) and not validate(request, asset_group=[gid]): - return httperror(request, '删除失败, 您无权删除!') - BisGroup.objects.filter(id=gid).delete() - else: - gid = int(offset) - if is_group_admin(request) and not validate(request, asset_group=[gid]): - return httperror(request, '删除失败, 您无权删除!') - BisGroup.objects.filter(id=gid).delete() - return HttpResponseRedirect('/jasset/group_list/') -@require_admin -def dept_host_ajax(request): - """ 添加主机组时, 部门联动主机异步 """ - dept_id = request.GET.get('id', '') - if dept_id not in ['1', '2']: - dept = DEPT.objects.filter(id=dept_id) - if dept: - dept = dept[0] - hosts = dept.asset_set.all() - else: - hosts = Asset.objects.all() - - return my_render('jasset/dept_host_ajax.html', locals(), request) - - -def show_all_ajax(request): - """ 批量修改主机时, 部门和组全部显示 """ - env = request.GET.get('env', '') - get_id = request.GET.get('id', '') - host = Asset.objects.filter(id=get_id) - if host: - host = host[0] - return my_render('jasset/show_all_ajax.html', locals(), request) - - -@require_login -def host_search(request): - """ 搜索主机 """ - keyword = request.GET.get('keyword') - login_types = {'L': 'LDAP', 'M': 'MAP'} - dept = get_session_user_info(request)[5] - post_all = Asset.objects.filter(Q(ip__contains=keyword) | - Q(idc__name__contains=keyword) | - Q(bis_group__name__contains=keyword) | - Q(comment__contains=keyword)).distinct().order_by('ip') - if is_super_user(request): - posts = post_all - - elif is_group_admin(request): - posts = post_all.filter(dept=dept) - - elif is_common_user(request): - user_id, username = get_session_user_info(request)[0:2] - post_perm = user_perm_asset_api(username) - posts = list(set(post_all) & set(post_perm)) - contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) - - return my_render('jasset/host_search.html', locals(), request) \ No newline at end of file +# +# +# @require_admin +# def group_edit(request): +# """ 修改主机组 """ +# header_title, path1, path2 = u'编辑主机组', u'资产管理', u'编辑主机组' +# group_id = request.GET.get('id', '') +# group = BisGroup.objects.filter(id=group_id) +# if group: +# group = group[0] +# else: +# httperror(request, u'没有这个主机组!') +# +# host_all = Asset.objects.all() +# dept_id = get_session_user_info(request)[3] +# eposts = Asset.objects.filter(bis_group=group) +# +# if is_group_admin(request) and not validate(request, asset_group=[group_id]): +# return httperror(request, '编辑失败, 您无权操作!') +# dept = DEPT.objects.filter(id=group.dept.id) +# if dept: +# dept = dept[0] +# else: +# return httperror(request, u'没有这个部门!') +# +# all_dept = dept.asset_set.all() +# posts = [g for g in all_dept if g not in eposts] +# +# if request.method == 'POST': +# j_group = request.POST.get('j_group', '') +# j_hosts = request.POST.getlist('j_hosts', '') +# j_dept = request.POST.get('j_dept', '') +# j_comment = request.POST.get('j_comment', '') +# +# j_dept = DEPT.objects.filter(id=int(j_dept)) +# j_dept = j_dept[0] +# +# group.asset_set.clear() +# for host in j_hosts: +# g = Asset.objects.get(id=host) +# group.asset_set.add(g) +# BisGroup.objects.filter(id=group_id).update(name=j_group, dept=j_dept, comment=j_comment) +# smg = u'主机组%s修改成功' % j_group +# return HttpResponseRedirect('/jasset/group_list') +# +# return my_render('jasset/group_edit.html', locals(), request) +# +# +# @require_admin +# def group_detail(request): +# """ 主机组详情 """ +# header_title, path1, path2 = u'主机组详情', u'资产管理', u'主机组详情' +# login_types = {'L': 'LDAP', 'M': 'MAP'} +# dept = get_session_user_info(request)[5] +# group_id = request.GET.get('id', '') +# group = BisGroup.objects.get(id=group_id) +# if is_super_user(request): +# posts = Asset.objects.filter(bis_group=group).order_by('ip') +# +# elif is_group_admin(request): +# if not validate(request, asset_group=[group_id]): +# return httperror(request, u'您无权查看!') +# posts = Asset.objects.filter(bis_group=group).filter(dept=dept).order_by('ip') +# +# contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) +# return my_render('jasset/group_detail.html', locals(), request) +# +# +# @require_admin +# def group_del_host(request): +# """ 主机组中剔除主机, 并不删除真实主机 """ +# if request.method == 'POST': +# group_id = request.POST.get('group_id') +# offset = request.GET.get('id', '') +# group = BisGroup.objects.get(id=group_id) +# if offset == 'group': +# len_list = request.POST.get("len_list") +# for i in range(int(len_list)): +# key = "id_list[" + str(i) + "]" +# jid = request.POST.get(key) +# g = Asset.objects.get(id=jid) +# group.asset_set.remove(g) +# +# else: +# offset = request.GET.get('id', '') +# group_id = request.GET.get('gid', '') +# group = BisGroup.objects.get(id=group_id) +# g = Asset.objects.get(id=offset) +# group.asset_set.remove(g) +# +# return HttpResponseRedirect('/jasset/group_detail/?id=%s' % group.id) +# +# +# @require_admin +# def group_del(request): +# """ 删除主机组 """ +# offset = request.GET.get('id', '') +# if offset == 'multi': +# len_list = request.POST.get("len_list") +# for i in range(int(len_list)): +# key = "id_list[" + str(i) + "]" +# gid = request.POST.get(key) +# if is_group_admin(request) and not validate(request, asset_group=[gid]): +# return httperror(request, '删除失败, 您无权删除!') +# BisGroup.objects.filter(id=gid).delete() +# else: +# gid = int(offset) +# if is_group_admin(request) and not validate(request, asset_group=[gid]): +# return httperror(request, '删除失败, 您无权删除!') +# BisGroup.objects.filter(id=gid).delete() +# return HttpResponseRedirect('/jasset/group_list/') +# +# +# @require_admin +# def dept_host_ajax(request): +# """ 添加主机组时, 部门联动主机异步 """ +# dept_id = request.GET.get('id', '') +# if dept_id not in ['1', '2']: +# dept = DEPT.objects.filter(id=dept_id) +# if dept: +# dept = dept[0] +# hosts = dept.asset_set.all() +# else: +# hosts = Asset.objects.all() +# +# return my_render('jasset/dept_host_ajax.html', locals(), request) +# +# +# def show_all_ajax(request): +# """ 批量修改主机时, 部门和组全部显示 """ +# env = request.GET.get('env', '') +# get_id = request.GET.get('id', '') +# host = Asset.objects.filter(id=get_id) +# if host: +# host = host[0] +# return my_render('jasset/show_all_ajax.html', locals(), request) +# +# +# @require_login +# def host_search(request): +# """ 搜索主机 """ +# keyword = request.GET.get('keyword') +# login_types = {'L': 'LDAP', 'M': 'MAP'} +# dept = get_session_user_info(request)[5] +# post_all = Asset.objects.filter(Q(ip__contains=keyword) | +# Q(idc__name__contains=keyword) | +# Q(bis_group__name__contains=keyword) | +# Q(comment__contains=keyword)).distinct().order_by('ip') +# if is_super_user(request): +# posts = post_all +# +# elif is_group_admin(request): +# posts = post_all.filter(dept=dept) +# +# elif is_common_user(request): +# user_id, username = get_session_user_info(request)[0:2] +# post_perm = user_perm_asset_api(username) +# posts = list(set(post_all) & set(post_perm)) +# contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) +# +# return my_render('jasset/host_search.html', locals(), request) \ No newline at end of file diff --git a/jperm/models.py b/jperm/models.py index 6541cb55f..3aaa7138a 100644 --- a/jperm/models.py +++ b/jperm/models.py @@ -4,12 +4,12 @@ from uuidfield import UUIDField from django.db import models from juser.models import UserGroup -from jasset.models import Asset, BisGroup +from jasset.models import Asset, AssetGroup class Perm(models.Model): user_group = models.ForeignKey(UserGroup) - asset_group = models.ForeignKey(BisGroup) + asset_group = models.ForeignKey(AssetGroup) def __unicode__(self): return '%s_%s' % (self.user_group.name, self.asset_group.name) @@ -27,7 +27,7 @@ class CmdGroup(models.Model): class SudoPerm(models.Model): user_group = models.ForeignKey(UserGroup) user_runas = models.CharField(max_length=100) - asset_group = models.ManyToManyField(BisGroup) + asset_group = models.ManyToManyField(AssetGroup) cmd_group = models.ManyToManyField(CmdGroup) comment = models.CharField(max_length=30, null=True, blank=True) diff --git a/jumpserver/api.py b/jumpserver/api.py index 72ac4fb1e..59411abe4 100644 --- a/jumpserver/api.py +++ b/jumpserver/api.py @@ -17,7 +17,7 @@ from django.core.paginator import Paginator, EmptyPage, InvalidPage from django.http import HttpResponse, Http404 from django.template import RequestContext from juser.models import User, UserGroup -from jasset.models import Asset, BisGroup, IDC +from jasset.models import Asset, AssetGroup from jlog.models import Log from jasset.models import AssetAlias from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned @@ -177,6 +177,7 @@ def pages(post_objects, request): else: show_end = 0 + # 所有对象, 分页器, 本页对象, 所有页码, 本页页码,是否显示第一页,是否显示最后一页 return post_objects, paginator, page_objects, page_range, current_page, show_first, show_end @@ -429,7 +430,7 @@ class PyCrypt(object): """ return crypt.crypt(password, '$6$%s$' % salt) - def encrypt(self, passwd=None): + def encrypt(self, passwd=None, length=32): """ encrypt gen password 对称加密之加密生成密码 @@ -438,7 +439,6 @@ class PyCrypt(object): passwd = self.random_pass() cryptor = AES.new(self.key, self.mode, b'8122ca7d906ad5e1') - length = 64 try: count = len(passwd) except TypeError: diff --git a/jumpserver/settings.py b/jumpserver/settings.py index 96f1617a8..aeeaec053 100644 --- a/jumpserver/settings.py +++ b/jumpserver/settings.py @@ -56,8 +56,8 @@ INSTALLED_APPS = ( 'django.contrib.humanize', 'jumpserver', 'juser', - # 'jasset', - # 'jperm', + 'jasset', + 'jperm', # 'jlog', ) diff --git a/jumpserver/templatetags/mytags.py b/jumpserver/templatetags/mytags.py index c0d5dedbc..8101bf27b 100644 --- a/jumpserver/templatetags/mytags.py +++ b/jumpserver/templatetags/mytags.py @@ -12,21 +12,28 @@ from jasset.models import AssetAlias register = template.Library() -@register.filter(name='stamp2str') -def stamp2str(value): - try: - return time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(value)) - except AttributeError: - return '0000/00/00 00:00:00' +# @register.filter(name='stamp2str') +# def stamp2str(value): +# try: +# return time.strftime('%Y/%m/%d %H:%M:%S', time.localtime(value)) +# except AttributeError: +# return '0000/00/00 00:00:00' @register.filter(name='int2str') def int2str(value): + """ + int 转换为 str + """ return str(value) @register.filter(name='get_role') def get_role(user_id): + """ + 根据用户id获取用户权限 + """ + user_role = {'SU': u'超级管理员', 'GA': u'组管理员', 'CU': u'普通用户'} user = get_object(User, id=user_id) if user: @@ -35,66 +42,69 @@ def get_role(user_id): return u"普通用户" -@register.filter(name='groups_str') -def groups_str(user_id): - groups = [] - user = User.objects.get(id=user_id) - for group in user.group.all(): - groups.append(group.name) - if len(groups) < 3: - return ' '.join(groups) - else: - return "%s ..." % ' '.join(groups[0:2]) - +# @register.filter(name='groups_str') +# def groups_str(user_id): +# groups = [] +# user = User.objects.get(id=user_id) +# for group in user.group.all(): +# groups.append(group.name) +# if len(groups) < 3: +# return ' '.join(groups) +# else: +# return "%s ..." % ' '.join(groups[0:2]) +# @register.filter(name='group_str2') def groups_str2(group_list): + """ + 将用户组列表转换为str + """ if len(group_list) < 3: return ' '.join([group.name for group in group_list]) else: return '%s ...' % ' '.join([group.name for group in group_list[0:2]]) - -@register.filter(name='group_str2_all') -def group_str2_all(group_list): - group_lis = [] - for i in group_list: - if str(i) != 'ALL': - group_lis.append(i) - if len(group_lis) < 3: - return ' '.join([group.name for group in group_lis]) - else: - return '%s ...' % ' '.join([group.name for group in group_lis[0:2]]) - - -@register.filter(name='group_dept_all') -def group_dept_all(group_list): - group_lis = [] - for i in group_list: - if str(i) != 'ALL': - group_lis.append(i) - return ' '.join([group.name for group in group_lis]) - - -@register.filter(name='group_manage_str') -def group_manage_str(username): - user = User.objects.get(username=username) - group = user.user_group.filter(type='M') - if group: - return group[0].name - else: - return '' - - -@register.filter(name='get_item') -def get_item(dictionary, key): - return dictionary.get(key) - - -@register.filter(name='get_login_type') -def get_login_type(login): - login_types = {'L': 'LDAP', 'M': 'MAP'} - return login_types[login] +# +# @register.filter(name='group_str2_all') +# def group_str2_all(group_list): +# group_lis = [] +# for i in group_list: +# if str(i) != 'ALL': +# group_lis.append(i) +# if len(group_lis) < 3: +# return ' '.join([group.name for group in group_lis]) +# else: +# return '%s ...' % ' '.join([group.name for group in group_lis[0:2]]) +# +# +# @register.filter(name='group_dept_all') +# def group_dept_all(group_list): +# group_lis = [] +# for i in group_list: +# if str(i) != 'ALL': +# group_lis.append(i) +# return ' '.join([group.name for group in group_lis]) +# +# +# @register.filter(name='group_manage_str') +# def group_manage_str(username): +# user = User.objects.get(username=username) +# group = user.user_group.filter(type='M') +# if group: +# return group[0].name +# else: +# return '' +# +# +# @register.filter(name='get_item') +# def get_item(dictionary, key): +# return dictionary.get(key) +# +# +# @register.filter(name='get_login_type') +# def get_login_type(login): +# login_types = {'L': 'LDAP', 'M': 'MAP'} +# return login_types[login] @register.filter(name='bool2str') @@ -105,169 +115,171 @@ def bool2str(value): return u'否' -# @register.filter(name='user_readonly') -# def user_readonly(user_id): -# user = User.objects.filter(id=user_id) -# if user: -# user = user[0] -# if user.role == 'CU': -# return False -# return True -# +# # @register.filter(name='user_readonly') +# # def user_readonly(user_id): +# # user = User.objects.filter(id=user_id) +# # if user: +# # user = user[0] +# # if user.role == 'CU': +# # return False +# # return True + @register.filter(name='members_count') def members_count(group_id): + """统计用户组下成员数量""" group = get_object(UserGroup, id=group_id) if group: return group.user_set.count() else: return 0 - -@register.filter(name='group_user_count') -def group_user_count(group_id): - group = UserGroup.objects.get(id=group_id) - return group.user_set.count() - - -@register.filter(name='dept_user_num') -def dept_user_num(dept_id): - dept = DEPT.objects.filter(id=dept_id) - if dept: - dept = dept[0] - return dept.user_set.count() - else: - return 0 - - -@register.filter(name='dept_group_num') -def dept_group_num(dept_id): - dept = DEPT.objects.filter(id=dept_id) - if dept: - dept = dept[0] - return dept.usergroup_set.all().count() - else: - return 0 - - -@register.filter(name='perm_count') -def perm_count(group_id): - group = UserGroup.objects.get(id=group_id) - return group.perm_set.count() - - -@register.filter(name='dept_asset_num') -def dept_asset_num(dept_id): - dept = DEPT.objects.filter(id=dept_id) - if dept: - dept = dept[0] - return dept.asset_set.all().count() - return 0 - - -@register.filter(name='ugrp_perm_agrp_count') -def ugrp_perm_agrp_count(user_group_id): - user_group = UserGroup.objects.filter(id=user_group_id) - if user_group: - user_group = user_group[0] - return user_group.perm_set.all().count() - return 0 - - -@register.filter(name='ugrp_sudo_agrp_count') -def ugrp_sudo_agrp_count(user_group_id): - user_group = UserGroup.objects.filter(id=user_group_id) - asset_groups = [] - if user_group: - user_group = user_group[0] - for perm in user_group.sudoperm_set.all(): - asset_groups.extend(perm.asset_group.all()) - return len(set(asset_groups)) - return 0 - - -@register.filter(name='ugrp_perm_asset_count') -def ugrp_perm_asset_count(user_group_id): - user_group = UserGroup.objects.filter(id=user_group_id) - assets = [] - if user_group: - user_group = user_group[0] - asset_groups = [perm.asset_group for perm in user_group.perm_set.all()] - for asset_group in asset_groups: - assets.extend(asset_group.asset_set.all()) - return len(set(assets)) - - -@register.filter(name='ugrp_sudo_asset_count') -def ugrp_sudo_asset_count(user_group_id): - user_group = UserGroup.objects.filter(id=user_group_id) - asset_groups = [] - assets = [] - if user_group: - user_group = user_group[0] - for perm in user_group.sudoperm_set.all(): - asset_groups.extend(perm.asset_group.all()) - - for asset_group in asset_groups: - assets.extend(asset_group.asset_set.all()) - return len(set(assets)) - - -@register.filter(name='get_user_alias') -def get_user_alias(post, user_id): - user = User.objects.get(id=user_id) - host = Asset.objects.get(id=post.id) - alias = AssetAlias.objects.filter(user=user, host=host) - if alias: - return alias[0].alias - else: - return '' - - -@register.filter(name='group_type_to_str') -def group_type_to_str(type_name): - group_types = { - 'P': '用户', - 'M': '部门', - 'A': '用户组', - } - return group_types.get(type_name) - - -@register.filter(name='ast_to_list') -def ast_to_list(lis): - ast_lis = ast.literal_eval(lis) - if len(ast_lis) <= 2: - return ','.join([i for i in ast_lis]) - else: - restr = ','.join([i for i in ast_lis[0:2]]) + '...' - return restr - - -@register.filter(name='get_group_count') -def get_group_count(post, dept): - count = post.asset_set.filter(dept=dept).count() - return count - - -@register.filter(name='get_idc_count') -def get_idc_count(post, dept): - count = post.asset_set.filter(dept=dept).count() - return count - - -@register.filter(name='ast_to_list_1') -def ast_to_list_1(lis): - return ast.literal_eval(lis) - - -@register.filter(name='string_length') -def string_length(string, length): - return '%s ...' % string[0:length] +# +# @register.filter(name='group_user_count') +# def group_user_count(group_id): +# group = UserGroup.objects.get(id=group_id) +# return group.user_set.count() +# +# +# @register.filter(name='dept_user_num') +# def dept_user_num(dept_id): +# dept = DEPT.objects.filter(id=dept_id) +# if dept: +# dept = dept[0] +# return dept.user_set.count() +# else: +# return 0 +# +# +# @register.filter(name='dept_group_num') +# def dept_group_num(dept_id): +# dept = DEPT.objects.filter(id=dept_id) +# if dept: +# dept = dept[0] +# return dept.usergroup_set.all().count() +# else: +# return 0 +# +# +# @register.filter(name='perm_count') +# def perm_count(group_id): +# group = UserGroup.objects.get(id=group_id) +# return group.perm_set.count() +# +# +# @register.filter(name='dept_asset_num') +# def dept_asset_num(dept_id): +# dept = DEPT.objects.filter(id=dept_id) +# if dept: +# dept = dept[0] +# return dept.asset_set.all().count() +# return 0 +# +# +# @register.filter(name='ugrp_perm_agrp_count') +# def ugrp_perm_agrp_count(user_group_id): +# user_group = UserGroup.objects.filter(id=user_group_id) +# if user_group: +# user_group = user_group[0] +# return user_group.perm_set.all().count() +# return 0 +# +# +# @register.filter(name='ugrp_sudo_agrp_count') +# def ugrp_sudo_agrp_count(user_group_id): +# user_group = UserGroup.objects.filter(id=user_group_id) +# asset_groups = [] +# if user_group: +# user_group = user_group[0] +# for perm in user_group.sudoperm_set.all(): +# asset_groups.extend(perm.asset_group.all()) +# return len(set(asset_groups)) +# return 0 +# +# +# @register.filter(name='ugrp_perm_asset_count') +# def ugrp_perm_asset_count(user_group_id): +# user_group = UserGroup.objects.filter(id=user_group_id) +# assets = [] +# if user_group: +# user_group = user_group[0] +# asset_groups = [perm.asset_group for perm in user_group.perm_set.all()] +# for asset_group in asset_groups: +# assets.extend(asset_group.asset_set.all()) +# return len(set(assets)) +# +# +# @register.filter(name='ugrp_sudo_asset_count') +# def ugrp_sudo_asset_count(user_group_id): +# user_group = UserGroup.objects.filter(id=user_group_id) +# asset_groups = [] +# assets = [] +# if user_group: +# user_group = user_group[0] +# for perm in user_group.sudoperm_set.all(): +# asset_groups.extend(perm.asset_group.all()) +# +# for asset_group in asset_groups: +# assets.extend(asset_group.asset_set.all()) +# return len(set(assets)) +# +# +# @register.filter(name='get_user_alias') +# def get_user_alias(post, user_id): +# user = User.objects.get(id=user_id) +# host = Asset.objects.get(id=post.id) +# alias = AssetAlias.objects.filter(user=user, host=host) +# if alias: +# return alias[0].alias +# else: +# return '' +# +# +# @register.filter(name='group_type_to_str') +# def group_type_to_str(type_name): +# group_types = { +# 'P': '用户', +# 'M': '部门', +# 'A': '用户组', +# } +# return group_types.get(type_name) +# +# +# @register.filter(name='ast_to_list') +# def ast_to_list(lis): +# ast_lis = ast.literal_eval(lis) +# if len(ast_lis) <= 2: +# return ','.join([i for i in ast_lis]) +# else: +# restr = ','.join([i for i in ast_lis[0:2]]) + '...' +# return restr +# +# +# @register.filter(name='get_group_count') +# def get_group_count(post, dept): +# count = post.asset_set.filter(dept=dept).count() +# return count +# +# +# @register.filter(name='get_idc_count') +# def get_idc_count(post, dept): +# count = post.asset_set.filter(dept=dept).count() +# return count +# +# +# @register.filter(name='ast_to_list_1') +# def ast_to_list_1(lis): +# return ast.literal_eval(lis) +# +# +# @register.filter(name='string_length') +# def string_length(string, length): +# return '%s ...' % string[0:length] @register.filter(name='to_name') def to_name(user_id): + """user id 转位用户名称""" try: user = User.objects.filter(id=int(user_id)) if user: @@ -277,120 +289,122 @@ def to_name(user_id): return '非法用户' -@register.filter(name='to_dept_name') -def to_dept_name(user_id): - try: - user = User.objects.filter(id=int(user_id)) - if user: - user = user[0] - return user.dept.name - except: - return '非法部门' +# @register.filter(name='to_dept_name') +# def to_dept_name(user_id): +# try: +# user = User.objects.filter(id=int(user_id)) +# if user: +# user = user[0] +# return user.dept.name +# except: +# return '非法部门' @register.filter(name='to_role_name') def to_role_name(role_id): + """role_id 转变为角色名称""" role_dict = {'0': '普通用户', '1': '组管理员', '2': '超级管理员'} return role_dict.get(str(role_id), '未知') @register.filter(name='to_avatar') def to_avatar(role_id='0'): + """不同角色不同头像""" role_dict = {'0': 'user', '1': 'admin', '2': 'root'} return role_dict.get(str(role_id), 'user') - - -@register.filter(name='get_user_asset_group') -def get_user_asset_group(user): - return user.get_asset_group() - - -@register.filter(name='group_asset_list') -def group_asset_list(group): - return group.asset_set.all() - - -@register.filter(name='group_asset_list_count') -def group_asset_list_count(group): - return group.asset_set.all().count() - - -@register.filter(name='time_delta') -def time_delta(time_before): - delta = datetime.datetime.now() - time_before - days = delta.days - if days: - return "%s 天前" % days - else: - hours = delta.seconds/3600 - if hours: - return "%s 小时前" % hours - else: - mins = delta.seconds/60 - if mins: - return '%s 分钟前' % mins - else: - return '%s 秒前' % delta.seconds - - -@register.filter(name='sudo_cmd_list') -def sudo_cmd_list(cmd_group_id): - cmd_group = CmdGroup.objects.filter(id=cmd_group_id) - if cmd_group: - cmd_group = cmd_group[0] - return cmd_group.cmd.split(',') - - -@register.filter(name='sudo_cmd_count') -def sudo_cmd_count(user_group_id): - user_group = UserGroup.objects.filter(id=user_group_id) - cmds = [] - if user_group: - user_group = user_group[0] - cmd_groups = [] - - for perm in user_group.sudoperm_set.all(): - cmd_groups.extend(perm.cmd_group.all()) - - for cmd_group in cmd_groups: - cmds.extend(cmd_group.cmd.split(',')) - return len(set(cmds)) - - else: - return 0 - - -@register.filter(name='sudo_cmd_count') -def sudo_cmd_count(user_group_id): - user_group = UserGroup.objects.filter(id=user_group_id) - cmds = [] - if user_group: - user_group = user_group[0] - cmd_groups = [] - for perm in user_group.sudoperm_set.all(): - cmd_groups.extend(perm.cmd_group.all()) - - for cmd_group in cmd_groups: - cmds.extend(cmd_group.cmd.split(',')) - return len(set(cmds)) - else: - return 0 - - -@register.filter(name='sudo_cmd_ids') -def sudo_cmd_ids(user_group_id): - user_group = UserGroup.objects.filter(id=user_group_id) - if user_group: - user_group = user_group[0] - cmd_groups = [] - for perm in user_group.sudoperm_set.all(): - cmd_groups.extend(perm.cmd_group.all()) - cmd_ids = [str(cmd_group.id) for cmd_group in cmd_groups] - return ','.join(cmd_ids) - else: - return '0' - - -@register.filter(name='cmd_group_split') -def cmd_group_split(cmd_group): - return cmd_group.cmd.split(',') +# +# +# @register.filter(name='get_user_asset_group') +# def get_user_asset_group(user): +# return user.get_asset_group() +# +# +# @register.filter(name='group_asset_list') +# def group_asset_list(group): +# return group.asset_set.all() +# +# +# @register.filter(name='group_asset_list_count') +# def group_asset_list_count(group): +# return group.asset_set.all().count() +# +# +# @register.filter(name='time_delta') +# def time_delta(time_before): +# delta = datetime.datetime.now() - time_before +# days = delta.days +# if days: +# return "%s 天前" % days +# else: +# hours = delta.seconds/3600 +# if hours: +# return "%s 小时前" % hours +# else: +# mins = delta.seconds/60 +# if mins: +# return '%s 分钟前' % mins +# else: +# return '%s 秒前' % delta.seconds +# +# +# @register.filter(name='sudo_cmd_list') +# def sudo_cmd_list(cmd_group_id): +# cmd_group = CmdGroup.objects.filter(id=cmd_group_id) +# if cmd_group: +# cmd_group = cmd_group[0] +# return cmd_group.cmd.split(',') +# +# +# @register.filter(name='sudo_cmd_count') +# def sudo_cmd_count(user_group_id): +# user_group = UserGroup.objects.filter(id=user_group_id) +# cmds = [] +# if user_group: +# user_group = user_group[0] +# cmd_groups = [] +# +# for perm in user_group.sudoperm_set.all(): +# cmd_groups.extend(perm.cmd_group.all()) +# +# for cmd_group in cmd_groups: +# cmds.extend(cmd_group.cmd.split(',')) +# return len(set(cmds)) +# +# else: +# return 0 +# +# +# @register.filter(name='sudo_cmd_count') +# def sudo_cmd_count(user_group_id): +# user_group = UserGroup.objects.filter(id=user_group_id) +# cmds = [] +# if user_group: +# user_group = user_group[0] +# cmd_groups = [] +# for perm in user_group.sudoperm_set.all(): +# cmd_groups.extend(perm.cmd_group.all()) +# +# for cmd_group in cmd_groups: +# cmds.extend(cmd_group.cmd.split(',')) +# return len(set(cmds)) +# else: +# return 0 +# +# +# @register.filter(name='sudo_cmd_ids') +# def sudo_cmd_ids(user_group_id): +# user_group = UserGroup.objects.filter(id=user_group_id) +# if user_group: +# user_group = user_group[0] +# cmd_groups = [] +# for perm in user_group.sudoperm_set.all(): +# cmd_groups.extend(perm.cmd_group.all()) +# cmd_ids = [str(cmd_group.id) for cmd_group in cmd_groups] +# return ','.join(cmd_ids) +# else: +# return '0' +# +# +# @register.filter(name='cmd_group_split') +# def cmd_group_split(cmd_group): +# return cmd_group.cmd.split(',') diff --git a/jumpserver/urls.py b/jumpserver/urls.py index c7a3eaf31..f6db9f3ad 100644 --- a/jumpserver/urls.py +++ b/jumpserver/urls.py @@ -14,7 +14,7 @@ urlpatterns = patterns('', (r'^file/download/$', 'jumpserver.views.download'), (r'^error/$', 'jumpserver.views.httperror'), (r'^juser/', include('juser.urls')), - # (r'^jasset/', include('jasset.urls')), + (r'^jasset/', include('jasset.urls')), # (r'^jlog/', include('jlog.urls')), # (r'^jperm/', include('jperm.urls')), (r'^node_auth/', 'jumpserver.views.node_auth'), diff --git a/juser/views.py b/juser/views.py index 833500b33..2c8cd50de 100644 --- a/juser/views.py +++ b/juser/views.py @@ -71,7 +71,7 @@ def group_list(request): if keyword: user_group_list = user_group_list.filter(Q(name__icontains=keyword) | Q(comment__icontains=keyword)) - contacts, p, contacts, page_range, current_page, show_first, show_end = pages(user_group_list, request) + user_group_list, p, user_groups, page_range, current_page, show_first, show_end = pages(user_group_list, request) return my_render('juser/group_list.html', locals(), request) @@ -356,18 +356,18 @@ def user_list(request): header_title, path1, path2 = '查看用户', '用户管理', '用户列表' keyword = request.GET.get('keyword', '') gid = request.GET.get('gid', '') - contact_list = User.objects.all().order_by('name') + users_list = User.objects.all().order_by('username') if gid: user_group = UserGroup.objects.filter(id=gid) if user_group: user_group = user_group[0] - contact_list = user_group.user_set.all() + users_list = user_group.user_set.all() if keyword: - contact_list = contact_list.filter(Q(username__icontains=keyword) | Q(name__icontains=keyword)).order_by('name') + users_list = users_list.filter(Q(username__icontains=keyword) | Q(name__icontains=keyword)).order_by('username') - contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(contact_list, request) + users_list, p, users, page_range, current_page, show_first, show_end = pages(users_list, request) return my_render('juser/user_list.html', locals(), request) @@ -568,8 +568,6 @@ def user_edit(request): role=role_post, is_active=is_active) - print '#'* 10 + role_post - if email_need: msg = u""" Hi %s: @@ -669,7 +667,7 @@ def change_info(request): if '' in [name, password, email]: error = '不能为空' - if len(password) < 6 : + if len(password) < 6: error = '密码须大于6位' if not error: diff --git a/static/js/base.js b/static/js/base.js index 9324ae0a6..0c5612a0d 100644 --- a/static/js/base.js +++ b/static/js/base.js @@ -70,17 +70,17 @@ function move(from, to, from_o, to_o) { }); } -function move_left(from, to, from_o, to_o) { - $("#" + from + " option").each(function () { - if ($(this).prop("selected") == true) { - $("#" + to).append(this); - if( typeof from_o !== 'undefined'){ - $("#"+to_o).append($("#"+from_o +" option[value='"+this.value+"']")); - } - } - $(this).attr("selected",'true'); - }); -} +//function move_left(from, to, from_o, to_o) { +// $("#" + from + " option").each(function () { +// if ($(this).prop("selected") == true) { +// $("#" + to).append(this); +// if( typeof from_o !== 'undefined'){ +// $("#"+to_o).append($("#"+from_o +" option[value='"+this.value+"']")); +// } +// } +// $(this).attr("selected",'true'); +// }); +//} //function move_all(from, to) { // $("#" + from).children().each(function () { diff --git a/templates/index.html b/templates/index.html index 3b8312182..e7634d15b 100644 --- a/templates/index.html +++ b/templates/index.html @@ -114,8 +114,8 @@ {{ perm.date_add|naturaltime }} {% endifequal %} {{ perm.applyer }} -
申请 {{ perm.bisgroup|ast_to_list }} 主机组权限
-
申请 {{ perm.asset|ast_to_list }} 主机权限
+{#
申请 {{ perm.bisgroup|ast_to_list }} 主机组权限
#} +{#
申请 {{ perm.asset|ast_to_list }} 主机权限
#} {{ perm.date_add }} diff --git a/templates/jasset/host_add.html b/templates/jasset/asset_add.html similarity index 50% rename from templates/jasset/host_add.html rename to templates/jasset/asset_add.html index bbfa730b5..fe6d72f5a 100644 --- a/templates/jasset/host_add.html +++ b/templates/jasset/asset_add.html @@ -7,7 +7,7 @@
-
填写主机基本信息
+
填写资产基本信息
@@ -15,12 +15,6 @@ - @@ -31,87 +25,61 @@
- {% if emg %} -
{{ emg }}
+ {% if error %} +
{{ error }}
{% endif %} - {% if smg %} -
{{ smg }}
+ {% if msg %} +
{{ msg }}
{% endif %}
-
+
-
-
-
- -
-
+
+
-
-
-
- -
- -
- + +
+
+ +
- {% ifequal session_role_id 2 %} -
-
- -
- -
+ -
-
- - - -{% endblock %} \ No newline at end of file diff --git a/templates/jasset/idc_edit.html b/templates/jasset/idc_edit.html deleted file mode 100644 index 0751a4114..000000000 --- a/templates/jasset/idc_edit.html +++ /dev/null @@ -1,109 +0,0 @@ -{% extends 'base.html' %} -{% block content %} -{% include 'nav_cat_bar.html' %} -
-
-
-
-
-
填写IDC基本信息
- -
- -
- {% if emg %} -
{{ emg }}
- {% endif %} - {% if smg %} -
{{ smg }}
- {% endif %} -
-
-
- - -
-
- -
-
-
-
- -
-
- -
- -
-
-
- - -
-
-
-
- -
-
-
- -
-
-
- - -
-
-
-
-
-
-
-
- - - -{% endblock %} \ No newline at end of file diff --git a/templates/jasset/idc_list.html b/templates/jasset/idc_list.html deleted file mode 100644 index e1f5dd465..000000000 --- a/templates/jasset/idc_list.html +++ /dev/null @@ -1,125 +0,0 @@ -{% extends 'base.html' %} -{% load mytags %} -{% block content %} -{% include 'nav_cat_bar.html' %} - -
-
-
-
-
-
IDC详细信息列表
- -
-
-
- {% ifequal session_role_id 2 %} - 添加IDC - {% endifequal %} - -
- -
- - - - {% ifequal session_role_id 2 %} - - {% endifequal %} - - - - - - - - {% for post in contacts.object_list %} - - {% ifequal session_role_id 2 %} - - {% endifequal %} - - {% ifequal session_role_id 2 %} - - {% else %} - - {% endifequal %} - - - - {% endfor %} - -
机房名 主机数量 备注 操作
{{ post.name }} {{ post.asset_set.count }} {{ post|get_idc_count:dept }} {{ post.comment }} - 详情 - {% ifequal session_role_id 2 %} - 编辑 - 删除 - {% endifequal %} -
-
-
- {% ifequal session_role_id 2 %} - - - {% endifequal %} -
- {% include 'paginator.html' %} -
-
-
-
-
-
-
- - - -{% endblock %} \ No newline at end of file diff --git a/templates/juser/group_list.html b/templates/juser/group_list.html index 4085685c9..0755016fd 100644 --- a/templates/juser/group_list.html +++ b/templates/juser/group_list.html @@ -52,7 +52,7 @@ - {% for group in contacts.object_list %} + {% for group in user_groups.object_list %} diff --git a/templates/juser/user_detail.html b/templates/juser/user_detail.html index 88b284c4f..479dd991d 100644 --- a/templates/juser/user_detail.html +++ b/templates/juser/user_detail.html @@ -109,30 +109,30 @@ 这里包含了用户所有的主机组和组下的主机.
- {% for group in user|get_user_asset_group %} -
-
-
- - {{ group.name }} -
- 共: {{ group | group_asset_list_count }}台 -
-
-

{{ group.comment }}

-

- {% for asset in group|group_asset_list %} - {{ asset.ip }}
- {% endfor %} -

-

-
-
-
- {% endfor %} - {% if not user|get_user_asset_group %} - (无) - {% endif %} +{# {% for group in user|get_user_asset_group %}#} +{#
#} +{#
#} +{#
#} +{# #} +{# {{ group.name }}#} +{#
#} +{# 共: {{ group | group_asset_list_count }}台#} +{#
#} +{#
#} +{#

{{ group.comment }}

#} +{#

#} +{# {% for asset in group|group_asset_list %}#} +{# {{ asset.ip }}
#} +{# {% endfor %}#} +{#

#} +{#

#} +{#
#} +{#
#} +{#
#} +{# {% endfor %}#} +{# {% if not user|get_user_asset_group %}#} +{# (无)#} +{# {% endif %}#}
@@ -154,7 +154,8 @@ image
- {{ log.start_time|time_delta }} +{# {{ log.start_time|time_delta }}#} + {{ log.start_time }} {{ log.user }} 登录了 {{ log.host }}.
{{ log.start_time|date:"Y-m-d H:i:s" }}
@@ -177,21 +178,18 @@ image
- {{ log.start_time|time_delta }} + {{ log.start_time }} +{# {{ log.start_time|time_delta }}#} {{ log.user }} 登录了 {{ log.host }}.
{{ log.start_time|date:"Y-m-d H:i:s" }}
{% endfor %}
-
-
-
- diff --git a/templates/juser/user_list.html b/templates/juser/user_list.html index b470c5e84..32e940868 100644 --- a/templates/juser/user_list.html +++ b/templates/juser/user_list.html @@ -54,7 +54,7 @@ - {% for user in contacts.object_list %} + {% for user in users.object_list %} @@ -63,7 +63,7 @@ {{ user.name }} {{ user.group.all | group_str2 }} {{ user.id | get_role }} - {{ user.is_active|bool2str }} + {{ user.is_active | bool2str }} 下载 详情 @@ -78,7 +78,7 @@
- Showing {{ contacts.start_index }} to {{ contacts.end_index }} of {{ p.count }} entries + Showing {{ users.start_index }} to {{ users.end_index }} of {{ p.count }} entries
{% include 'paginator.html' %} @@ -93,25 +93,27 @@ {% block self_head_css_js %}