Hello team,
I have made a simple virtual doctype with the code below:
import frappe
import ast
from frappe.model.document import Document
class ActiveUser(Document):
def db_insert(self, *args, **kwargs):
pass
def load_from_db(self):
pass
def db_update(self):
pass
@staticmethod
def get_list(args):
return get_active_users()
@staticmethod
def get_count(args):
return len(get_active_users())
@staticmethod
def get_stats(args):
pass
@staticmethod
def delete():
frappe.throw("Deletion is not allowed for Active Users.")
@frappe.whitelist()
def get_active_users(filters=None, fields=None, order_by=None, start=0, page_length=20):
"""
Fetch all active user sessions from the 'tabSessions' table and extract required fields.
"""
active_sessions = frappe.db.sql(
"""
SELECT user, sid, sessiondata, ipaddress, lastupdate as login_time, status
FROM `tabSessions`
ORDER BY lastupdate DESC
""",
as_dict=True
)
session_list = []
for session in active_sessions:
# Parse sessiondata (json.loads fails because of '')
session_data = session_data = ast.literal_eval(session.get("sessiondata") or "{}")
session_entry = {
"sid": session["sid"],
"user": session["user"],
"status": session["status"],
"full_name": session_data.get("full_name", ""),
"last_updated": session_data.get("last_updated"),
"session_ip": session_data.get("session_ip"),
"user_type": session_data.get("user_type"),
"csrf_token": session_data.get("csrf_token"),
}
session_list.append(session_entry)
return session_list
The List View only shows the first record returned even when session_list returns all rows in list form. Also, it does not open form view. Report View shows all records and even opens associated link (user) when clicked.
NOTE:
sid has been set as name field
What could be the problem here? Any help is highly appreciated.