Compare commits

...

4 Commits

Author SHA1 Message Date
Stefan Allius
cb9344a476 set timezone of scanned creation time 2025-05-13 00:34:22 +02:00
Stefan Allius
7aab01bcfc add an empty file
- the empty file is needed for unit tests to force
  an exception on the try to scan the first line
  for an timestamp
2025-05-12 23:16:09 +02:00
Stefan Allius
60971be000 increase test coverage 2025-05-12 23:05:24 +02:00
Stefan Allius
9304407348 scan log files for timestamp as creating timestamp 2025-05-12 23:03:35 +02:00
3 changed files with 59 additions and 6 deletions

View File

@@ -1,26 +1,58 @@
from quart import render_template from quart import render_template
from quart_babel import format_datetime, format_decimal from quart_babel import format_datetime, format_decimal, _
from quart.helpers import send_from_directory from quart.helpers import send_from_directory
from werkzeug.utils import secure_filename from werkzeug.utils import secure_filename
from cnf.config import Config from cnf.config import Config
from datetime import datetime
from os import DirEntry
import os import os
from dateutil import tz
from . import web from . import web
def _get_file(file): def _get_birth_from_log(path: str) -> None | datetime:
'''read timestamp from the first line of a log file'''
dt = None
try:
with open(path) as f:
first_line = f.readline()
first_line = first_line.lstrip("'")
fmt = "%Y-%m-%d %H:%M:%S" if first_line[4] == '-' \
else "%d-%m-%Y %H:%M:%S"
dt = datetime.strptime(first_line[0:19], fmt). \
replace(tzinfo=tz.tzlocal())
except Exception:
pass
return dt
def _get_file(file: DirEntry) -> dict:
'''build one row for the connection table''' '''build one row for the connection table'''
entry = {} entry = {}
entry['name'] = file.name entry['name'] = file.name
stat = file.stat() stat = file.stat()
entry['size'] = format_decimal(stat.st_size) entry['size'] = format_decimal(stat.st_size)
entry['date'] = stat.st_mtime try:
entry['created'] = format_datetime(stat.st_ctime, format="short") dt = stat.st_birthtime
except Exception:
dt = _get_birth_from_log(file.path)
if dt:
entry['created'] = format_datetime(dt, format="short")
# sort by creating date, if available
entry['date'] = dt if isinstance(dt, float) else dt.timestamp()
else:
entry['created'] = _('n/a')
entry['date'] = stat.st_mtime
entry['modified'] = format_datetime(stat.st_mtime, format="short") entry['modified'] = format_datetime(stat.st_mtime, format="short")
return entry return entry
def get_list_data(): def get_list_data() -> list:
'''build the connection table''' '''build the connection table'''
file_list = [] file_list = []
with os.scandir(Config.get_log_path()) as it: with os.scandir(Config.get_log_path()) as it:

0
app/tests/log/empty.txt Normal file
View File

View File

@@ -9,6 +9,8 @@ from cnf.config import Config
from mock import patch from mock import patch
from proxy import Proxy from proxy import Proxy
import os, errno import os, errno
from os import DirEntry, stat_result
import datetime
pytest_plugins = ('pytest_asyncio',) pytest_plugins = ('pytest_asyncio',)
@@ -201,14 +203,33 @@ async def test_notes_fetch(client, config_conn):
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_file_fetch(client, config_conn): async def test_file_fetch(client, config_conn, monkeypatch):
"""Test the data-fetch route.""" """Test the data-fetch route."""
_ = config_conn _ = config_conn
assert Config.log_path == 'app/tests/log/' assert Config.log_path == 'app/tests/log/'
def my_stat1(*arg):
stat = stat_result
stat.st_size = 20
stat.st_birthtime = datetime.datetime(2024, 1, 31, 10, 30, 15)
stat.st_mtime = datetime.datetime(2024, 1, 1, 1, 30, 15).timestamp()
return stat
monkeypatch.setattr(DirEntry, "stat", my_stat1)
response = await client.get('/file-fetch') response = await client.get('/file-fetch')
assert response.status_code == 200 assert response.status_code == 200
def my_stat2(*arg):
stat = stat_result
stat.st_size = 20
stat.st_mtime = datetime.datetime(2024, 1, 1, 1, 30, 15).timestamp()
return stat
monkeypatch.setattr(DirEntry, "stat", my_stat2)
monkeypatch.delattr(stat_result, "st_birthtime")
response = await client.get('/file-fetch')
assert response.status_code == 200
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_send_file(client, config_conn): async def test_send_file(client, config_conn):
"""Test the send-file route.""" """Test the send-file route."""