From 00c5b3c0a2df8ef2d1d8d141c3b14f724b3ca1f4 Mon Sep 17 00:00:00 2001 From: feng <1304903146@qq.com> Date: Thu, 28 Mar 2024 12:20:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E3=80=90=E8=B5=84=E4=BA=A7=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E3=80=91=E5=B1=9E=E6=80=A7=E4=B8=BA=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E8=A7=84=E5=88=99=E4=B8=8D=E7=94=9F=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/common/db/fields.py | 102 ++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/apps/common/db/fields.py b/apps/common/db/fields.py index 0a620e1f0..f5b1e4b83 100644 --- a/apps/common/db/fields.py +++ b/apps/common/db/fields.py @@ -469,7 +469,7 @@ class JSONManyToManyDescriptor: rule_match = rule.get('match', 'exact') custom_filter_q = None - spec_attr_filter = getattr(to_model, "get_filter_{}_attr_q".format(rule['name']), None) + spec_attr_filter = getattr(to_model, "get_{}_filter_attr_q".format(rule['name']), None) if spec_attr_filter: custom_filter_q = spec_attr_filter(rule_value, rule_match) elif custom_attr_filter: @@ -478,59 +478,61 @@ class JSONManyToManyDescriptor: custom_q &= custom_filter_q continue - if rule_match == 'in': - res &= value in rule_value or '*' in rule_value - elif rule_match == 'exact': - res &= value == rule_value or rule_value == '*' - elif rule_match == 'contains': - res &= (rule_value in value) - elif rule_match == 'startswith': - res &= str(value).startswith(str(rule_value)) - elif rule_match == 'endswith': - res &= str(value).endswith(str(rule_value)) - elif rule_match == 'regex': - try: - matched = bool(re.search(r'{}'.format(rule_value), value)) - except Exception as e: - logging.error('Error regex match: %s', e) - matched = False - res &= matched - elif rule_match == 'not': - res &= value != rule_value - elif rule['match'] == 'gte': - res &= value >= rule_value - elif rule['match'] == 'lte': - res &= value <= rule_value - elif rule['match'] == 'gt': - res &= value > rule_value - elif rule['match'] == 'lt': - res &= value < rule_value - elif rule['match'] == 'ip_in': - if isinstance(rule_value, str): - rule_value = [rule_value] - res &= '*' in rule_value or contains_ip(value, rule_value) - elif rule['match'].startswith('m2m'): - if isinstance(value, Manager): - value = value.values_list('id', flat=True) - elif isinstance(value, QuerySet): - value = value.values_list('id', flat=True) - elif isinstance(value, models.Model): - value = [value.id] - if isinstance(rule_value, (str, int)): - rule_value = [rule_value] - value = set(map(str, value)) - rule_value = set(map(str, rule_value)) + match rule_match: + case 'in': + res &= value in rule_value or '*' in rule_value + case 'exact': + res &= value == rule_value or rule_value == '*' + case 'contains': + res &= rule_value in value + case 'startswith': + res &= str(value).startswith(str(rule_value)) + case 'endswith': + res &= str(value).endswith(str(rule_value)) + case 'regex': + try: + matched = bool(re.search(r'{}'.format(rule_value), value)) + except Exception as e: + logging.error('Error regex match: %s', e) + matched = False + res &= matched + case 'not': + res &= value != rule_value + case 'gte' | 'lte' | 'gt' | 'lt': + operations = { + 'gte': lambda x, y: x >= y, + 'lte': lambda x, y: x <= y, + 'gt': lambda x, y: x > y, + 'lt': lambda x, y: x < y + } + res &= operations[rule_match](value, rule_value) + case 'ip_in': + if isinstance(rule_value, str): + rule_value = [rule_value] + res &= '*' in rule_value or contains_ip(value, rule_value) + case rule_match if rule_match.startswith('m2m'): + if isinstance(value, Manager): + value = value.values_list('id', flat=True) + elif isinstance(value, QuerySet): + value = value.values_list('id', flat=True) + elif isinstance(value, models.Model): + value = [value.id] + if isinstance(rule_value, (str, int)): + rule_value = [rule_value] + value = set(map(str, value)) + rule_value = set(map(str, rule_value)) - if rule['match'] == 'm2m_all': - res &= rule_value.issubset(value) - else: - res &= bool(value & rule_value) - else: - logging.error("unknown match: {}".format(rule['match'])) - res &= False + if rule['match'] == 'm2m_all': + res &= rule_value.issubset(value) + else: + res &= bool(value & rule_value) + case __: + logging.error("unknown match: {}".format(rule['match'])) + res &= False if not res: return res + if custom_q: res &= to_model.objects.filter(custom_q).filter(id=obj.id).exists() return res