@@ -266,3 +266,58 @@ def edit_user(username):
266266 # prepopulate form
267267 form .roles .data = user_doc .get ("roles" , [])
268268 return render_template ("admin/users/edit.html.jinja2" , user = user_doc , form = form )
269+
270+
271+ @admin .route ("/accounting" )
272+ @login_required
273+ @admin_permission .require ()
274+ @register_breadcrumb (admin , ".accounting" , "Accounting" )
275+ def accounting ():
276+ page = int (request .args .get ("page" , 1 ))
277+ per_page = current_app .config .get ("SEARCH_ITEMS_PER_PAGE" , 25 )
278+ username_filter = request .args .get ("username" )
279+ endpoint_filter = request .args .get ("endpoint" )
280+
281+ # Validate filters to prevent injection attacks
282+ if username_filter is not None and not isinstance (username_filter , str ):
283+ username_filter = None
284+ if endpoint_filter is not None and not isinstance (endpoint_filter , str ):
285+ endpoint_filter = None
286+
287+ # Build query based on filters
288+ query = {}
289+ if username_filter :
290+ query ["username" ] = username_filter
291+ if endpoint_filter :
292+ query ["endpoint" ] = endpoint_filter
293+
294+ # Get accounting logs sorted by period (most recent first)
295+ logs_cursor = mongo .db .accounting .find (query ).sort ([("period" , pymongo .DESCENDING )])
296+ total = mongo .db .accounting .count_documents (query )
297+ logs_list = list (logs_cursor [(page - 1 ) * per_page : page * per_page ])
298+
299+ # Get distinct usernames and endpoints for filter dropdowns
300+ distinct_usernames = sorted (
301+ [u for u in mongo .db .accounting .distinct ("username" ) if u is not None ], key = lambda x : x .lower ()
302+ )
303+ distinct_endpoints = sorted (mongo .db .accounting .distinct ("endpoint" ))
304+
305+ pagination = Pagination (
306+ page = page ,
307+ per_page = per_page ,
308+ search = False ,
309+ found = total ,
310+ total = total ,
311+ css_framework = "bootstrap5" ,
312+ alignment = "center" ,
313+ )
314+
315+ return render_template (
316+ "admin/accounting.html.jinja2" ,
317+ logs = logs_list ,
318+ pagination = pagination ,
319+ distinct_usernames = distinct_usernames ,
320+ distinct_endpoints = distinct_endpoints ,
321+ username_filter = username_filter ,
322+ endpoint_filter = endpoint_filter ,
323+ )
0 commit comments