From 8b1233be62abbfe9d4ddee3fa674b7da3674a8a6 Mon Sep 17 00:00:00 2001 From: smilerz Date: Thu, 13 Jan 2022 12:02:28 -0600 Subject: [PATCH] facets cache-only on initial load --- cookbook/helper/recipe_search.py | 14 ++++-- cookbook/views/api.py | 4 +- .../RecipeSearchView/RecipeSearchView.vue | 50 ++++++++++--------- vue/src/locales/en.json | 6 ++- 4 files changed, 46 insertions(+), 28 deletions(-) diff --git a/cookbook/helper/recipe_search.py b/cookbook/helper/recipe_search.py index e026a0b14..8126baf19 100644 --- a/cookbook/helper/recipe_search.py +++ b/cookbook/helper/recipe_search.py @@ -6,6 +6,7 @@ from django.core.cache import caches from django.db.models import Avg, Case, Count, Func, Max, OuterRef, Q, Subquery, Value, When from django.db.models.functions import Coalesce, Substr from django.utils import timezone, translation +from django.utils.translation import gettext as _ from cookbook.filters import RecipeFilter from cookbook.helper.HelperFunctions import Round, str2bool @@ -259,9 +260,16 @@ class RecipeFacet(): } caches['default'].set(self._SEARCH_CACHE_KEY, self._cache, self._cache_timeout) - def get_facets(self): - if self._cache is None: - pass + def get_facets(self, from_cache=False): + if from_cache: + return { + 'cache_key': self.hash_key or '', + 'Ratings': self.Ratings or {}, + 'Recent': self.Recent or [], + 'Keywords': self.Keywords or [], + 'Foods': self.Foods or [], + 'Books': self.Books or [] + } return { 'cache_key': self.hash_key, 'Ratings': self.get_ratings(), diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 333e0c8bd..a65c22fa5 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -604,6 +604,8 @@ class RecipePagination(PageNumberPagination): max_page_size = 100 def paginate_queryset(self, queryset, request, view=None): + if queryset is None: + raise Exception self.facets = RecipeFacet(request, queryset=queryset) return super().paginate_queryset(queryset, request, view) @@ -613,7 +615,7 @@ class RecipePagination(PageNumberPagination): ('next', self.get_next_link()), ('previous', self.get_previous_link()), ('results', data), - ('facets', self.facets.get_facets()) + ('facets', self.facets.get_facets(from_cache=True)) ])) diff --git a/vue/src/apps/RecipeSearchView/RecipeSearchView.vue b/vue/src/apps/RecipeSearchView/RecipeSearchView.vue index 0e8d25a05..2c0c80f85 100644 --- a/vue/src/apps/RecipeSearchView/RecipeSearchView.vue +++ b/vue/src/apps/RecipeSearchView/RecipeSearchView.vue @@ -98,9 +98,10 @@ v-model="settings.search_keywords" :options="facets.Keywords" :load-options="loadKeywordChildren" - :flat="true" - searchNested :multiple="true" + :flat="true" + :auto-load-root-options="false" + searchNested :placeholder="$t('Keywords')" :normalizer="normalizer" @input="refreshData(false)" @@ -126,9 +127,10 @@ v-model="settings.search_foods" :options="facets.Foods" :load-options="loadFoodChildren" - :flat="true" - searchNested :multiple="true" + :flat="true" + :auto-load-root-options="false" + searchNested :placeholder="$t('Ingredients')" :normalizer="normalizer" @input="refreshData(false)" @@ -400,22 +402,28 @@ export default { if (!this.searchFiltered) { params.options = { query: { last_viewed: this.settings.recently_viewed } } } - this.genericAPI(this.Models.RECIPE, this.Actions.LIST, params).then((result) => { - window.scrollTo(0, 0) - this.pagination_count = result.data.count + this.genericAPI(this.Models.RECIPE, this.Actions.LIST, params) + .then((result) => { + window.scrollTo(0, 0) + this.pagination_count = result.data.count - this.facets = result.data.facets - // if (this.facets?.cache_key) { - // this.getFacets(this.facets.cache_key) - // } - this.recipes = this.removeDuplicates(result.data.results, (recipe) => recipe.id) - if (!this.searchFiltered) { - // if meal plans are being shown - filter out any meal plan recipes from the recipe list - let mealPlans = [] - this.meal_plans.forEach((x) => mealPlans.push(x.recipe.id)) - this.recipes = this.recipes.filter((recipe) => !mealPlans.includes(recipe.id)) - } - }) + this.facets = result.data.facets + // if (this.facets?.cache_key) { + // this.getFacets(this.facets.cache_key) + // } + this.recipes = this.removeDuplicates(result.data.results, (recipe) => recipe.id) + if (!this.searchFiltered) { + // if meal plans are being shown - filter out any meal plan recipes from the recipe list + let mealPlans = [] + this.meal_plans.forEach((x) => mealPlans.push(x.recipe.id)) + this.recipes = this.recipes.filter((recipe) => !mealPlans.includes(recipe.id)) + } + }) + .then(() => { + this.$nextTick(function () { + this.getFacets(this.facets?.cache_key) + }) + }) }, openRandom: function () { this.refreshData(true) @@ -525,8 +533,6 @@ export default { if (this.facets?.cache_key) { this.getFacets(this.facets.cache_key, "food", parentNode.id).then(callback()) } - } else { - callback() } }, loadKeywordChildren({ action, parentNode, callback }) { @@ -538,8 +544,6 @@ export default { if (this.facets?.cache_key) { this.getFacets(this.facets.cache_key, "keyword", parentNode.id).then(callback()) } - } else { - callback() } }, }, diff --git a/vue/src/locales/en.json b/vue/src/locales/en.json index c404ed99a..5d3c70a0c 100644 --- a/vue/src/locales/en.json +++ b/vue/src/locales/en.json @@ -282,5 +282,9 @@ "shopping_add_onhand_desc": "Mark food 'On Hand' when checked off shopping list.", "shopping_add_onhand": "Auto On Hand", "related_recipes": "Related Recipes", - "today_recipes": "Today's Recipes" + "today_recipes": "Today's Recipes", + "mark_complete": "Mark Complete", + "QuickEntry": "Quick Entry", + "shopping_add_onhand_desc": "Mark food 'On Hand' when checked off shopping list.", + "shopping_add_onhand": "Auto On Hand" }