Update README and csv_uploader to match hardcoded field names

Remove --id-field and --seeding-field args from csv_uploader, hardcode
field names as class constants, and fix the same PATCH/GET bugs. Update
README examples to reflect simplified CLI args.
This commit is contained in:
constantprojects
2026-02-17 15:25:07 -07:00
parent 16eb35f095
commit e7dd24de73
2 changed files with 17 additions and 34 deletions

View File

@@ -17,14 +17,14 @@ from pathlib import Path
class NocoDBClient:
"""Simple NocoDB API client."""
def __init__(self, base_url: str, table_id: str, api_token: str,
id_field: str, seeding_field: str):
ID_FIELD = "Id"
SEEDING_FIELD = "Seeding Users"
def __init__(self, base_url: str, table_id: str, api_token: str):
self.base_url = base_url.rstrip('/')
self.table_id = table_id
self.api_token = api_token
self.id_field = id_field
self.seeding_field = seeding_field
self.endpoint = f"{self.base_url}/api/v2/tables/{table_id}/records"
def _request(self, method: str, data: dict | None = None, params: dict | None = None) -> dict:
@@ -52,16 +52,16 @@ class NocoDBClient:
def get_record(self, record_id: int | str) -> dict | None:
try:
params = {"where": f"({self.id_field},eq,{record_id})", "limit": "1"}
params = {"where": f"({self.ID_FIELD},eq,{record_id})", "limit": "1"}
result = self._request("GET", params=params)
records = result.get("list", [])
return records[0] if records else None
except Exception:
return None
def update_record(self, record_id: int | str, value: str) -> bool:
def update_record(self, row_id: int, value: str) -> bool:
try:
data = {self.id_field: int(record_id), self.seeding_field: value}
data = {"Id": row_id, self.SEEDING_FIELD: value}
self._request("PATCH", data=data)
return True
except Exception as e:
@@ -94,8 +94,7 @@ def main():
epilog="""
Example:
%(prog)s --csv seeds.csv --nocodb-url https://noco.example.com \\
--table-id tblXXX --api-token xc-xxx \\
--id-field cXXX --seeding-field cYYY
--table-id xxxxxxxxxxxxx --api-token xc-xxx
"""
)
@@ -107,10 +106,6 @@ Example:
help='Table ID')
parser.add_argument('--api-token', required=True, type=str,
help='API token')
parser.add_argument('--id-field', required=True, type=str,
help='Field ID for Id column')
parser.add_argument('--seeding-field', required=True, type=str,
help='Field ID for seeding_users column')
args = parser.parse_args()
@@ -126,10 +121,7 @@ Example:
print(f"Loaded {len(rows)} rows from {args.csv}")
# Connect
noco = NocoDBClient(
args.nocodb_url, args.table_id, args.api_token,
args.id_field, args.seeding_field
)
noco = NocoDBClient(args.nocodb_url, args.table_id, args.api_token)
print("Testing connection...")
try:
@@ -157,7 +149,7 @@ Example:
stats['not_found'] += 1
continue
current = parse_multiselect(record.get(noco.seeding_field))
current = parse_multiselect(record.get(noco.SEEDING_FIELD))
if username in current:
print(f" = {record_id}: already listed")
@@ -166,7 +158,7 @@ Example:
current.add(username)
if noco.update_record(record_id, format_multiselect(current)):
if noco.update_record(record["Id"], format_multiselect(current)):
print(f" + {record_id}: added {username}")
stats['updated'] += 1
else: