From ce530e417ccb7942eb2533ccf9e6f91b74d96df1 Mon Sep 17 00:00:00 2001 From: ibuler Date: Wed, 9 Oct 2019 19:10:36 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20=E4=BF=AE=E6=94=B9cmd=20filter=20rul?= =?UTF-8?q?es=20pattern?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/assets/models/cmd_filter.py | 28 +++++++++++-------- .../ops/command_execution_create.html | 3 +- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/apps/assets/models/cmd_filter.py b/apps/assets/models/cmd_filter.py index 38776ff38..91febd40b 100644 --- a/apps/assets/models/cmd_filter.py +++ b/apps/assets/models/cmd_filter.py @@ -7,6 +7,7 @@ from django.db import models from django.core.validators import MinValueValidator, MaxValueValidator from django.utils.translation import ugettext_lazy as _ +from common.utils import lazyproperty from orgs.mixins.models import OrgModelMixin @@ -57,25 +58,30 @@ class CommandFilterRule(OrgModelMixin): date_updated = models.DateTimeField(auto_now=True) created_by = models.CharField(max_length=128, blank=True, default='', verbose_name=_('Created by')) - __pattern = None - class Meta: ordering = ('-priority', 'action') verbose_name = _("Command filter rule") - @property + @lazyproperty def _pattern(self): - if self.__pattern: - return self.__pattern if self.type == 'command': regex = [] - for cmd in self.content.split('\r\n'): - cmd = cmd.replace(' ', '\s+') - regex.append(r'\b{0}\b'.format(cmd)) - self.__pattern = re.compile(r'{}'.format('|'.join(regex))) + content = self.content.replace('\r\n', '\n') + for cmd in content.split('\n'): + cmd = re.escape(cmd) + cmd = cmd.replace('\\ ', '\s+') + if cmd[-1].isalpha(): + regex.append(r'\b{0}\b'.format(cmd)) + else: + regex.append(r'\b{0}'.format(cmd)) + s = r'{}'.format('|'.join(regex)) else: - self.__pattern = re.compile(r'{0}'.format(self.content)) - return self.__pattern + s = r'{0}'.format(self.content) + try: + _pattern = re.compile(s) + except: + _pattern = '' + return _pattern def match(self, data): found = self._pattern.search(data) diff --git a/apps/ops/templates/ops/command_execution_create.html b/apps/ops/templates/ops/command_execution_create.html index 19936154f..e0e96f267 100644 --- a/apps/ops/templates/ops/command_execution_create.html +++ b/apps/ops/templates/ops/command_execution_create.html @@ -140,6 +140,7 @@ } function initTree() { + $('#assetTree').html("{% trans 'Loading' %}" + '..'); if (systemUserId) { url = treeUrl + '&system_user=' + systemUserId } else { @@ -365,4 +366,4 @@ execute() }) -{% endblock %} \ No newline at end of file +{% endblock %}