TTY nav sort by ip / hostname / none (#198)

* TTY nav sort by ip / hostname / none

* add newline at end of file
pull/207/head
Astraeux 2016-04-12 11:11:19 +08:00 committed by ibuler
parent 35cc966132
commit 288a42663a
3 changed files with 22 additions and 3 deletions

View File

@ -29,7 +29,7 @@ from django.contrib.sessions.models import Session
from jumpserver.api import ServerError, User, Asset, PermRole, AssetGroup, get_object, mkdir, get_asset_info from jumpserver.api import ServerError, User, Asset, PermRole, AssetGroup, get_object, mkdir, get_asset_info
from jumpserver.api import logger, Log, TtyLog, get_role_key, CRYPTOR, bash, get_tmp_dir from jumpserver.api import logger, Log, TtyLog, get_role_key, CRYPTOR, bash, get_tmp_dir
from jperm.perm_api import gen_resource, get_group_asset_perm, get_group_user_perm, user_have_perm, PermRole from jperm.perm_api import gen_resource, get_group_asset_perm, get_group_user_perm, user_have_perm, PermRole
from jumpserver.settings import LOG_DIR from jumpserver.settings import LOG_DIR, NAV_SORT_BY
from jperm.ansible_api import MyRunner from jperm.ansible_api import MyRunner
# from jlog.log_api import escapeString # from jlog.log_api import escapeString
from jlog.models import ExecLog, FileLog from jlog.models import ExecLog, FileLog
@ -437,11 +437,21 @@ class Nav(object):
def __init__(self, user): def __init__(self, user):
self.user = user self.user = user
self.user_perm = get_group_user_perm(self.user) self.user_perm = get_group_user_perm(self.user)
self.perm_assets = sorted(self.user_perm.get('asset', []).keys(), if NAV_SORT_BY == 'ip':
key=lambda x: [int(num) for num in x.ip.split('.') if num.isdigit()]) self.perm_assets = sorted(self.user_perm.get('asset', []).keys(),
key=lambda x: [int(num) for num in x.ip.split('.') if num.isdigit()])
elif NAV_SORT_BY == 'hostname':
self.perm_assets = self.natural_sort_hostname(self.user_perm.get('asset', []).keys())
else:
self.perm_assets = tuple(self.user_perm.get('asset', []))
self.search_result = self.perm_assets self.search_result = self.perm_assets
self.perm_asset_groups = self.user_perm.get('asset_group', []) self.perm_asset_groups = self.user_perm.get('asset_group', [])
def natural_sort_hostname(self, list):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda x: [ convert(c) for c in re.split('([0-9]+)', x.hostname) ]
return sorted(list, key = alphanum_key)
@staticmethod @staticmethod
def print_nav(): def print_nav():
""" """

View File

@ -20,3 +20,6 @@ email_host_user =
email_host_password = email_host_password =
email_use_tls = True email_use_tls = True
email_use_ssl = False email_use_ssl = False
[connect]
nav_sort_by = ip

View File

@ -49,6 +49,12 @@ LOG_LEVEL = config.get('base', 'log')
IP = config.get('base', 'ip') IP = config.get('base', 'ip')
PORT = config.get('base', 'port') PORT = config.get('base', 'port')
# ======== Connect ==========
try:
NAV_SORT_BY = config.get('connect', 'nav_sort_by')
except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
NAV_SORT_BY = 'ip'
# Quick-start development settings - unsuitable for production # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/