fix many issues
This commit is contained in:
@@ -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