fix post_save signal for sqlite

This commit is contained in:
smilerz
2021-12-15 13:23:22 -06:00
parent e33ca876a6
commit 682f4a4297
4 changed files with 24 additions and 16 deletions

View File

@@ -12,10 +12,7 @@ class CookbookConfig(AppConfig):
name = 'cookbook' name = 'cookbook'
def ready(self): def ready(self):
# post_save signal is only necessary if using full-text search on postgres import cookbook.signals # noqa
if settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2',
'django.db.backends.postgresql']:
import cookbook.signals # noqa
if not settings.DISABLE_TREE_FIX_STARTUP: if not settings.DISABLE_TREE_FIX_STARTUP:
# when starting up run fix_tree to: # when starting up run fix_tree to:
@@ -23,7 +20,7 @@ class CookbookConfig(AppConfig):
# b) fix problems, if any, with tree consistency # b) fix problems, if any, with tree consistency
with scopes_disabled(): with scopes_disabled():
try: try:
from cookbook.models import Keyword, Food from cookbook.models import Food, Keyword
Keyword.fix_tree(fix_paths=True) Keyword.fix_tree(fix_paths=True)
Food.fix_tree(fix_paths=True) Food.fix_tree(fix_paths=True)
except OperationalError: except OperationalError:

View File

@@ -1,6 +1,7 @@
from decimal import Decimal from decimal import Decimal
from functools import wraps from functools import wraps
from django.conf import settings
from django.contrib.postgres.search import SearchVector from django.contrib.postgres.search import SearchVector
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.dispatch import receiver from django.dispatch import receiver
@@ -11,8 +12,14 @@ from cookbook.managers import DICTIONARY
from cookbook.models import (Food, FoodInheritField, Ingredient, MealPlan, Recipe, from cookbook.models import (Food, FoodInheritField, Ingredient, MealPlan, Recipe,
ShoppingListEntry, Step) ShoppingListEntry, Step)
SQLITE = True
if settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2',
'django.db.backends.postgresql']:
SQLITE = False
# wraps a signal with the ability to set 'skip_signal' to avoid creating recursive signals # wraps a signal with the ability to set 'skip_signal' to avoid creating recursive signals
def skip_signal(signal_func): def skip_signal(signal_func):
@wraps(signal_func) @wraps(signal_func)
def _decorator(sender, instance, **kwargs): def _decorator(sender, instance, **kwargs):
@@ -27,6 +34,8 @@ def skip_signal(signal_func):
@receiver(post_save, sender=Recipe) @receiver(post_save, sender=Recipe)
@skip_signal @skip_signal
def update_recipe_search_vector(sender, instance=None, created=False, **kwargs): def update_recipe_search_vector(sender, instance=None, created=False, **kwargs):
if SQLITE:
return
language = DICTIONARY.get(translation.get_language(), 'simple') language = DICTIONARY.get(translation.get_language(), 'simple')
instance.name_search_vector = SearchVector('name__unaccent', weight='A', config=language) instance.name_search_vector = SearchVector('name__unaccent', weight='A', config=language)
instance.desc_search_vector = SearchVector('description__unaccent', weight='C', config=language) instance.desc_search_vector = SearchVector('description__unaccent', weight='C', config=language)
@@ -40,6 +49,8 @@ def update_recipe_search_vector(sender, instance=None, created=False, **kwargs):
@receiver(post_save, sender=Step) @receiver(post_save, sender=Step)
@skip_signal @skip_signal
def update_step_search_vector(sender, instance=None, created=False, **kwargs): def update_step_search_vector(sender, instance=None, created=False, **kwargs):
if SQLITE:
return
language = DICTIONARY.get(translation.get_language(), 'simple') language = DICTIONARY.get(translation.get_language(), 'simple')
instance.search_vector = SearchVector('instruction__unaccent', weight='B', config=language) instance.search_vector = SearchVector('instruction__unaccent', weight='B', config=language)
try: try:

View File

@@ -97,7 +97,7 @@ class SupermarketCategoryFactory(factory.django.DjangoModelFactory):
@register @register
class FoodFactory(factory.django.DjangoModelFactory): class FoodFactory(factory.django.DjangoModelFactory):
"""Food factory.""" """Food factory."""
name = factory.LazyAttribute(lambda x: faker.sentence(nb_words=3)) name = factory.LazyAttribute(lambda x: faker.sentence(nb_words=3, variable_nb_words=False))
description = factory.LazyAttribute(lambda x: faker.sentence(nb_words=10)) description = factory.LazyAttribute(lambda x: faker.sentence(nb_words=10))
supermarket_category = factory.Maybe( supermarket_category = factory.Maybe(
factory.LazyAttribute(lambda x: x.has_category), factory.LazyAttribute(lambda x: x.has_category),
@@ -135,7 +135,7 @@ class UnitFactory(factory.django.DjangoModelFactory):
@register @register
class KeywordFactory(factory.django.DjangoModelFactory): class KeywordFactory(factory.django.DjangoModelFactory):
"""Keyword factory.""" """Keyword factory."""
name = factory.LazyAttribute(lambda x: faker.sentence(nb_words=3)) name = factory.LazyAttribute(lambda x: faker.sentence(nb_words=2, variable_nb_words=False))
# icon = models.CharField(max_length=16, blank=True, null=True) # icon = models.CharField(max_length=16, blank=True, null=True)
description = factory.LazyAttribute(lambda x: faker.sentence(nb_words=10)) description = factory.LazyAttribute(lambda x: faker.sentence(nb_words=10))
space = factory.SubFactory(SpaceFactory) space = factory.SubFactory(SpaceFactory)
@@ -156,7 +156,7 @@ class IngredientFactory(factory.django.DjangoModelFactory):
food = factory.SubFactory(FoodFactory, space=factory.SelfAttribute('..space')) food = factory.SubFactory(FoodFactory, space=factory.SelfAttribute('..space'))
unit = factory.SubFactory(UnitFactory, space=factory.SelfAttribute('..space')) unit = factory.SubFactory(UnitFactory, space=factory.SelfAttribute('..space'))
amount = factory.LazyAttribute(lambda x: faker.random_int(min=1, max=10)) amount = factory.LazyAttribute(lambda x: faker.random_int(min=1, max=10))
note = factory.LazyAttribute(lambda x: faker.sentence(nb_words=5)) note = factory.LazyAttribute(lambda x: faker.sentence(nb_words=8))
space = factory.SubFactory(SpaceFactory) space = factory.SubFactory(SpaceFactory)
class Meta: class Meta:

View File

@@ -308,14 +308,14 @@ else:
# } # }
# SQLite testing DB # SQLite testing DB
# DATABASES = { DATABASES = {
# 'default': { 'default': {
# 'ENGINE': 'django.db.backends.sqlite3', 'ENGINE': 'django.db.backends.sqlite3',
# 'OPTIONS': ast.literal_eval(os.getenv('DB_OPTIONS')) if os.getenv('DB_OPTIONS') else {}, 'OPTIONS': ast.literal_eval(os.getenv('DB_OPTIONS')) if os.getenv('DB_OPTIONS') else {},
# 'NAME': 'db.sqlite3', 'NAME': 'db.sqlite3',
# 'CONN_MAX_AGE': 600, 'CONN_MAX_AGE': 600,
# } }
# } }
CACHES = { CACHES = {
'default': { 'default': {