allow admin to enable/disable sorting

This commit is contained in:
smilerz 2021-09-28 12:51:49 -05:00
parent 20c2c5cc40
commit 39255c93a5
3 changed files with 28 additions and 3 deletions

View File

@ -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

View File

@ -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)

View File

@ -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()