add log file function

This commit is contained in:
2026-03-24 16:56:23 +05:30
parent 6c74b5d3bf
commit 88e8771b51
3 changed files with 15 additions and 15 deletions

View File

View File

@@ -14,22 +14,24 @@ class LogHelper:
logData.WriteLog(action, details="") logData.WriteLog(action, details="")
class LogData: class LogData:
filepath = "" filepath = ""
timestamp = None timestamp = None
def __init__(self): def __init__(self):
self.filepath = FolderAndFile.get_activity_log_path('activity.log') self.filepath = FolderAndFile.get_activity_log_path('activity.log')
self.timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") self.timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.user = LogData.get_current_user()
@staticmethod
def get_current_user():
if hasattr(current_user, "cn") and current_user.cn: if hasattr(current_user, "cn") and current_user.cn:
self.user = current_user.cn return current_user.cn
elif hasattr(current_user, "username") and current_user.username: elif hasattr(current_user, "username") and current_user.username:
self.user = current_user.username return current_user.username
elif hasattr(current_user, "sAMAccountName") and current_user.sAMAccountName: elif hasattr(current_user, "sAMAccountName") and current_user.sAMAccountName:
self.user = current_user.sAMAccountName return current_user.sAMAccountName
else: return "Unknown"
self.user = "Unknown"
def WriteLog(self, action, details=""): def WriteLog(self, action, details=""):
"""Log user actions with timestamp, user, action, and details.""" """Log user actions with timestamp, user, action, and details."""
@@ -42,7 +44,6 @@ class LogData:
f"Details: {details}\n" f"Details: {details}\n"
) )
def GetActivitiesLog(self): def GetActivitiesLog(self):
logs = [] logs = []
@@ -60,7 +61,6 @@ class LogData:
return logs return logs
def GetFilteredActivitiesLog(self, startDate, endDate, userName): def GetFilteredActivitiesLog(self, startDate, endDate, userName):
filtered_logs = self.GetActivitiesLog() filtered_logs = self.GetActivitiesLog()
# Date filter # Date filter
@@ -69,17 +69,14 @@ class LogData:
start_dt = datetime.strptime(startDate, "%Y-%m-%d") if startDate else datetime.min 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 end_dt = datetime.strptime(endDate, "%Y-%m-%d") if endDate else datetime.max
filtered_logs = [ filtered_logs = [
log for log in filtered_logs log for log in filtered_logs
if start_dt <= datetime.strptime(log["timestamp"], "%Y-%m-%d %H:%M:%S") <= end_dt if start_dt <= datetime.strptime(log["timestamp"], "%Y-%m-%d %H:%M:%S") <= end_dt
] ]
except Exception as e: except Exception as e:
print("Date filter error:", e) print("Date filter error:", e)
#Why catching all exceptions? Need to handle specific exceptions
# Username filter # Username filter
if userName: if userName:
filtered_logs = [log for log in filtered_logs if userName.lower() in log["user"].lower()] filtered_logs = [log for log in filtered_logs if userName.lower() in log["user"].lower()]

View File

@@ -7,6 +7,7 @@
<title>Edit Invoice</title> <title>Edit Invoice</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/invoice.css') }}"> <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/invoice.css') }}">
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style1.css') }}"> <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/style1.css') }}">
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head> </head>
@@ -204,15 +205,17 @@
type: "POST", type: "POST",
url: $(this).attr("action"), url: $(this).attr("action"),
data: $(this).serialize(), data: $(this).serialize(),
dataType: 'json', // ensure JSON is returned
success: function (response) { success: function (response) {
if (response.status === "success") { if (response.status === "success") {
$("#invoiceSuccessAlert").fadeIn().delay(3000).fadeOut();
alert("Invoice updated successfully!"); // <-- Popup alert alert("Invoice updated successfully!"); // <-- Popup alert
}
// ✅ Redirect to Add Invoice page (table part visible)
window.location.href = "{{ url_for('invoice.add_invoice') }}#addTable";
}
}, },
error: function (xhr) { error: function (xhr) {
alert("Error: " + xhr.responseJSON.message); alert("Error: " + xhr.responseJSON?.message || "Something went wrong!");
} }
}); });
}); });