From 26ef66f4e13a4e91ab9f538e74425e33d8ec6061 Mon Sep 17 00:00:00 2001 From: vabene1111 Date: Fri, 1 Oct 2021 15:58:16 +0200 Subject: [PATCH] moved fix tree to apps ready hook and wrapped in catch for non existing models --- cookbook/apps.py | 13 +++++++++++++ cookbook/models.py | 14 -------------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/cookbook/apps.py b/cookbook/apps.py index d133d292..65cf8a00 100644 --- a/cookbook/apps.py +++ b/cookbook/apps.py @@ -1,5 +1,7 @@ from django.apps import AppConfig from django.conf import settings +from django.db import OperationalError +from django_scopes import scopes_disabled class CookbookConfig(AppConfig): @@ -9,3 +11,14 @@ class CookbookConfig(AppConfig): # post_save signal is only necessary if using full-text search on postgres if settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql']: import cookbook.signals # noqa + + # when starting up run fix_tree to: + # a) make sure that nodes are sorted when switching between sort modes + # b) fix problems, if any, with tree consistency + with scopes_disabled(): + try: + from cookbook.models import Keyword, Food + Keyword.fix_tree(fix_paths=True) + Food.fix_tree(fix_paths=True) + except OperationalError: + pass # if model does not exist there is no need to fix it diff --git a/cookbook/models.py b/cookbook/models.py index 63db9b65..cf859ee1 100644 --- a/cookbook/models.py +++ b/cookbook/models.py @@ -358,13 +358,6 @@ class Keyword(ExportModelOperationsMixin('keyword'), TreeModel, PermissionModelM indexes = (Index(fields=['id', 'name']),) -# when starting up run fix_tree to: -# a) make sure that nodes are sorted when switching between sort modes -# b) fix problems, if any, with tree consistency -with scopes_disabled(): - Keyword.fix_tree(fix_paths=True) - - class Unit(ExportModelOperationsMixin('unit'), models.Model, PermissionModelMixin): name = models.CharField(max_length=128, validators=[MinLengthValidator(1)]) description = models.TextField(blank=True, null=True) @@ -412,13 +405,6 @@ class Food(ExportModelOperationsMixin('food'), TreeModel, PermissionModelMixin): ) -# when starting up run fix_tree to: -# a) make sure that nodes are sorted when switching between sort modes -# b) fix problems, if any, with tree consistency -with scopes_disabled(): - Food.fix_tree(fix_paths=True) - - class Ingredient(ExportModelOperationsMixin('ingredient'), models.Model, PermissionModelMixin): # a pre-delete signal on Food checks if the Ingredient is part of a Step, if it is raises a ProtectedError instead of cascading the delete food = models.ForeignKey(Food, on_delete=models.CASCADE, null=True, blank=True)