From 70fa43adaa0b5ebc8bb4b0ca8ce5eb32f7ab251b Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 10 Oct 2018 19:29:53 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20=E4=BF=AE=E6=94=B9=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/forms/cmd_filter.py | 7 +++++++ apps/assets/models/cmd_filter.py | 10 ++++++---- apps/assets/models/node.py | 10 ++++------ apps/assets/models/user.py | 2 +- .../templates/assets/cmd_filter_list.html | 2 +- .../assets/cmd_filter_rule_create_update.html | 17 +++++++++++++++++ .../templates/assets/system_user_asset.html | 3 ++- apps/assets/views/system_user.py | 3 ++- apps/common/templatetags/common_tags.py | 6 ++++++ apps/locale/zh/LC_MESSAGES/django.mo | Bin 51572 -> 51572 bytes apps/locale/zh/LC_MESSAGES/django.po | 2 +- 11 files changed, 47 insertions(+), 15 deletions(-) diff --git a/apps/assets/forms/cmd_filter.py b/apps/assets/forms/cmd_filter.py index ebac4fee2..6cb35e631 100644 --- a/apps/assets/forms/cmd_filter.py +++ b/apps/assets/forms/cmd_filter.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- # +from django import forms + from orgs.mixins import OrgModelForm from ..models import CommandFilter, CommandFilterRule @@ -18,3 +20,8 @@ class CommandFilterRuleForm(OrgModelForm): fields = [ 'filter', 'type', 'content', 'priority', 'action', 'comment' ] + widgets = { + 'content': forms.Textarea(attrs={ + 'placeholder': 'eg:\r\nreboot\r\nrm -rf' + }), + } diff --git a/apps/assets/models/cmd_filter.py b/apps/assets/models/cmd_filter.py index b06bed5c6..e214073c6 100644 --- a/apps/assets/models/cmd_filter.py +++ b/apps/assets/models/cmd_filter.py @@ -35,11 +35,10 @@ class CommandFilterRule(OrgModelMixin): (TYPE_COMMAND, _('Command')), ) - ACTION_DENY = 'deny' - ACTION_ACCEPT = 'accept' + ACTION_DENY, ACTION_ALLOW = range(2) ACTION_CHOICES = ( (ACTION_DENY, _('Deny')), - (ACTION_ACCEPT, _('Accept')) + (ACTION_ALLOW, _('Allow')), ) id = models.UUIDField(default=uuid.uuid4, primary_key=True) @@ -47,11 +46,14 @@ class CommandFilterRule(OrgModelMixin): type = models.CharField(max_length=16, default=TYPE_COMMAND, choices=TYPE_CHOICES, verbose_name=_("Type")) priority = models.IntegerField(default=50, verbose_name=_("Priority"), validators=[MinValueValidator(1), MaxValueValidator(100)]) content = models.TextField(max_length=1024, verbose_name=_("Content"), help_text=_("One line one command")) - action = models.CharField(max_length=16, default=ACTION_DENY, choices=ACTION_CHOICES, verbose_name=_("Action")) + action = models.IntegerField(default=ACTION_DENY, choices=ACTION_CHOICES, verbose_name=_("Action")) comment = models.CharField(max_length=64, blank=True, default='', verbose_name=_("Comment")) date_created = models.DateTimeField(auto_now_add=True) date_updated = models.DateTimeField(auto_now=True) created_by = models.CharField(max_length=128, blank=True, default='', verbose_name=_('Created by')) + class Meta: + ordering = ('priority', 'action') + def __str__(self): return '{} % {}'.format(self.type, self.content) diff --git a/apps/assets/models/node.py b/apps/assets/models/node.py index 517996086..54cf77364 100644 --- a/apps/assets/models/node.py +++ b/apps/assets/models/node.py @@ -38,12 +38,10 @@ class Node(OrgModelMixin): return True self_key = [int(k) for k in self.key.split(':')] other_key = [int(k) for k in other.key.split(':')] - if len(self_key) < len(other_key): - return True - elif len(self_key) > len(other_key): - return False - else: - return self_key[-1] < other_key[-1] + return self_key.__lt__(other_key) + + def __lt__(self, other): + return not self.__gt__(other) @property def name(self): diff --git a/apps/assets/models/user.py b/apps/assets/models/user.py index 338f861ea..cd90d4207 100644 --- a/apps/assets/models/user.py +++ b/apps/assets/models/user.py @@ -117,7 +117,7 @@ class SystemUser(AssetUser): sudo = models.TextField(default='/bin/whoami', verbose_name=_('Sudo')) shell = models.CharField(max_length=64, default='/bin/bash', verbose_name=_('Shell')) login_mode = models.CharField(choices=LOGIN_MODE_CHOICES, default=AUTO_LOGIN, max_length=10, verbose_name=_('Login mode')) - cmd_filters = models.ManyToManyField('CommandFilter', related_name='system_users', verbose_name=_("Command filter")) + cmd_filters = models.ManyToManyField('CommandFilter', related_name='system_users', verbose_name=_("Command filter"), blank=True) cache_key = "__SYSTEM_USER_CACHED_{}" diff --git a/apps/assets/templates/assets/cmd_filter_list.html b/apps/assets/templates/assets/cmd_filter_list.html index 4bc33cf02..4887d609e 100644 --- a/apps/assets/templates/assets/cmd_filter_list.html +++ b/apps/assets/templates/assets/cmd_filter_list.html @@ -66,7 +66,7 @@ $(document).ready(function(){ var $data_table = $('#cmd_filter_list_table').DataTable(); var name = $(this).closest("tr").find(":nth-child(2)").children('a').html(); var uid = $this.data('uid'); - var the_url = '{% url "api-assets:label-detail" pk=DEFAULT_PK %}'.replace('{{ DEFAULT_PK }}', uid); + var the_url = '{% url "api-assets:cmd-filter-detail" pk=DEFAULT_PK %}'.replace('{{ DEFAULT_PK }}', uid); objectDelete($this, name, the_url); setTimeout( function () { $data_table.ajax.reload(); diff --git a/apps/assets/templates/assets/cmd_filter_rule_create_update.html b/apps/assets/templates/assets/cmd_filter_rule_create_update.html index bbb02f48c..9b240bd74 100644 --- a/apps/assets/templates/assets/cmd_filter_rule_create_update.html +++ b/apps/assets/templates/assets/cmd_filter_rule_create_update.html @@ -51,7 +51,24 @@ {% block custom_foot_js %} {% endblock %} \ No newline at end of file diff --git a/apps/assets/templates/assets/system_user_asset.html b/apps/assets/templates/assets/system_user_asset.html index a7d5bf50b..bf778d46f 100644 --- a/apps/assets/templates/assets/system_user_asset.html +++ b/apps/assets/templates/assets/system_user_asset.html @@ -1,4 +1,5 @@ {% extends 'base.html' %} +{% load common_tags %} {% load static %} {% load i18n %} @@ -113,7 +114,7 @@ - {% for node in system_user.nodes.all %} + {% for node in system_user.nodes.all|sort %} {{ node }} diff --git a/apps/assets/views/system_user.py b/apps/assets/views/system_user.py index c8c6ea6be..c31fd8da0 100644 --- a/apps/assets/views/system_user.py +++ b/apps/assets/views/system_user.py @@ -91,10 +91,11 @@ class SystemUserAssetView(AdminUserRequiredMixin, DetailView): context_object_name = 'system_user' def get_context_data(self, **kwargs): + nodes_remain = sorted(Node.objects.exclude(systemuser=self.object), reverse=True) context = { 'app': _('assets'), 'action': _('System user asset'), - 'nodes_remain': Node.objects.exclude(systemuser=self.object) + 'nodes_remain': nodes_remain } kwargs.update(context) return super().get_context_data(**kwargs) diff --git a/apps/common/templatetags/common_tags.py b/apps/common/templatetags/common_tags.py index 9123ecef8..7cafb0460 100644 --- a/apps/common/templatetags/common_tags.py +++ b/apps/common/templatetags/common_tags.py @@ -100,3 +100,9 @@ def is_bool_field(field): @register.filter def to_dict(data): return dict(data) + + +@register.filter +def sort(data): + print(data) + return sorted(data) diff --git a/apps/locale/zh/LC_MESSAGES/django.mo b/apps/locale/zh/LC_MESSAGES/django.mo index 4dcc60c68a42ae407f37bf2d39d5d0d818b5a6f9..422c312742c2dee1293b43dd826e86bbb2ef9d98 100644 GIT binary patch delta 21 dcmew|iTTSU<_%ig*q*PQ_I&En%?8_CgaLg33giF) delta 21 dcmew|iTTSU<_%ig*q(N@J>R%{v%xkOVE}wE3f}+# diff --git a/apps/locale/zh/LC_MESSAGES/django.po b/apps/locale/zh/LC_MESSAGES/django.po index 7bd6620c1..89584ac35 100644 --- a/apps/locale/zh/LC_MESSAGES/django.po +++ b/apps/locale/zh/LC_MESSAGES/django.po @@ -1295,7 +1295,7 @@ msgstr "创建规则" #: assets/templates/assets/cmd_filter_rule_list.html:61 msgid "Strategy" -msgstr "分类" +msgstr "策略" #: assets/templates/assets/delete_confirm.html:6 #: perms/templates/perms/delete_confirm.html:6 templates/delete_confirm.html:6