This commit is contained in:
Ladebeze66 2025-04-03 14:59:35 +02:00
parent 84b6bdb328
commit 3bf32de480
5 changed files with 199 additions and 14 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,43 @@
{
"id": 11046,
"name": "changement nom centrale d'enrobage",
"description": "<p><br></p>",
"stage_id": [
8,
"Clôturé"
],
"project_id": [
3,
"Demandes"
],
"partner_id": [
5144,
"CONSEIL DEPARTEMENTAL DU MORBIHAN (56), Dominique CARVAL"
],
"user_id": [
32,
"Romuald GRUSON"
],
"date_start": "2025-03-18 13:22:28",
"date_end": false,
"create_date": "2025-03-18 13:22:27",
"write_date": "2025-04-02 07:16:48",
"message_ids": [
228803,
227733,
227732,
227731,
227730,
227728,
227726,
227725,
227724
],
"message_follower_ids": [
89590,
89592,
89593
],
"timesheet_ids": [],
"attachment_ids": []
}

View File

@ -79,8 +79,32 @@ def main():
# Récupérer et sauvegarder les pièces jointes
attachments = attachment_manager.get_ticket_attachments(ticket_id)
attachments_path = os.path.join(output_dir, "attachments_info.json")
save_json(attachments, attachments_path)
attachments_info = []
attachment_dir = os.path.join(output_dir, "attachments")
os.makedirs(attachment_dir, exist_ok=True)
for attachment in attachments:
file_data = attachment.get("datas")
if file_data:
file_name = attachment.get("name", "unnamed_file")
file_path = os.path.join(attachment_dir, file_name)
# Sauvegarder le fichier binaire
with open(file_path, "wb") as f:
f.write(base64.b64decode(file_data))
# Ajouter l'information de l'attachement à la liste
attachments_info.append({
"id": attachment.get("id"),
"name": file_name,
"path": file_path,
"mimetype": attachment.get("mimetype"),
"create_date": attachment.get("create_date")
})
# Sauvegarder les métadonnées des pièces jointes
attachments_info_path = os.path.join(output_dir, "attachments_info.json")
save_json(attachments_info, attachments_info_path)
# Génération de structure.json
structure = {
@ -98,7 +122,7 @@ def main():
print("Extraction terminée avec succès")
print(f"- Informations du ticket: {ticket_info_path}")
print(f"- Messages: {all_messages_path}")
print(f"- Pièces jointes: {attachments_path}")
print(f"- Pièces jointes: {attachments_info_path}")
print(f"- Structure: {structure_path}")
if __name__ == "__main__":

View File

@ -1,15 +1,50 @@
import os
import base64
import json
from typing import List, Dict, Any
from .auth_manager import AuthManager
from datetime import datetime
class AttachmentManager:
def __init__(self, auth: AuthManager):
self.auth = auth
def __init__(self, odoo_instance, model_name: str = "project.task"):
self.odoo = odoo_instance
self.model_name = model_name
def get_ticket_attachments(self, ticket_id: int) -> List[Dict[str, Any]]:
params = {
"model": "ir.attachment",
"method": "search_read",
"args": [[[ "res_id", "=", ticket_id], ["res_model", "=", "project.task"]]],
"kwargs": {"fields": ["id", "name", "datas"]}
}
return self.auth._rpc_call("/web/dataset/call_kw", params)
def fetch_attachments(self, ticket_id: int) -> List[Dict[str, Any]]:
attachments = self.odoo.execute('ir.attachment', 'search_read', [
[('res_model', '=', self.model_name), ('res_id', '=', ticket_id)]
], ['id', 'name', 'datas', 'mimetype', 'create_date', 'description'])
return attachments if isinstance(attachments, list) else []
def save_attachments(self, ticket_id: int, ticket_dir: str, attachments: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
attachment_dir = os.path.join(ticket_dir, "attachments")
os.makedirs(attachment_dir, exist_ok=True)
attachment_info_list = []
for attachment in attachments:
if attachment.get("datas"):
attachment_name = f"{attachment['id']}_{attachment['name'].replace('/', '_')}"
file_path = os.path.join(attachment_dir, attachment_name)
try:
with open(file_path, "wb") as f:
f.write(base64.b64decode(attachment["datas"]))
attachment_info_list.append({
"id": attachment["id"],
"name": attachment["name"],
"file_path": file_path,
"mimetype": attachment.get("mimetype"),
"create_date": attachment.get("create_date"),
"description": attachment.get("description"),
})
except Exception as e:
print(f"Erreur lors de l'enregistrement de l'attachement {attachment['name']}: {e}")
# Sauvegarde des métadonnées dans un fichier JSON
attachments_info_path = os.path.join(ticket_dir, "attachments_info.json")
with open(attachments_info_path, "w", encoding="utf-8") as f:
json.dump(attachment_info_list, f, indent=4, ensure_ascii=False)
return attachment_info_list