From 39255c93a5655ed35a469888df2e62c430fadb03 Mon Sep 17 00:00:00 2001 From: smilerz Date: Tue, 28 Sep 2021 12:51:49 -0500 Subject: [PATCH] allow admin to enable/disable sorting --- .env.template | 1 + cookbook/admin.py | 21 ++++++++++++++++++--- recipes/wsgi.py | 9 +++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/.env.template b/.env.template index 9fb82192..bff19860 100644 --- a/.env.template +++ b/.env.template @@ -128,4 +128,5 @@ REVERSE_PROXY_AUTH=0 # however, when doing large imports of recipes that will create new objects, can increase total run time by 5-10x # Disabling SORT_TREE_BY_NAME (setting value to 0) will store objects unsorted, but will substantially increase speed of imports. # Keywords and Food can be manually sorted by name in Admin +# This value can also be temporarily changed in Admin, it will revert the next time the application is started # SORT_TREE_BY_NAME=0 \ No newline at end of file diff --git a/cookbook/admin.py b/cookbook/admin.py index 77fc06da..f86232cb 100644 --- a/cookbook/admin.py +++ b/cookbook/admin.py @@ -89,19 +89,34 @@ class SyncLogAdmin(admin.ModelAdmin): admin.site.register(SyncLog, SyncLogAdmin) +@admin.action(description='Temporarily ENABLE sorting on Foods and Keywords.') +def enable_tree_sorting(modeladmin, request, queryset): + Food.node_order_by = ['name'] + Keyword.node_order_by = ['name'] + with scopes_disabled(): + Food.fix_tree(fix_paths=True) + Keyword.fix_tree(fix_paths=True) + + +@admin.action(description='Temporarily DISABLE sorting on Foods and Keywords.') +def disable_tree_sorting(modeladmin, request, queryset): + Food.node_order_by = [] + Keyword.node_order_by = [] + + @admin.action(description='Fix problems and sort tree by name') def sort_tree(modeladmin, request, queryset): orginal_value = modeladmin.model.node_order_by[:] modeladmin.model.node_order_by = ['name'] with scopes_disabled(): - Keyword.fix_tree(fix_paths=True) + modeladmin.model.fix_tree(fix_paths=True) modeladmin.model.node_order_by = orginal_value class KeywordAdmin(TreeAdmin): form = movenodeform_factory(Keyword) ordering = ('space', 'path',) - actions = [sort_tree] + actions = [sort_tree, enable_tree_sorting, disable_tree_sorting] admin.site.register(Keyword, KeywordAdmin) @@ -148,7 +163,7 @@ admin.site.register(Unit) class FoodAdmin(TreeAdmin): form = movenodeform_factory(Keyword) ordering = ('space', 'path',) - actions = [sort_tree] + actions = [sort_tree, enable_tree_sorting, disable_tree_sorting] admin.site.register(Food, FoodAdmin) diff --git a/recipes/wsgi.py b/recipes/wsgi.py index a33e55a2..474e7f41 100644 --- a/recipes/wsgi.py +++ b/recipes/wsgi.py @@ -10,9 +10,18 @@ https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/ import os from django.core.wsgi import get_wsgi_application +from django_scopes import scopes_disabled +from cookbook.models import Food, Keyword os.environ.setdefault("DJANGO_SETTINGS_MODULE", "recipes.settings") +# run fix tree here solves 2 problems: +# 1 if tree sorting is changed from unsorted to sorted ensures that objects are sorted +# 2 if any condition caused the tree to be in an inconsistent state this will fix most problems +with scopes_disabled(): + Food.fix_tree(fix_paths=True) + Keyword.fix_tree(fix_paths=True) + _application = get_wsgi_application()