调整 格式
This commit is contained in:
@@ -4,6 +4,7 @@ import json
|
||||
import re
|
||||
import time
|
||||
import uuid
|
||||
from collections.abc import Sequence
|
||||
from typing import Any
|
||||
from urllib import error, parse, request
|
||||
|
||||
@@ -201,19 +202,44 @@ class FeishuClient:
|
||||
time.sleep(0.2)
|
||||
return created
|
||||
|
||||
def ensure_target_fields(self, access_token: str, app_token: str, table_id: str) -> list[str]:
|
||||
def ensure_target_fields(
|
||||
self,
|
||||
access_token: str,
|
||||
app_token: str,
|
||||
table_id: str,
|
||||
field_specs: Sequence[Any] = FIELD_SPECS,
|
||||
) -> list[str]:
|
||||
fields = self.list_fields(access_token, app_token, table_id)
|
||||
primary_field = next((field for field in fields if field.get("is_primary")), None)
|
||||
if not primary_field:
|
||||
raise FeishuApiError("未找到主字段")
|
||||
expected_field_names = {spec.feishu_name for spec in FIELD_SPECS}
|
||||
expected_field_names = {spec.feishu_name for spec in field_specs}
|
||||
expected_field_order = [spec.feishu_name for spec in field_specs]
|
||||
actual_managed_field_names = {
|
||||
field.get("field_name")
|
||||
for field in fields
|
||||
if not field.get("is_primary") and field.get("field_name") in expected_field_names
|
||||
}
|
||||
actual_expected_field_order = [
|
||||
field.get("field_name")
|
||||
for field in fields
|
||||
if not field.get("is_primary") and field.get("field_name") in expected_field_names
|
||||
]
|
||||
obsolete_managed_field_names = {
|
||||
field.get("field_name")
|
||||
for field in fields
|
||||
if (
|
||||
not field.get("is_primary")
|
||||
and field.get("field_name") not in expected_field_names
|
||||
and self._is_managed_field(field.get("field_name") or "")
|
||||
)
|
||||
}
|
||||
primary_matches = primary_field.get("field_name") == PRIMARY_FIELD_NAME
|
||||
fields_match = actual_managed_field_names == expected_field_names
|
||||
fields_match = (
|
||||
actual_managed_field_names == expected_field_names
|
||||
and actual_expected_field_order == expected_field_order
|
||||
and not obsolete_managed_field_names
|
||||
)
|
||||
if primary_matches and fields_match:
|
||||
return [field.get("field_name") for field in fields if field.get("field_name")]
|
||||
|
||||
@@ -242,11 +268,11 @@ class FeishuClient:
|
||||
field_id = field.get("field_id")
|
||||
if field.get("is_primary") or not field_name or not field_id:
|
||||
continue
|
||||
if self._is_managed_field(field_name):
|
||||
if field_name in expected_field_names or self._is_managed_field(field_name):
|
||||
self.delete_field(access_token, app_token, table_id, field_id)
|
||||
time.sleep(0.2)
|
||||
|
||||
for spec in FIELD_SPECS:
|
||||
for spec in field_specs:
|
||||
payload = {"field_name": spec.feishu_name, "type": spec.field_type}
|
||||
if spec.formatter is not None:
|
||||
payload["property"] = {"formatter": spec.formatter}
|
||||
|
||||
Reference in New Issue
Block a user