保证代理表中“有且仅有一个默认代理”: - 没有默认时,自动使用最早创建(ID 最小)的代理作为默认 - 有多个默认时,仅保留最早的一个
(db: Session)
| 500 | # ============================================================================ |
| 501 | |
| 502 | def _ensure_single_default_proxy(db: Session) -> Optional[Proxy]: |
| 503 | """ |
| 504 | 保证代理表中“有且仅有一个默认代理”: |
| 505 | - 没有默认时,自动使用最早创建(ID 最小)的代理作为默认 |
| 506 | - 有多个默认时,仅保留最早的一个 |
| 507 | """ |
| 508 | proxies = db.query(Proxy).order_by(asc(Proxy.id)).all() |
| 509 | if not proxies: |
| 510 | return None |
| 511 | |
| 512 | default_proxies = [proxy for proxy in proxies if bool(proxy.is_default)] |
| 513 | keeper = default_proxies[0] if default_proxies else proxies[0] |
| 514 | changed = False |
| 515 | |
| 516 | if not keeper.is_default: |
| 517 | keeper.is_default = True |
| 518 | changed = True |
| 519 | |
| 520 | for proxy in proxies: |
| 521 | should_default = proxy.id == keeper.id |
| 522 | if bool(proxy.is_default) != should_default: |
| 523 | proxy.is_default = should_default |
| 524 | changed = True |
| 525 | |
| 526 | if changed: |
| 527 | db.commit() |
| 528 | db.refresh(keeper) |
| 529 | |
| 530 | return keeper |
| 531 | |
| 532 | |
| 533 | def create_proxy( |
no test coverage detected