From 07d5ead12820805cc2dc6f5ee87766873a4cfadb Mon Sep 17 00:00:00 2001 From: Chris Scoggins Date: Thu, 27 Jan 2022 10:21:05 -0600 Subject: [PATCH] load RecipeFilter cookbook entries --- cookbook/helper/recipe_search.py | 13 ++++--- vue/src/apps/CookbookView/CookbookView.vue | 36 +++++++++++++++++-- .../RecipeSearchView/RecipeSearchView.vue | 12 ++++--- vue/src/locales/en.json | 2 +- vue/src/utils/openapi/api.ts | 15 ++++---- 5 files changed, 54 insertions(+), 24 deletions(-) diff --git a/cookbook/helper/recipe_search.py b/cookbook/helper/recipe_search.py index df1426a4a..d115e1346 100644 --- a/cookbook/helper/recipe_search.py +++ b/cookbook/helper/recipe_search.py @@ -25,10 +25,11 @@ class RecipeSearch(): def __init__(self, request, **params): self._request = request self._queryset = None - if filter := params.get('filter', None): - try: - self._params = {**json.loads(CustomFilter.objects.get(id=filter).search)} - except CustomFilter.DoesNotExist: + if f := params.get('filter', None): + filter = CustomFilter.objects.filter(id=f, space=self._request.space).filter(Q(created_by=self._request.user) | Q(shared=self._request.user)).first() + if filter: + self._params = {**json.loads(filter.search)} + else: self._params = {**(params or {})} else: self._params = {**(params or {})} @@ -116,9 +117,7 @@ class RecipeSearch(): self.unit_filters(units=self._units) self.string_filters(string=self._string) self._makenow_filter() - - # self._queryset = self._queryset.distinct() # TODO 2x check. maybe add filter of recipe__in after orderby - return self._queryset.filter(space=self._request.space).order_by(*self.orderby) + return self._queryset.filter(space=self._request.space).distinct().order_by(*self.orderby) def _sort_includes(self, *args): for x in args: diff --git a/vue/src/apps/CookbookView/CookbookView.vue b/vue/src/apps/CookbookView/CookbookView.vue index bcdb70045..84d70052d 100644 --- a/vue/src/apps/CookbookView/CookbookView.vue +++ b/vue/src/apps/CookbookView/CookbookView.vue @@ -58,13 +58,13 @@ import "bootstrap-vue/dist/bootstrap-vue.css" import { ApiApiFactory } from "@/utils/openapi/api" import CookbookSlider from "@/components/CookbookSlider" import LoadingSpinner from "@/components/LoadingSpinner" -import { StandardToasts } from "@/utils/utils" +import { StandardToasts, ApiMixin } from "@/utils/utils" Vue.use(BootstrapVue) export default { name: "CookbookView", - mixins: [], + mixins: [ApiMixin], components: { LoadingSpinner, CookbookSlider }, data() { return { @@ -105,8 +105,24 @@ export default { let apiClient = new ApiApiFactory() this.current_book = book + const book_contents = this.cookbooks.filter((b) => { + return b.id == book + })[0] + apiClient.listRecipeBookEntrys({ query: { book: book } }).then((result) => { this.recipes = result.data + if (book_contents.filter) { + var promises = [] + var page = 1 + this.appendRecipeFilter(page, book_contents).then((count) => { + while (count.total > 0) { + page++ + promises.push(this.appendRecipeFilter(page, book_contents)) + count.total = count.total - count.page + } + Promise.all(promises).then() + }) + } this.loading = false }) }, @@ -124,6 +140,22 @@ export default { StandardToasts.makeStandardToast(StandardToasts.FAIL_CREATE) }) }, + appendRecipeFilter: function (page, book) { + let params = { page: page, options: { query: { filter: book.filter.id } } } + return this.genericAPI(this.Models.RECIPE, this.Actions.LIST, params).then((results) => { + let recipes = results.data.results.map((x) => { + return { + id: x.id, + book: book.id, + book_content: book, + recipe: x.id, + recipe_content: x, + } + }) + this.recipes.push(...recipes) + return { total: results.data.count - results.data.results.length, page: results.data.results.length } + }) + }, }, directives: { hover: { diff --git a/vue/src/apps/RecipeSearchView/RecipeSearchView.vue b/vue/src/apps/RecipeSearchView/RecipeSearchView.vue index 92629d3df..7cc4431aa 100644 --- a/vue/src/apps/RecipeSearchView/RecipeSearchView.vue +++ b/vue/src/apps/RecipeSearchView/RecipeSearchView.vue @@ -817,6 +817,7 @@ export default { this.meal_plans.forEach((x) => mealPlans.push(x.recipe.id)) this.recipes = this.recipes.filter((recipe) => !mealPlans.includes(recipe.id)) } + console.log(result.data) }) .then(() => { this.$nextTick(function () { @@ -1065,14 +1066,15 @@ export default { }, saveSearch: function () { let filtername = window.prompt(this.$t("save_filter"), this.$t("filter_name")) + let search = this.buildParams(false) + ;["page", "pageSize"].forEach((key) => { + delete search[key] + }) let params = { name: filtername, - search: JSON.stringify(this.buildParams(false)), + search: JSON.stringify(search), } - let delete_keys = ["page", "pageSize"] - delete_keys.forEach((key) => { - delete params.search[key] - }) + this.genericAPI(this.Models.CUSTOM_FILTER, this.Actions.CREATE, params) .then((result) => { this.search.search_filter = result.data diff --git a/vue/src/locales/en.json b/vue/src/locales/en.json index 9a1a29ae9..dc5740dca 100644 --- a/vue/src/locales/en.json +++ b/vue/src/locales/en.json @@ -318,7 +318,7 @@ "asc": "Ascending", "desc": "Descending", "date_viewed": "Last Viewed", - "date_cooked": "Last Cooked", + "last_cooked": "Last Cooked", "times_cooked": "Times Cooked", "date_created": "Date Created", "show_sortby": "Show Sort By", diff --git a/vue/src/utils/openapi/api.ts b/vue/src/utils/openapi/api.ts index a21b4e710..6dce56bd9 100644 --- a/vue/src/utils/openapi/api.ts +++ b/vue/src/utils/openapi/api.ts @@ -3134,21 +3134,18 @@ export interface UserPreference { * @memberof UserPreference */ shopping_add_onhand?: boolean; -<<<<<<< HEAD -<<<<<<< HEAD -======= ->>>>>>> created CustomFilter model and api /** * * @type {boolean} * @memberof UserPreference */ left_handed?: boolean; -<<<<<<< HEAD -======= ->>>>>>> complex keyword filters -======= ->>>>>>> created CustomFilter model and api + /** + * + * @type {string} + * @memberof UserPreference + */ + food_children_exist?: string; } /**