diff --git a/analytics.py b/analytics.py index 87b4346..2a24a85 100644 --- a/analytics.py +++ b/analytics.py @@ -727,6 +727,8 @@ def file_access(): if 'timeframe' not in session: session['timeframe'] = 'last24hours' session['timeframe'] = request.args.get('timeframe', session['timeframe']) + if 'file_access_category' not in session: + session['file_access_category'] = None now = datetime.now() @@ -775,25 +777,56 @@ def file_access(): 'category': hf.extract_structure_from_string(rel_path)[0] }) - # Get possible categories from the rows - categories = sorted({r['category'] for r in rows if r['category'] is not None}) - all_categories = [None] + categories - top20 = [] - for category in all_categories: - label = category if category is not None else 'Keine Kategorie gefunden !' - files = [r for r in rows if r['category'] == category][:20] - top20.append({ - 'category': label, - 'files': files + # Build a list of selectable categories for the dropdown + category_options = [] + has_uncategorized = any(r['category'] is None for r in rows) + if has_uncategorized: + category_options.append({ + 'value': 'uncategorized', + 'label': 'Keine Kategorie gefunden !' }) + unique_categories = sorted({r['category'] for r in rows if r['category'] is not None}) + for category in unique_categories: + category_options.append({ + 'value': category, + 'label': category + }) + + default_category_value = category_options[0]['value'] if category_options else None + requested_category = request.args.get('category') + stored_category = session.get('file_access_category') + selected_category = requested_category or stored_category or default_category_value + + valid_values = {opt['value'] for opt in category_options} + if selected_category not in valid_values: + selected_category = default_category_value + + session['file_access_category'] = selected_category + + def matches_category(row): + if selected_category == 'uncategorized': + return row['category'] is None + return row['category'] == selected_category + + filtered_rows = [r for r in rows if matches_category(r)] if selected_category is not None else [] + top20_files = filtered_rows[:20] + + selected_category_label = next( + (opt['label'] for opt in category_options if opt['value'] == selected_category), + 'Keine Daten verfügbar' + ) + title_short = app_config.get('TITLE_SHORT', 'Default Title') title_long = app_config.get('TITLE_LONG' , 'Default Title') return render_template( "file_access.html", timeframe=session['timeframe'], - top20 = top20, + categories=category_options, + selected_category=selected_category, + selected_category_label=selected_category_label, + top20_files=top20_files, admin_enabled=auth.is_admin(), title_short=title_short, title_long=title_long diff --git a/templates/file_access.html b/templates/file_access.html index 78ac21d..ba67461 100644 --- a/templates/file_access.html +++ b/templates/file_access.html @@ -34,47 +34,63 @@
+ + {% if categories %} +| {{ row.access_count }} | {{ row.rel_path }} |
| No data available for the selected timeframe. | -|
No data available for the selected timeframe and category.
+ {% endif %}