updated to handle sqlite

This commit is contained in:
smilerz 2021-04-11 17:01:18 -05:00
parent b741d827b3
commit 43d223cf02
5 changed files with 19 additions and 13 deletions

View File

@ -1,8 +1,11 @@
from django.apps import AppConfig from django.apps import AppConfig
from django.conf import settings
class CookbookConfig(AppConfig): 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
if settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql']:
import cookbook.signals # noqa import cookbook.signals # noqa

View File

@ -17,8 +17,8 @@ class RecipeSearchManager(models.Manager):
) )
search_vectors = ( search_vectors = (
SearchVector('search_vector') SearchVector('search_vector')
+ SearchVector(StringAgg('steps__ingredients__food__name', delimiter=' '), weight='A', config='english') + SearchVector(StringAgg('steps__ingredients__food__name', delimiter=' '), weight='B', config='english')
+ SearchVector(StringAgg('keywords__name', delimiter=' '), weight='A', config='english')) + SearchVector(StringAgg('keywords__name', delimiter=' '), weight='B', config='english'))
search_rank = SearchRank(search_vectors, search_query) search_rank = SearchRank(search_vectors, search_query)
# trigram_similarity = TrigramSimilarity( # trigram_similarity = TrigramSimilarity(
# 'headline', search_text # 'headline', search_text

View File

@ -1,5 +1,5 @@
# Generated by Django 3.1.7 on 2021-04-07 20:00 # Generated by Django 3.1.7 on 2021-04-07 20:00
from django.conf import settings
from django.contrib.postgres.indexes import GinIndex from django.contrib.postgres.indexes import GinIndex
from django.contrib.postgres.search import SearchVectorField, SearchVector from django.contrib.postgres.search import SearchVectorField, SearchVector
from django.db import migrations from django.db import migrations
@ -8,6 +8,8 @@ from cookbook.models import Recipe
def set_default_search_vector(apps, schema_editor): def set_default_search_vector(apps, schema_editor):
if settings.DATABASES['default']['ENGINE'] not in ['django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql']:
return
with scopes_disabled(): with scopes_disabled():
search_vector = ( search_vector = (
SearchVector('name', weight='A') SearchVector('name', weight='A')
@ -16,11 +18,9 @@ def set_default_search_vector(apps, schema_editor):
class Migration(migrations.Migration): class Migration(migrations.Migration):
dependencies = [ dependencies = [
('cookbook', '0118_auto_20210406_1805'), ('cookbook', '0118_auto_20210406_1805'),
] ]
operations = [ operations = [
migrations.AddField( migrations.AddField(
model_name='recipe', model_name='recipe',

View File

@ -3,6 +3,7 @@ import uuid
from datetime import date, timedelta from datetime import date, timedelta
from annoying.fields import AutoOneToOneField from annoying.fields import AutoOneToOneField
from django.conf import settings
from django.contrib import auth from django.contrib import auth
from django.contrib.auth.models import Group, User from django.contrib.auth.models import Group, User
from django.contrib.postgres.indexes import GinIndex from django.contrib.postgres.indexes import GinIndex
@ -385,10 +386,6 @@ class NutritionInformation(models.Model, PermissionModelMixin):
return 'Nutrition' return 'Nutrition'
# TODO adjust model based on DB capabilities
# options to have multiple recipe models based on DB capability (to drive search)
# required_db_features, required-db-vendor
# https://docs.djangoproject.com/en/3.1/ref/models/options/#required-db-vendor
class Recipe(models.Model, PermissionModelMixin): class Recipe(models.Model, PermissionModelMixin):
name = models.CharField(max_length=128) name = models.CharField(max_length=128)
description = models.CharField(max_length=512, blank=True, null=True) description = models.CharField(max_length=512, blank=True, null=True)
@ -413,10 +410,15 @@ class Recipe(models.Model, PermissionModelMixin):
created_by = models.ForeignKey(User, on_delete=models.PROTECT) created_by = models.ForeignKey(User, on_delete=models.PROTECT)
created_at = models.DateTimeField(auto_now_add=True) created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True) updated_at = models.DateTimeField(auto_now=True)
search_vector = SearchVectorField(null=True)
search_vector = SearchVectorField(null=True)
space = models.ForeignKey(Space, on_delete=models.CASCADE) space = models.ForeignKey(Space, on_delete=models.CASCADE)
# load custom manager for full text search if postgress is available
if settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql']:
objects = ScopedManager(_manager_class=RecipeSearchManager, space='space') objects = ScopedManager(_manager_class=RecipeSearchManager, space='space')
else:
objects = ScopedManager(space='space')
def __str__(self): def __str__(self):
return self.name return self.name

View File

@ -10,12 +10,13 @@ def update_recipe_search_vector(sender, instance=None, created=False, **kwargs):
if not instance: if not instance:
return return
# needed to ensure search vector update doesn't trigger recursion
if hasattr(instance, '_dirty'): if hasattr(instance, '_dirty'):
return return
instance.search_vector = ( instance.search_vector = (
SearchVector('name', weight='A', config='english') SearchVector('name', weight='A', config='english')
+ SearchVector('description', weight='B', config='english') + SearchVector('description', weight='C', config='english')
) )
try: try: