make node_sort_order an .env variable
This commit is contained in:
parent
cb330bc6e8
commit
658e04addb
@ -121,3 +121,11 @@ REVERSE_PROXY_AUTH=0
|
|||||||
# when running under the same database
|
# when running under the same database
|
||||||
# SESSION_COOKIE_DOMAIN=.example.com
|
# SESSION_COOKIE_DOMAIN=.example.com
|
||||||
# SESSION_COOKIE_NAME=sessionid # use this only to not interfere with non unified django applications under the same top level domain
|
# SESSION_COOKIE_NAME=sessionid # use this only to not interfere with non unified django applications under the same top level domain
|
||||||
|
|
||||||
|
|
||||||
|
# by default SORT_TREE_BY_NAME is enabled this will store all Keywords and Food in case sensitive order
|
||||||
|
# this setting makes saving new keywords and foods very slow, which doesn't matter in most usecases.
|
||||||
|
# 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
|
||||||
|
# SORT_TREE_BY_NAME=0
|
@ -89,7 +89,7 @@ class SyncLogAdmin(admin.ModelAdmin):
|
|||||||
admin.site.register(SyncLog, SyncLogAdmin)
|
admin.site.register(SyncLog, SyncLogAdmin)
|
||||||
|
|
||||||
|
|
||||||
@admin.action(description='Sort tree by name')
|
@admin.action(description='Fix problems and sort tree by name')
|
||||||
def sort_tree(modeladmin, request, queryset):
|
def sort_tree(modeladmin, request, queryset):
|
||||||
modeladmin.model.node_order_by = ['name']
|
modeladmin.model.node_order_by = ['name']
|
||||||
with scopes_disabled():
|
with scopes_disabled():
|
||||||
|
@ -19,7 +19,7 @@ from treebeard.mp_tree import MP_Node, MP_NodeManager
|
|||||||
from django_scopes import ScopedManager, scopes_disabled
|
from django_scopes import ScopedManager, scopes_disabled
|
||||||
from django_prometheus.models import ExportModelOperationsMixin
|
from django_prometheus.models import ExportModelOperationsMixin
|
||||||
from recipes.settings import (COMMENT_PREF_DEFAULT, FRACTION_PREF_DEFAULT,
|
from recipes.settings import (COMMENT_PREF_DEFAULT, FRACTION_PREF_DEFAULT,
|
||||||
STICKY_NAV_PREF_DEFAULT)
|
STICKY_NAV_PREF_DEFAULT, SORT_TREE_BY_NAME)
|
||||||
|
|
||||||
|
|
||||||
def get_user_name(self):
|
def get_user_name(self):
|
||||||
@ -335,8 +335,8 @@ class SyncLog(models.Model, PermissionModelMixin):
|
|||||||
|
|
||||||
|
|
||||||
class Keyword(ExportModelOperationsMixin('keyword'), TreeModel, PermissionModelMixin):
|
class Keyword(ExportModelOperationsMixin('keyword'), TreeModel, PermissionModelMixin):
|
||||||
# TODO add find and fix problem functions
|
if SORT_TREE_BY_NAME:
|
||||||
# node_order_by = ['name']
|
node_order_by = ['name']
|
||||||
name = models.CharField(max_length=64)
|
name = models.CharField(max_length=64)
|
||||||
icon = models.CharField(max_length=16, blank=True, null=True)
|
icon = models.CharField(max_length=16, blank=True, null=True)
|
||||||
description = models.TextField(default="", blank=True)
|
description = models.TextField(default="", blank=True)
|
||||||
@ -370,8 +370,8 @@ class Unit(ExportModelOperationsMixin('unit'), models.Model, PermissionModelMixi
|
|||||||
|
|
||||||
|
|
||||||
class Food(ExportModelOperationsMixin('food'), TreeModel, PermissionModelMixin):
|
class Food(ExportModelOperationsMixin('food'), TreeModel, PermissionModelMixin):
|
||||||
# TODO add find and fix problem functions
|
if SORT_TREE_BY_NAME:
|
||||||
# node_order_by = ['name']
|
node_order_by = ['name']
|
||||||
name = models.CharField(max_length=128, validators=[MinLengthValidator(1)])
|
name = models.CharField(max_length=128, validators=[MinLengthValidator(1)])
|
||||||
recipe = models.ForeignKey('Recipe', null=True, blank=True, on_delete=models.SET_NULL)
|
recipe = models.ForeignKey('Recipe', null=True, blank=True, on_delete=models.SET_NULL)
|
||||||
supermarket_category = models.ForeignKey(SupermarketCategory, null=True, blank=True, on_delete=models.SET_NULL)
|
supermarket_category = models.ForeignKey(SupermarketCategory, null=True, blank=True, on_delete=models.SET_NULL)
|
||||||
|
@ -21,6 +21,10 @@ LIST_URL = 'api:food-list'
|
|||||||
DETAIL_URL = 'api:food-detail'
|
DETAIL_URL = 'api:food-detail'
|
||||||
MOVE_URL = 'api:food-move'
|
MOVE_URL = 'api:food-move'
|
||||||
MERGE_URL = 'api:food-merge'
|
MERGE_URL = 'api:food-merge'
|
||||||
|
if (Food.node_order_by):
|
||||||
|
node_location = 'sorted-child'
|
||||||
|
else:
|
||||||
|
node_location = 'last-child'
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
@ -429,7 +433,7 @@ def test_root_filter(obj_1, obj_1_1, obj_1_1_1, obj_2, obj_3, u1_s1):
|
|||||||
assert len(response['results']) == 2
|
assert len(response['results']) == 2
|
||||||
|
|
||||||
with scopes_disabled():
|
with scopes_disabled():
|
||||||
obj_2.move(obj_1, 'last-child')
|
obj_2.move(obj_1, node_location)
|
||||||
# should return direct children of obj_1 (obj_1_1, obj_2), ignoring query filters
|
# should return direct children of obj_1 (obj_1_1, obj_2), ignoring query filters
|
||||||
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?root={obj_1.id}').content)
|
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?root={obj_1.id}').content)
|
||||||
assert response['count'] == 2
|
assert response['count'] == 2
|
||||||
@ -439,7 +443,7 @@ def test_root_filter(obj_1, obj_1_1, obj_1_1_1, obj_2, obj_3, u1_s1):
|
|||||||
|
|
||||||
def test_tree_filter(obj_1, obj_1_1, obj_1_1_1, obj_2, obj_3, u1_s1):
|
def test_tree_filter(obj_1, obj_1_1, obj_1_1_1, obj_2, obj_3, u1_s1):
|
||||||
with scopes_disabled():
|
with scopes_disabled():
|
||||||
obj_2.move(obj_1, 'last-child')
|
obj_2.move(obj_1, node_location)
|
||||||
# should return full tree starting at obj_1 (obj_1_1_1, obj_2), ignoring query filters
|
# should return full tree starting at obj_1 (obj_1_1_1, obj_2), ignoring query filters
|
||||||
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?tree={obj_1.id}').content)
|
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?tree={obj_1.id}').content)
|
||||||
assert response['count'] == 4
|
assert response['count'] == 4
|
||||||
|
@ -20,7 +20,10 @@ LIST_URL = 'api:keyword-list'
|
|||||||
DETAIL_URL = 'api:keyword-detail'
|
DETAIL_URL = 'api:keyword-detail'
|
||||||
MOVE_URL = 'api:keyword-move'
|
MOVE_URL = 'api:keyword-move'
|
||||||
MERGE_URL = 'api:keyword-merge'
|
MERGE_URL = 'api:keyword-merge'
|
||||||
|
if (Keyword.node_order_by):
|
||||||
|
node_location = 'sorted-child'
|
||||||
|
else:
|
||||||
|
node_location = 'last-child'
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
def obj_1(space_1):
|
def obj_1(space_1):
|
||||||
@ -350,7 +353,7 @@ def test_root_filter(obj_1, obj_1_1, obj_1_1_1, obj_2, obj_3, u1_s1):
|
|||||||
assert len(response['results']) == 2
|
assert len(response['results']) == 2
|
||||||
|
|
||||||
with scopes_disabled():
|
with scopes_disabled():
|
||||||
obj_2.move(obj_1, 'last-child')
|
obj_2.move(obj_1, node_location)
|
||||||
# should return direct children of obj_1 (obj_1_1, obj_2), ignoring query filters
|
# should return direct children of obj_1 (obj_1_1, obj_2), ignoring query filters
|
||||||
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?root={obj_1.id}').content)
|
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?root={obj_1.id}').content)
|
||||||
assert response['count'] == 2
|
assert response['count'] == 2
|
||||||
@ -360,7 +363,7 @@ def test_root_filter(obj_1, obj_1_1, obj_1_1_1, obj_2, obj_3, u1_s1):
|
|||||||
|
|
||||||
def test_tree_filter(obj_1, obj_1_1, obj_1_1_1, obj_2, obj_3, u1_s1):
|
def test_tree_filter(obj_1, obj_1_1, obj_1_1_1, obj_2, obj_3, u1_s1):
|
||||||
with scopes_disabled():
|
with scopes_disabled():
|
||||||
obj_2.move(obj_1, 'last-child')
|
obj_2.move(obj_1, node_location)
|
||||||
# should return full tree starting at obj_1 (obj_1_1_1, obj_2), ignoring query filters
|
# should return full tree starting at obj_1 (obj_1_1_1, obj_2), ignoring query filters
|
||||||
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?tree={obj_1.id}').content)
|
response = json.loads(u1_s1.get(f'{reverse(LIST_URL)}?tree={obj_1.id}').content)
|
||||||
assert response['count'] == 4
|
assert response['count'] == 4
|
||||||
|
@ -189,9 +189,14 @@ class MergeMixin(ViewSetMixin): # TODO update Units to use merge API
|
|||||||
# a new scenario exists and needs to be handled
|
# a new scenario exists and needs to be handled
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
if isTree:
|
if isTree:
|
||||||
|
if self.model.node_order_by:
|
||||||
|
node_location = 'sorted-child'
|
||||||
|
else:
|
||||||
|
node_location = 'last-child'
|
||||||
|
|
||||||
children = source.get_children().exclude(id=target.id)
|
children = source.get_children().exclude(id=target.id)
|
||||||
for c in children:
|
for c in children:
|
||||||
c.move(target, 'last-child')
|
c.move(target, node_location)
|
||||||
content = {'msg': _(f'{source.name} was merged successfully with {target.name}')}
|
content = {'msg': _(f'{source.name} was merged successfully with {target.name}')}
|
||||||
source.delete()
|
source.delete()
|
||||||
return Response(content, status=status.HTTP_200_OK)
|
return Response(content, status=status.HTTP_200_OK)
|
||||||
@ -232,6 +237,10 @@ class TreeMixin(MergeMixin, FuzzyFilterMixin):
|
|||||||
@decorators.renderer_classes((TemplateHTMLRenderer, JSONRenderer))
|
@decorators.renderer_classes((TemplateHTMLRenderer, JSONRenderer))
|
||||||
def move(self, request, pk, parent):
|
def move(self, request, pk, parent):
|
||||||
self.description = f"Move {self.basename} to be a child of {self.basename} with ID of [int]. Use ID: 0 to move {self.basename} to the root."
|
self.description = f"Move {self.basename} to be a child of {self.basename} with ID of [int]. Use ID: 0 to move {self.basename} to the root."
|
||||||
|
if self.model.node_order_by:
|
||||||
|
node_location = 'sorted'
|
||||||
|
else:
|
||||||
|
node_location = 'last'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
child = self.model.objects.get(pk=pk, space=self.request.space)
|
child = self.model.objects.get(pk=pk, space=self.request.space)
|
||||||
@ -244,7 +253,7 @@ class TreeMixin(MergeMixin, FuzzyFilterMixin):
|
|||||||
if parent == 0:
|
if parent == 0:
|
||||||
try:
|
try:
|
||||||
with scopes_disabled():
|
with scopes_disabled():
|
||||||
child.move(self.model.get_first_root_node(), 'last-sibling')
|
child.move(self.model.get_first_root_node(), f'{node_location}-sibling')
|
||||||
content = {'msg': _(f'{child.name} was moved successfully to the root.')}
|
content = {'msg': _(f'{child.name} was moved successfully to the root.')}
|
||||||
return Response(content, status=status.HTTP_200_OK)
|
return Response(content, status=status.HTTP_200_OK)
|
||||||
except (PathOverflow, InvalidMoveToDescendant, InvalidPosition):
|
except (PathOverflow, InvalidMoveToDescendant, InvalidPosition):
|
||||||
@ -262,7 +271,7 @@ class TreeMixin(MergeMixin, FuzzyFilterMixin):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
with scopes_disabled():
|
with scopes_disabled():
|
||||||
child.move(parent, 'last-child')
|
child.move(parent, f'{node_location}-child')
|
||||||
content = {'msg': _(f'{child.name} was moved successfully to parent {parent.name}')}
|
content = {'msg': _(f'{child.name} was moved successfully to parent {parent.name}')}
|
||||||
return Response(content, status=status.HTTP_200_OK)
|
return Response(content, status=status.HTTP_200_OK)
|
||||||
except (PathOverflow, InvalidMoveToDescendant, InvalidPosition):
|
except (PathOverflow, InvalidMoveToDescendant, InvalidPosition):
|
||||||
|
@ -73,6 +73,7 @@ ACCOUNT_SIGNUP_FORM_CLASS = 'cookbook.forms.AllAuthSignupForm'
|
|||||||
TERMS_URL = os.getenv('TERMS_URL', '')
|
TERMS_URL = os.getenv('TERMS_URL', '')
|
||||||
PRIVACY_URL = os.getenv('PRIVACY_URL', '')
|
PRIVACY_URL = os.getenv('PRIVACY_URL', '')
|
||||||
IMPRINT_URL = os.getenv('IMPRINT_URL', '')
|
IMPRINT_URL = os.getenv('IMPRINT_URL', '')
|
||||||
|
SORT_TREE_BY_NAME = os.getenv('IMPRINT_URL', 1)
|
||||||
|
|
||||||
HOSTED = bool(int(os.getenv('HOSTED', False)))
|
HOSTED = bool(int(os.getenv('HOSTED', False)))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user