You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Django中实现政策ID四位数字完全匹配的重复检测?

政策提交系统重复检测逻辑修改方案

需求说明

原系统仅通过文件名完全匹配判断政策是否存在,现需调整为:只要政策ID中包含的四位连续数字完全匹配,即使前后缀不同(如GIRL4876S与BOY4876T),也判定为重复政策,提交时需添加对应错误提示。

修改后代码实现

import re

def extract_four_digits(policy_id):
    # 提取Policy ID中的四位连续数字,返回匹配结果或None
    match = re.search(r'\d{4}', policy_id)
    return match.group() if match else None

def policy_exists(policy_id, ref=None):
    target_digits = extract_four_digits(policy_id)
    if not target_digits:
        # 无四位数字时, fallback 到原文件名完全匹配逻辑(可根据业务需求调整)
        filename = policy_id + '.xml'
        try:
            if ref:
                repo.get_contents('policies/' + filename, ref)
            else:
                repo.get_contents('policies/' + filename, prev_release_commit())
            return True
        except UnknownObjectException:
            return False
    
    # 获取指定版本下的所有政策文件
    try:
        if ref:
            policies_dir = repo.get_contents('policies/', ref)
        else:
            policies_dir = repo.get_contents('policies/', prev_release_commit())
    except UnknownObjectException:
        # 政策目录不存在则无重复
        return False
    
    # 遍历所有文件,检查四位数字是否匹配
    for file in policies_dir:
        existing_policy_id = file.name.replace('.xml', '')
        existing_digits = extract_four_digits(existing_policy_id)
        if existing_digits == target_digits:
            return True
    return False

def submit_new(submissions):
    errors = []
    for sub in submissions:
        policy_id = sub.attrib['policy_id']
        if sub.attrib['defect_type'] != 'NV':
            errors.append('Invalid defect type: ' + sub.attrib['defect_type'])
        if gh.policy_exists(policy_id):
            errors.append(f'Existing Policy (matching 4-digit code): {policy_id}')
        # if sub.attrib['policy_id'].endswith('T'):
        #     errors.append('Terminate policy ' + sub.attrib['policy_id'] + ' is not allowed for this submission type')

    branch_name = 'NEW-' + ','.join(str(s.attrib['policy_id']) for s in submissions)
    return branch_name, errors

关键改动说明

  • 新增extract_four_digits函数:用正则表达式\d{4}统一提取Policy ID中的四位连续数字,保证匹配逻辑一致性
  • 重构policy_exists函数:
    1. 先提取当前提交政策的四位数字,若无则沿用原文件名匹配逻辑(可按需移除)
    2. 获取政策目录下所有文件,遍历提取每个文件的四位数字并与目标对比,存在匹配则返回重复
  • 错误提示优化:更新提示文本,明确说明是基于四位编码匹配的重复

内容的提问来源于stack exchange,提问作者h3ll0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 09:50:31