diff --git a/connect.py b/connect.py index e9bcee094..41d7e89bd 100755 --- a/connect.py +++ b/connect.py @@ -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 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 jumpserver.settings import LOG_DIR +from jumpserver.settings import LOG_DIR, NAV_SORT_BY from jperm.ansible_api import MyRunner # from jlog.log_api import escapeString from jlog.models import ExecLog, FileLog @@ -437,11 +437,21 @@ class Nav(object): def __init__(self, user): self.user = user self.user_perm = get_group_user_perm(self.user) - self.perm_assets = sorted(self.user_perm.get('asset', []).keys(), - key=lambda x: [int(num) for num in x.ip.split('.') if num.isdigit()]) + if NAV_SORT_BY == 'ip': + 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.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 def print_nav(): """ diff --git a/jumpserver.conf b/jumpserver.conf index a2b4132d1..28c359c3a 100644 --- a/jumpserver.conf +++ b/jumpserver.conf @@ -20,3 +20,6 @@ email_host_user = email_host_password = email_use_tls = True email_use_ssl = False + +[connect] +nav_sort_by = ip diff --git a/jumpserver/settings.py b/jumpserver/settings.py index d01dcc96a..6f9dc2dc4 100644 --- a/jumpserver/settings.py +++ b/jumpserver/settings.py @@ -49,6 +49,12 @@ LOG_LEVEL = config.get('base', 'log') IP = config.get('base', 'ip') 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 # See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/