From b5c84635cf5e969b195386cc95d45fbfd917a3a9 Mon Sep 17 00:00:00 2001 From: Laurent Chedanne Date: Fri, 29 May 2026 10:40:34 +0200 Subject: [PATCH] Code changes for sql-to-api branch --- csv-to-timemanager.py | 46 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 9 deletions(-) diff --git a/csv-to-timemanager.py b/csv-to-timemanager.py index ce77c7a..dc82ea6 100644 --- a/csv-to-timemanager.py +++ b/csv-to-timemanager.py @@ -139,10 +139,13 @@ def get_client_uuid_by_name(client_name, user_id): query = ("select uuid from oc_timemanager_client where name=\""+client_name+"\" AND user_id=\""+user_id+"\"") cursor.execute(query) - client_uuid = cursor.fetchone()[0] + row = cursor.fetchone() save_db() - return client_uuid.decode('utf-8') + if not row: + return None + + return row[0].decode('utf-8') def get_project_uuid_by_name(project_name, client_uuid, user_id): @@ -311,15 +314,28 @@ def create_client(name): } response = requests.post(url, data=json.dumps(req_data), headers=headers) - + # Vérification de la réponse if response.status_code == 200: - pass - #print("Client créé avec succès.") - #print(json.dumps(response.json(), indent=1)) + try: + resp = response.json() + # Tenter d'extraire l'UUID renvoyé par l'API (structure attendue) + client_uuid = None + if isinstance(resp, dict): + client_created = resp.get('data', {}).get('clients', {}).get('created', []) + if client_created and isinstance(client_created, list): + client_uuid = client_created[0].get('uuid') + + if client_uuid: + return client_uuid + # si pas d'uuid dans la réponse, retourner None (le caller gérera) + return None + except Exception: + return None else: print("Erreur de création client") print(response.reason) + return None def insert_task_time(name, project_name, client_name, task_uuid, user_id, dstart, hstart, dend, hend): @@ -422,11 +438,23 @@ for user_info in config['calendars']: continue # Description => Client + client_uuid = None if not client_exist_by_name(client_name, user_id): print("Création du client {}".format(client_name)) - create_client(client_name) - - client_uuid = get_client_uuid_by_name(client_name, user_id) + created_uuid = create_client(client_name) + if created_uuid: + client_uuid = created_uuid + else: + # tentative de récupération depuis la base au cas où l'API a créé l'entrée + client_uuid = get_client_uuid_by_name(client_name, user_id) + if not client_uuid: + print("Erreur: impossible d'obtenir l'UUID du client '{}' après création, saut.".format(client_name)) + continue + else: + client_uuid = get_client_uuid_by_name(client_name, user_id) + if not client_uuid: + print("Erreur: le client '{}' existe mais son UUID est introuvable, saut.".format(client_name)) + continue # Categories => Project if not project_exist_by_name(project_name, client_uuid, user_id):