importer tweaks and default tree sort setting

This commit is contained in:
vabene1111 2021-10-08 10:20:42 +02:00
parent 2c27e528ae
commit 15b722345b
3 changed files with 18 additions and 12 deletions

View File

@ -123,10 +123,10 @@ REVERSE_PROXY_AUTH=0
# 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 # by default SORT_TREE_BY_NAME is disabled this will store all Keywords and Food in the order they are created
# this setting makes saving new keywords and foods very slow, which doesn't matter in most usecases. # enabling 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 # however, when doing large imports of recipes that will create new objects, can increase total run time by 10-15x
# 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 # 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 # This value can also be temporarily changed in Admin, it will revert the next time the application is started
# This will be fixed/changed in the future by changing the implementation or finding a better workaround for sorting
# SORT_TREE_BY_NAME=0 # SORT_TREE_BY_NAME=0

View File

@ -5,7 +5,9 @@ import uuid
from io import BytesIO, StringIO from io import BytesIO, StringIO
from zipfile import ZipFile, BadZipFile from zipfile import ZipFile, BadZipFile
from django.core.exceptions import ObjectDoesNotExist
from django.core.files import File from django.core.files import File
from django.db import IntegrityError
from django.http import HttpResponse from django.http import HttpResponse
from django.utils.formats import date_format from django.utils.formats import date_format
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
@ -35,20 +37,24 @@ class Integration:
description = f'Imported by {request.user.get_user_name()} at {date_format(datetime.datetime.now(), "DATETIME_FORMAT")}. Type: {export_type}' description = f'Imported by {request.user.get_user_name()} at {date_format(datetime.datetime.now(), "DATETIME_FORMAT")}. Type: {export_type}'
icon = '📥' icon = '📥'
count = Keyword.objects.filter(name__icontains='Import', space=request.space).count()
name = f'Import {count + 1}'
if DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql']: try:
parent, created = Keyword.objects.get_or_create(name='Import', space=request.space) last_kw = Keyword.objects.filter(name__regex=r'^(Import [0-9]+)', space=request.space).latest('created_at')
name = f'Import {int(last_kw.name.replace("Import ", "")) + 1}'
except ObjectDoesNotExist:
name = 'Import 1'
parent, created = Keyword.objects.get_or_create(name='Import', space=request.space)
try:
self.keyword = parent.add_child( self.keyword = parent.add_child(
name=name, name=name,
description=description, description=description,
icon=icon, icon=icon,
space=request.space space=request.space
) )
else: except IntegrityError: # in case, for whatever reason, the name does exist append UUID to it. Not nice but works for now.
self.keyword, created = Keyword.objects.get_or_create( self.keyword = parent.add_child(
name=name, name=f'{name} {str(uuid.uuid4())[0:8]}',
description=description, description=description,
icon=icon, icon=icon,
space=request.space space=request.space

View File

@ -149,7 +149,7 @@ MIDDLEWARE = [
'cookbook.helper.scope_middleware.ScopeMiddleware', 'cookbook.helper.scope_middleware.ScopeMiddleware',
] ]
SORT_TREE_BY_NAME = bool(int(os.getenv('SORT_TREE_BY_NAME', True))) SORT_TREE_BY_NAME = bool(int(os.getenv('SORT_TREE_BY_NAME', False)))
if bool(int(os.getenv('SQL_DEBUG', False))): if bool(int(os.getenv('SQL_DEBUG', False))):
MIDDLEWARE += ('recipes.middleware.SqlPrintingMiddleware',) MIDDLEWARE += ('recipes.middleware.SqlPrintingMiddleware',)