From 556f74cc2b43d01464405b20cfb1352a4e529548 Mon Sep 17 00:00:00 2001 From: smilerz Date: Sun, 11 Apr 2021 19:42:22 -0500 Subject: [PATCH] rebuild recipe index from admin site --- cookbook/admin.py | 15 +++++++++++++++ cookbook/management/commands/rebuildindex.py | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/cookbook/admin.py b/cookbook/admin.py index 2301ca7fb..14dc0fca4 100644 --- a/cookbook/admin.py +++ b/cookbook/admin.py @@ -1,6 +1,9 @@ +from django.conf import settings from django.contrib import admin +from django.contrib.postgres.search import SearchVector from django.contrib.auth.admin import UserAdmin from django.contrib.auth.models import User, Group +from django_scopes import scopes_disabled from .models import (Comment, CookLog, Food, Ingredient, InviteLink, Keyword, MealPlan, MealType, NutritionInformation, Recipe, @@ -91,6 +94,15 @@ class StepAdmin(admin.ModelAdmin): admin.site.register(Step, StepAdmin) +@admin.action(description='Rebuild index for selected recipes') +def rebuild_index(modeladmin, request, queryset): + with scopes_disabled(): + search_vector = ( + SearchVector('name', weight='A') + + SearchVector('description', weight='B')) + queryset.update(search_vector=search_vector) + + class RecipeAdmin(admin.ModelAdmin): list_display = ('name', 'internal', 'created_by', 'storage') search_fields = ('name', 'created_by__username') @@ -101,6 +113,9 @@ class RecipeAdmin(admin.ModelAdmin): def created_by(obj): return obj.created_by.get_user_name() + if settings.DATABASES['default']['ENGINE'] in ['django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql']: + actions = [rebuild_index] + admin.site.register(Recipe, RecipeAdmin) diff --git a/cookbook/management/commands/rebuildindex.py b/cookbook/management/commands/rebuildindex.py index d123e9d5b..e5f46c429 100644 --- a/cookbook/management/commands/rebuildindex.py +++ b/cookbook/management/commands/rebuildindex.py @@ -1,6 +1,6 @@ from django.conf import settings from django.contrib.postgres.search import SearchVector -from django.core.management.base import BaseCommand, CommandError +from django.core.management.base import BaseCommand from django_scopes import scopes_disabled from django.utils.translation import gettext_lazy as _