diff --git a/.env.template b/.env.template index bff19860..cb873e0a 100644 --- a/.env.template +++ b/.env.template @@ -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 -# 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. +# by default SORT_TREE_BY_NAME is disabled this will store all Keywords and Food in the order they are created +# 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 10-15x # 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 will be fixed/changed in the future by changing the implementation or finding a better workaround for sorting # SORT_TREE_BY_NAME=0 \ No newline at end of file diff --git a/cookbook/integration/integration.py b/cookbook/integration/integration.py index 9e118ec8..fe1c10a6 100644 --- a/cookbook/integration/integration.py +++ b/cookbook/integration/integration.py @@ -5,7 +5,9 @@ import uuid from io import BytesIO, StringIO from zipfile import ZipFile, BadZipFile +from django.core.exceptions import ObjectDoesNotExist from django.core.files import File +from django.db import IntegrityError from django.http import HttpResponse from django.utils.formats import date_format 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}' 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']: - parent, created = Keyword.objects.get_or_create(name='Import', space=request.space) + try: + 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( name=name, description=description, icon=icon, space=request.space ) - else: - self.keyword, created = Keyword.objects.get_or_create( - name=name, + except IntegrityError: # in case, for whatever reason, the name does exist append UUID to it. Not nice but works for now. + self.keyword = parent.add_child( + name=f'{name} {str(uuid.uuid4())[0:8]}', description=description, icon=icon, space=request.space diff --git a/recipes/settings.py b/recipes/settings.py index 944ac327..d0c60e43 100644 --- a/recipes/settings.py +++ b/recipes/settings.py @@ -149,7 +149,7 @@ MIDDLEWARE = [ '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))): MIDDLEWARE += ('recipes.middleware.SqlPrintingMiddleware',)