[IMP] hw_drivers: IoT log file download error and better name

Small improvements on the download logs button in the Iot App:
 - Log file contains the IoT name and date of generation
 - Internal server error araise if the logfile config is missing
 - Internal server error aised if the log file is missin

Was meant to be part of:
https://github.com/odoo/odoo/pull/159186

opw-3827121

closes odoo/odoo#160755

X-original-commit: a6bcfce6714c4a804fe84d81d8b9ee29c1440046
Signed-off-by: Loan Sens (lse) <lse@odoo.com>
This commit is contained in:
Loan (LSE)
2024-04-09 07:22:43 +00:00
parent 88437a0865
commit 76a9c4d0c0
+23 -14
View File
@@ -2,11 +2,14 @@
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from base64 import b64decode
from datetime import datetime
import json
import logging
import os
import subprocess
from socket import gethostname
import time
from werkzeug.exceptions import InternalServerError
from zlib import adler32
from odoo import http, tools
@@ -80,18 +83,24 @@ class DriverController(http.Controller):
Downloads the log file
"""
log_path = tools.config['logfile']
if log_path:
check = adler32(log_path.encode())
if not log_path:
raise InternalServerError("Log file configuration is not set")
try:
stat = os.stat(log_path)
# intentionally don't use Stream.from_path as the path used is not in the addons path
# for instance, for the iot-box it will be in /var/log/odoo
return http.Stream(
type='path',
path=log_path,
download_name=os.path.basename(log_path),
etag=f'{int(stat.st_mtime)}-{stat.st_size}-{check}',
last_modified=stat.st_mtime,
size=stat.st_size,
).get_response(
mimetype='text/plain', as_attachment=True
)
except FileNotFoundError:
raise InternalServerError("Log file has not been found")
check = adler32(log_path.encode())
log_file_name = f"iot-odoo-{gethostname()}-{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}.log"
# intentionally don't use Stream.from_path as the path used is not in the addons path
# for instance, for the iot-box it will be in /var/log/odoo
return http.Stream(
type='path',
path=log_path,
download_name=log_file_name,
etag=f'{int(stat.st_mtime)}-{stat.st_size}-{check}',
last_modified=stat.st_mtime,
size=stat.st_size,
mimetype='text/plain',
).get_response(
mimetype='text/plain', as_attachment=True
)