fix many issues
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import pytest
|
||||
from django.urls import reverse
|
||||
|
||||
|
||||
@pytest.mark.django_db
|
||||
def test_logs_view_filters_by_date_and_can_reset_log_file(client, admin_user, monkeypatch, tmp_path):
|
||||
client.force_login(admin_user)
|
||||
|
||||
log_file = tmp_path / "log.txt"
|
||||
log_file.write_text(
|
||||
"[03/Jun/2026 10:00:00] INFO [atlas.views:10] first entry\n"
|
||||
"[03/Jun/2026 11:00:00] ERROR [atlas.views:11] second entry\n",
|
||||
encoding="utf-8",
|
||||
)
|
||||
monkeypatch.setattr("rad.views.settings.BASE_DIR", str(tmp_path))
|
||||
|
||||
response = client.get(
|
||||
reverse("logs_view"),
|
||||
{
|
||||
"date_from": "2026-06-03",
|
||||
"date_to": "2026-06-03",
|
||||
"time_from": "10:30",
|
||||
"time_to": "11:30",
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert b"second entry" in response.content
|
||||
assert b"first entry" not in response.content
|
||||
|
||||
response = client.post(reverse("logs_view"), {"action": "clear"})
|
||||
|
||||
assert response.status_code == 200
|
||||
assert b"Log file cleared." in response.content
|
||||
assert log_file.read_text(encoding="utf-8") == ""
|
||||
@@ -3,6 +3,7 @@ from typing import Any, Optional
|
||||
import os
|
||||
import re
|
||||
from io import StringIO
|
||||
from datetime import datetime
|
||||
from django.conf import settings
|
||||
|
||||
from django_tables2 import SingleTableMixin
|
||||
@@ -306,6 +307,53 @@ def logs_view(request):
|
||||
show_all = request.GET.get("all") == "1"
|
||||
raw_mode = request.GET.get("raw") == "1"
|
||||
include_noise = request.GET.get("include_noise") == "1"
|
||||
date_from_raw = request.GET.get("date_from", "").strip()
|
||||
date_to_raw = request.GET.get("date_to", "").strip()
|
||||
time_from_raw = request.GET.get("time_from", "").strip()
|
||||
time_to_raw = request.GET.get("time_to", "").strip()
|
||||
action_message = ""
|
||||
|
||||
if request.method == "POST" and request.POST.get("action") == "clear":
|
||||
try:
|
||||
os.makedirs(os.path.dirname(log_file_path), exist_ok=True)
|
||||
with open(log_file_path, "w", encoding="utf-8"):
|
||||
pass
|
||||
action_message = "Log file cleared."
|
||||
except Exception as exc:
|
||||
action_message = f"Failed to clear log file: {exc}"
|
||||
|
||||
def _parse_log_timestamp(timestamp_text: str) -> datetime | None:
|
||||
if not timestamp_text:
|
||||
return None
|
||||
for timestamp_format in ("%d/%b/%Y %H:%M:%S", "%Y-%m-%d %H:%M:%S"):
|
||||
try:
|
||||
return datetime.strptime(timestamp_text, timestamp_format)
|
||||
except ValueError:
|
||||
continue
|
||||
return None
|
||||
|
||||
def _parse_date_input(value: str):
|
||||
if not value:
|
||||
return None
|
||||
try:
|
||||
return datetime.strptime(value, "%Y-%m-%d").date()
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
def _parse_time_input(value: str):
|
||||
if not value:
|
||||
return None
|
||||
for time_format in ("%H:%M", "%H:%M:%S"):
|
||||
try:
|
||||
return datetime.strptime(value, time_format).time()
|
||||
except ValueError:
|
||||
continue
|
||||
return None
|
||||
|
||||
date_from = _parse_date_input(date_from_raw)
|
||||
date_to = _parse_date_input(date_to_raw)
|
||||
time_from = _parse_time_input(time_from_raw)
|
||||
time_to = _parse_time_input(time_to_raw)
|
||||
|
||||
if raw_mode:
|
||||
if not os.path.exists(log_file_path):
|
||||
@@ -438,6 +486,19 @@ def logs_view(request):
|
||||
if search_lower and search_lower not in haystack:
|
||||
continue
|
||||
|
||||
entry_timestamp = _parse_log_timestamp(entry.get("timestamp", ""))
|
||||
if date_from or date_to or time_from or time_to:
|
||||
if entry_timestamp is None:
|
||||
continue
|
||||
if date_from and entry_timestamp.date() < date_from:
|
||||
continue
|
||||
if date_to and entry_timestamp.date() > date_to:
|
||||
continue
|
||||
if time_from and entry_timestamp.time() < time_from:
|
||||
continue
|
||||
if time_to and entry_timestamp.time() > time_to:
|
||||
continue
|
||||
|
||||
filtered_entries.append(entry)
|
||||
|
||||
total = len(filtered_entries)
|
||||
@@ -459,6 +520,11 @@ def logs_view(request):
|
||||
"raw_url": f"{reverse('logs_view')}?raw=1",
|
||||
"include_noise": include_noise,
|
||||
"noise_suppressed": noise_suppressed,
|
||||
"date_from": date_from_raw,
|
||||
"date_to": date_to_raw,
|
||||
"time_from": time_from_raw,
|
||||
"time_to": time_to_raw,
|
||||
"action_message": action_message,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user