fix(import): provision workbook profession catalog
Some checks are pending
ci / verify (push) Waiting to run
Some checks are pending
ci / verify (push) Waiting to run
This commit is contained in:
parent
9ea9a004ed
commit
f74aa6d7f5
1 changed files with 27 additions and 5 deletions
|
|
@ -226,19 +226,28 @@ def chunks(values: list[dict], size: int = 500):
|
||||||
yield values[offset:offset + size]
|
yield values[offset:offset + size]
|
||||||
|
|
||||||
|
|
||||||
def post(url: str, token: str, payload: dict, allow_self_signed: bool) -> dict:
|
def api_request(url: str, token: str, path: str, method: str,
|
||||||
body = json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
payload: dict | None, allow_self_signed: bool) -> dict:
|
||||||
|
body = None if payload is None else json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
||||||
request = urllib.request.Request(
|
request = urllib.request.Request(
|
||||||
url.rstrip("/") + "/api/v1/staff/imports/owner-workbook",
|
url.rstrip("/") + path,
|
||||||
data=body,
|
data=body,
|
||||||
method="POST",
|
method=method,
|
||||||
headers={"authorization": f"Bearer {token}", "content-type": "application/json"},
|
headers={
|
||||||
|
"authorization": f"Bearer {token}",
|
||||||
|
**({"content-type": "application/json"} if body is not None else {}),
|
||||||
|
},
|
||||||
)
|
)
|
||||||
context = ssl._create_unverified_context() if allow_self_signed else ssl.create_default_context()
|
context = ssl._create_unverified_context() if allow_self_signed else ssl.create_default_context()
|
||||||
with urllib.request.urlopen(request, context=context, timeout=90) as response:
|
with urllib.request.urlopen(request, context=context, timeout=90) as response:
|
||||||
return json.loads(response.read().decode("utf-8"))
|
return json.loads(response.read().decode("utf-8"))
|
||||||
|
|
||||||
|
|
||||||
|
def post(url: str, token: str, payload: dict, allow_self_signed: bool) -> dict:
|
||||||
|
return api_request(url, token, "/api/v1/staff/imports/owner-workbook", "POST",
|
||||||
|
payload, allow_self_signed)
|
||||||
|
|
||||||
|
|
||||||
def main() -> int:
|
def main() -> int:
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
parser.add_argument("workbook", type=Path)
|
parser.add_argument("workbook", type=Path)
|
||||||
|
|
@ -257,6 +266,19 @@ def main() -> int:
|
||||||
if len(args.token) < 32:
|
if len(args.token) < 32:
|
||||||
raise SystemExit("Provide --token or KRONA_STAFF_TOKEN")
|
raise SystemExit("Provide --token or KRONA_STAFF_TOKEN")
|
||||||
|
|
||||||
|
profession_response = api_request(
|
||||||
|
args.server, args.token, "/api/v1/staff/professions", "GET", None,
|
||||||
|
args.allow_self_signed)
|
||||||
|
existing_professions = {
|
||||||
|
item["name"] for item in profession_response.get("professions", [])
|
||||||
|
}
|
||||||
|
for profession_name in sorted(set(PROFESSIONS.values()) - existing_professions):
|
||||||
|
code = "xlsx-" + hashlib.sha256(
|
||||||
|
profession_name.casefold().encode("utf-8")).hexdigest()[:12]
|
||||||
|
api_request(args.server, args.token, "/api/v1/staff/professions", "POST",
|
||||||
|
{"code": code, "name": profession_name}, args.allow_self_signed)
|
||||||
|
print(f"Created profession: {profession_name}")
|
||||||
|
|
||||||
source = f"{args.workbook.name}:2026"
|
source = f"{args.workbook.name}:2026"
|
||||||
batches = [
|
batches = [
|
||||||
{"units": payload["units"], "employees": payload["employees"]},
|
{"units": payload["units"], "employees": payload["employees"]},
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue