fix: 【资产登录】属性为标签时,规则不生效

pull/12890/head
feng 2024-03-28 12:20:09 +08:00 committed by 老广
parent 3aeadc2f03
commit 00c5b3c0a2
1 changed files with 52 additions and 50 deletions

View File

@ -469,7 +469,7 @@ class JSONManyToManyDescriptor:
rule_match = rule.get('match', 'exact') rule_match = rule.get('match', 'exact')
custom_filter_q = None 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: if spec_attr_filter:
custom_filter_q = spec_attr_filter(rule_value, rule_match) custom_filter_q = spec_attr_filter(rule_value, rule_match)
elif custom_attr_filter: elif custom_attr_filter:
@ -478,38 +478,39 @@ class JSONManyToManyDescriptor:
custom_q &= custom_filter_q custom_q &= custom_filter_q
continue continue
if rule_match == 'in': match rule_match:
case 'in':
res &= value in rule_value or '*' in rule_value res &= value in rule_value or '*' in rule_value
elif rule_match == 'exact': case 'exact':
res &= value == rule_value or rule_value == '*' res &= value == rule_value or rule_value == '*'
elif rule_match == 'contains': case 'contains':
res &= (rule_value in value) res &= rule_value in value
elif rule_match == 'startswith': case 'startswith':
res &= str(value).startswith(str(rule_value)) res &= str(value).startswith(str(rule_value))
elif rule_match == 'endswith': case 'endswith':
res &= str(value).endswith(str(rule_value)) res &= str(value).endswith(str(rule_value))
elif rule_match == 'regex': case 'regex':
try: try:
matched = bool(re.search(r'{}'.format(rule_value), value)) matched = bool(re.search(r'{}'.format(rule_value), value))
except Exception as e: except Exception as e:
logging.error('Error regex match: %s', e) logging.error('Error regex match: %s', e)
matched = False matched = False
res &= matched res &= matched
elif rule_match == 'not': case 'not':
res &= value != rule_value res &= value != rule_value
elif rule['match'] == 'gte': case 'gte' | 'lte' | 'gt' | 'lt':
res &= value >= rule_value operations = {
elif rule['match'] == 'lte': 'gte': lambda x, y: x >= y,
res &= value <= rule_value 'lte': lambda x, y: x <= y,
elif rule['match'] == 'gt': 'gt': lambda x, y: x > y,
res &= value > rule_value 'lt': lambda x, y: x < y
elif rule['match'] == 'lt': }
res &= value < rule_value res &= operations[rule_match](value, rule_value)
elif rule['match'] == 'ip_in': case 'ip_in':
if isinstance(rule_value, str): if isinstance(rule_value, str):
rule_value = [rule_value] rule_value = [rule_value]
res &= '*' in rule_value or contains_ip(value, rule_value) res &= '*' in rule_value or contains_ip(value, rule_value)
elif rule['match'].startswith('m2m'): case rule_match if rule_match.startswith('m2m'):
if isinstance(value, Manager): if isinstance(value, Manager):
value = value.values_list('id', flat=True) value = value.values_list('id', flat=True)
elif isinstance(value, QuerySet): elif isinstance(value, QuerySet):
@ -525,12 +526,13 @@ class JSONManyToManyDescriptor:
res &= rule_value.issubset(value) res &= rule_value.issubset(value)
else: else:
res &= bool(value & rule_value) res &= bool(value & rule_value)
else: case __:
logging.error("unknown match: {}".format(rule['match'])) logging.error("unknown match: {}".format(rule['match']))
res &= False res &= False
if not res: if not res:
return res return res
if custom_q: if custom_q:
res &= to_model.objects.filter(custom_q).filter(id=obj.id).exists() res &= to_model.objects.filter(custom_q).filter(id=obj.id).exists()
return res return res