Files
Payment_Reconciliation/model/Log.py

84 lines
3.0 KiB
Python
Raw Normal View History

import os
2025-11-25 14:14:10 +05:30
from flask import current_app
2026-03-24 16:29:18 +05:30
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
from datetime import datetime
2025-11-25 14:14:10 +05:30
2026-03-24 16:29:18 +05:30
from model.FolderAndFile import FolderAndFile
2025-11-25 14:14:10 +05:30
class LogHelper:
@staticmethod
def log_action(action, details=""):
2026-03-24 16:29:18 +05:30
"""Log user actions with timestamp, user, action, and details."""
logData = LogData()
logData.WriteLog(action, details="")
2025-11-25 14:14:10 +05:30
class LogData:
2026-03-24 16:29:18 +05:30
filepath = ""
timestamp = None
2025-11-25 14:14:10 +05:30
def __init__(self):
2026-03-24 16:29:18 +05:30
self.filepath = FolderAndFile.get_activity_log_path('activity.log')
2025-11-25 14:14:10 +05:30
self.timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
2026-03-24 16:56:23 +05:30
self.user = LogData.get_current_user()
2026-03-24 16:56:23 +05:30
@staticmethod
def get_current_user():
2026-03-24 16:29:18 +05:30
if hasattr(current_user, "cn") and current_user.cn:
2026-03-24 16:56:23 +05:30
return current_user.cn
2026-03-24 16:29:18 +05:30
elif hasattr(current_user, "username") and current_user.username:
2026-03-24 16:56:23 +05:30
return current_user.username
2026-03-24 16:29:18 +05:30
elif hasattr(current_user, "sAMAccountName") and current_user.sAMAccountName:
2026-03-24 16:56:23 +05:30
return current_user.sAMAccountName
return "Unknown"
2026-03-24 16:29:18 +05:30
def WriteLog(self, action, details=""):
"""Log user actions with timestamp, user, action, and details."""
2025-11-25 14:14:10 +05:30
with open(self.filepath, "a", encoding="utf-8") as f:
f.write(
f"Timestamp: {self.timestamp} | "
f"User: {self.user} | "
f"Action: {action} | "
f"Details: {details}\n"
)
2026-03-24 16:29:18 +05:30
def GetActivitiesLog(self):
2025-11-25 14:14:10 +05:30
logs = []
2026-03-24 16:29:18 +05:30
2025-11-25 14:14:10 +05:30
if os.path.exists(self.filepath):
2026-03-24 16:29:18 +05:30
with open(self.filepath, 'r') as f:
2025-11-25 14:14:10 +05:30
for line in f:
parts = line.strip().split(" | ")
if len(parts) == 4:
logs.append({
2026-03-24 16:29:18 +05:30
"timestamp": parts[0].replace("Timestamp:", "").strip(),
"user": parts[1].replace("User:", "").strip(),
"action": parts[2].replace("Action:", "").strip(),
"details": parts[3].replace("Details:", "").strip()
2025-11-25 14:14:10 +05:30
})
return logs
2026-03-24 16:29:18 +05:30
def GetFilteredActivitiesLog(self, startDate, endDate, userName):
filtered_logs = self.GetActivitiesLog()
2025-11-25 14:14:10 +05:30
2026-03-24 16:29:18 +05:30
# Date filter
if startDate or endDate:
try:
start_dt = datetime.strptime(startDate, "%Y-%m-%d") if startDate else datetime.min
end_dt = datetime.strptime(endDate, "%Y-%m-%d") if endDate else datetime.max
filtered_logs = [
log for log in filtered_logs
if start_dt <= datetime.strptime(log["timestamp"], "%Y-%m-%d %H:%M:%S") <= end_dt
]
2025-11-25 14:14:10 +05:30
2026-03-24 16:29:18 +05:30
except Exception as e:
print("Date filter error:", e)
2026-03-24 16:56:23 +05:30
2026-03-24 16:29:18 +05:30
# Username filter
if userName:
filtered_logs = [log for log in filtered_logs if userName.lower() in log["user"].lower()]
2025-11-25 14:14:10 +05:30
2026-03-24 16:29:18 +05:30
return filtered_logs