respect ignore_shopping flag

This commit is contained in:
smilerz 2023-09-06 10:27:43 -05:00
parent c72bf57ccb
commit f8f08ae337
No known key found for this signature in database
GPG Key ID: 39444C7606D47126
3 changed files with 173 additions and 168 deletions

View File

@ -176,7 +176,7 @@ class RecipeShoppingEditor():
if not ingredients: if not ingredients:
return return
elif isinstance(ingredients, list): elif isinstance(ingredients, list):
ingredients = Ingredient.objects.filter(id__in=ingredients) ingredients = Ingredient.objects.filter(id__in=ingredients, food__ignore_shopping=False)
existing = self._shopping_list_recipe.entries.filter(ingredient__in=ingredients).values_list('ingredient__pk', flat=True) existing = self._shopping_list_recipe.entries.filter(ingredient__in=ingredients).values_list('ingredient__pk', flat=True)
add_ingredients = ingredients.exclude(id__in=existing) add_ingredients = ingredients.exclude(id__in=existing)

View File

@ -969,10 +969,11 @@ class RecipeViewSet(viewsets.ModelViewSet):
raise PermissionDenied(detail='Not available in demo', code=None) raise PermissionDenied(detail='Not available in demo', code=None)
obj = self.get_object() obj = self.get_object()
ingredients = request.data.get('ingredients', None) ingredients = request.data.get('ingredients', None)
servings = request.data.get('servings', None) servings = request.data.get('servings', None)
list_recipe = request.data.get('list_recipe', None) list_recipe = request.data.get('list_recipe', None)
mealplan = request.data.get('mealplan', None) mealplan = request.data.get('mealplan', None)
SLR = RecipeShoppingEditor(request.user, request.space, id=list_recipe, recipe=obj, mealplan=mealplan) SLR = RecipeShoppingEditor(request.user, request.space, id=list_recipe, recipe=obj, mealplan=mealplan, servings=servings)
content = {'msg': _(f'{obj.name} was added to the shopping list.')} content = {'msg': _(f'{obj.name} was added to the shopping list.')}
http_status = status.HTTP_204_NO_CONTENT http_status = status.HTTP_204_NO_CONTENT

View File

@ -1,190 +1,194 @@
<template> <template>
<div> <div>
<b-modal hide-footer :id="`shopping_${this.modal_id}`" @show="loadRecipe" body-class="p-3 pt-0 pt-md-3"> <b-modal hide-footer :id="`shopping_${this.modal_id}`" @show="loadRecipe" body-class="p-3 pt-0 pt-md-3">
<template v-slot:modal-title><h5>{{ $t("Add_Servings_to_Shopping", {servings: recipe_servings}) }}</h5></template> <template v-slot:modal-title
<loading-spinner v-if="loading"></loading-spinner> ><h5>{{ $t("Add_Servings_to_Shopping", { servings: recipe_servings }) }}</h5></template
<div class="accordion" role="tablist" v-if="!loading"> >
<b-card no-body class="mb-1"> <loading-spinner v-if="loading"></loading-spinner>
<b-card-header header-tag="header" class="p-0" role="tab"> <div class="accordion" role="tablist" v-if="!loading">
<b-button block v-b-toggle.accordion-0 class="text-left" variant="outline-info">{{ recipe.name }}</b-button> <b-card no-body class="mb-1">
</b-card-header> <b-card-header header-tag="header" class="p-0" role="tab">
<b-collapse id="accordion-0" class="p-2" visible accordion="my-accordion" role="tabpanel"> <b-button block v-b-toggle.accordion-0 class="text-left" variant="outline-info">{{ recipe.name }}</b-button>
</b-card-header>
<b-collapse id="accordion-0" class="p-2" visible accordion="my-accordion" role="tabpanel">
<div>
<table class="table table-sm mb-0">
<ingredient-component
v-for="i in steps.flatMap((s) => s.ingredients)"
v-bind:key="i.id"
:detailed="true"
:ingredient="i"
:ingredient_factor="ingredient_factor"
@checked-state-changed="$set(i, 'checked', !i.checked)"
/>
</table>
</div>
</b-collapse>
<!-- eslint-disable vue/no-v-for-template-key-on-child -->
<template v-for="r in related_recipes">
<b-card no-body class="mb-1" :key="r.recipe.id">
<b-card-header header-tag="header" class="p-1" role="tab">
<b-button btn-sm block v-b-toggle="'accordion-' + r.recipe.id" class="text-left" variant="outline-primary">{{ r.recipe.name }}</b-button>
</b-card-header>
<b-collapse :id="'accordion-' + r.recipe.id" accordion="my-accordion" role="tabpanel">
<div v-for="i in r.steps.flatMap((s) => s.ingredients)" v-bind:key="i.id">
<table class="table table-sm mb-0">
<ingredient-component
:use_plural="true"
:key="i.id"
:detailed="true"
:ingredient="i"
:ingredient_factor="ingredient_factor"
@checked-state-changed="$set(i, 'checked', !i.checked)"
/>
</table>
</div>
</b-collapse>
</b-card>
</template>
<!-- eslint-disable vue/no-v-for-template-key-on-child -->
</b-card>
</div>
<div> <div>
<table class="table table-sm mb-0"> <b-input-group>
<b-input-group-prepend is-text class="d-inline-flex">
<i class="fa fa-calculator"></i>
</b-input-group-prepend>
<b-form-spinbutton min="1" v-model="recipe_servings" inline style="height: 3em"></b-form-spinbutton>
<!-- <CustomInputSpinButton v-model.number="recipe_servings" style="height: 3em" /> -->
<ingredient-component v-for="i in steps.flatMap(s => s.ingredients)" v-bind:key="i.id" <b-input-group-append class="d-inline-flex">
:detailed="true" <b-button variant="success" @click="saveShopping" class="d-none d-lg-inline-block">{{ $t("Save") }}</b-button>
:ingredient="i" <b-button variant="success" @click="saveShopping" class="d-inline-block d-lg-none"><i class="fa fa-cart-plus"></i></b-button>
:ingredient_factor="ingredient_factor" </b-input-group-append>
@checked-state-changed="$set(i, 'checked', !i.checked)" </b-input-group>
/> </div>
</table> </b-modal>
</div> </div>
</b-collapse>
<!-- eslint-disable vue/no-v-for-template-key-on-child -->
<template v-for="r in related_recipes">
<b-card no-body class="mb-1" :key="r.recipe.id">
<b-card-header header-tag="header" class="p-1" role="tab">
<b-button btn-sm block v-b-toggle="'accordion-' + r.recipe.id" class="text-left" variant="outline-primary">{{ r.recipe.name }}</b-button>
</b-card-header>
<b-collapse :id="'accordion-' + r.recipe.id" accordion="my-accordion" role="tabpanel">
<div v-for="i in r.steps.flatMap(s => s.ingredients)" v-bind:key="i.id">
<table class="table table-sm mb-0">
<ingredient-component
:use_plural="true"
:key="i.id"
:detailed="true"
:ingredient="i"
:ingredient_factor="ingredient_factor"
@checked-state-changed="$set(i, 'checked', !i.checked)"
/>
</table>
</div>
</b-collapse>
</b-card>
</template>
<!-- eslint-disable vue/no-v-for-template-key-on-child -->
</b-card>
</div>
<div>
<b-input-group>
<b-input-group-prepend is-text class="d-inline-flex">
<i class="fa fa-calculator"></i>
</b-input-group-prepend>
<b-form-spinbutton min="1" v-model="recipe_servings" inline style="height: 3em"></b-form-spinbutton>
<!-- <CustomInputSpinButton v-model.number="recipe_servings" style="height: 3em" /> -->
<b-input-group-append class="d-inline-flex">
<b-button variant="success" @click="saveShopping" class="d-none d-lg-inline-block">{{ $t("Save") }}</b-button>
<b-button variant="success" @click="saveShopping" class="d-inline-block d-lg-none"><i class="fa fa-cart-plus"></i></b-button>
</b-input-group-append>
</b-input-group>
</div>
</b-modal>
</div>
</template> </template>
<script> <script>
import Vue from "vue" import Vue from "vue"
import {BootstrapVue} from "bootstrap-vue" import { BootstrapVue } from "bootstrap-vue"
Vue.use(BootstrapVue) Vue.use(BootstrapVue)
const {ApiApiFactory} = require("@/utils/openapi/api") const { ApiApiFactory } = require("@/utils/openapi/api")
import {StandardToasts} from "@/utils/utils" import { StandardToasts } from "@/utils/utils"
import IngredientComponent from "@/components/IngredientComponent" import IngredientComponent from "@/components/IngredientComponent"
import LoadingSpinner from "@/components/LoadingSpinner" import LoadingSpinner from "@/components/LoadingSpinner"
import {useMealPlanStore} from "@/stores/MealPlanStore"; import { useMealPlanStore } from "@/stores/MealPlanStore"
// import CustomInputSpinButton from "@/components/CustomInputSpinButton" // import CustomInputSpinButton from "@/components/CustomInputSpinButton"
export default { export default {
name: "ShoppingModal", name: "ShoppingModal",
components: {LoadingSpinner, IngredientComponent}, components: { LoadingSpinner, IngredientComponent },
mixins: [], mixins: [],
props: { props: {
recipe: {required: true, type: Object}, recipe: { required: true, type: Object },
servings: {type: Number, default: undefined}, servings: { type: Number, default: undefined },
modal_id: {required: true, type: Number}, modal_id: { required: true, type: Number },
mealplan: {type: Object, default: undefined}, mealplan: { type: Object, default: undefined },
list_recipe: {type: Number, default: undefined}, list_recipe: { type: Number, default: undefined },
}, },
data() { data() {
return { return {
loading: true, loading: true,
steps: [], steps: [],
recipe_servings: undefined, recipe_servings: undefined,
add_shopping: [], add_shopping: [],
related_recipes: [], related_recipes: [],
} }
}, },
mounted() { mounted() {
this.recipe_servings = this.servings this.recipe_servings = this.servings
}, },
computed: { computed: {
ingredient_factor: function () { ingredient_factor: function () {
return this.recipe_servings / this.recipe.servings return this.recipe_servings / this.recipe.servings
}, },
}, },
watch: { watch: {
servings: function (newVal) { servings: function (newVal) {
this.recipe_servings = parseInt(newVal) this.recipe_servings = parseInt(newVal)
}, },
}, },
methods: { methods: {
loadRecipe: function () { loadRecipe: function () {
this.add_shopping = [] this.add_shopping = []
this.related_recipes = [] this.related_recipes = []
let apiClient = new ApiApiFactory() let apiClient = new ApiApiFactory()
apiClient apiClient
.retrieveRecipe(this.recipe.id) .retrieveRecipe(this.recipe.id)
.then((result) => { .then((result) => {
this.steps = result.data.steps this.steps = result.data.steps
// ALERT: this will all break if ingredients are re-used between recipes // ALERT: this will all break if ingredients are re-used between recipes
// ALERT: this also doesn't quite work right if the same recipe appears multiple time in the related recipes // ALERT: this also doesn't quite work right if the same recipe appears multiple time in the related recipes
if (!this.recipe_servings) { if (!this.recipe_servings) {
this.recipe_servings = result.data?.servings this.recipe_servings = result.data?.servings
} }
this.steps.forEach(s => s.ingredients.filter(i => i.food?.food_onhand === false).forEach(i => this.$set(i, 'checked', true))) this.steps.forEach((s) => s.ingredients.filter((i) => i.food?.food_onhand === false).forEach((i) => this.$set(i, "checked", true)))
this.loading = false this.loading = false
}) })
.then(() => { .then(() => {
// get a list of related recipes // get a list of related recipes
apiClient apiClient
.relatedRecipe(this.recipe.id) .relatedRecipe(this.recipe.id)
.then((result) => { .then((result) => {
return result.data return result.data
}) })
.then((related_recipes) => { .then((related_recipes) => {
let promises = [] let promises = []
related_recipes.forEach((x) => { related_recipes.forEach((x) => {
promises.push( promises.push(
apiClient.listSteps(x.id).then((recipe_steps) => { apiClient.listSteps(x.id).then((recipe_steps) => {
let sub_recipe_steps = recipe_steps.data.results.filter((x) => x.ingredients.length > 0) let sub_recipe_steps = recipe_steps.data.results.filter((x) => x.ingredients.length > 0)
sub_recipe_steps.forEach(s => s.ingredients.filter(i => i.food.food_onhand === false).forEach(i => this.$set(i, 'checked', true))) sub_recipe_steps.forEach((s) => s.ingredients.filter((i) => i.food.food_onhand === false).forEach((i) => this.$set(i, "checked", true)))
this.related_recipes.push({ this.related_recipes.push({
recipe: x, recipe: x,
steps: sub_recipe_steps, steps: sub_recipe_steps,
}) })
}) })
) )
}) })
return Promise.all(promises) return Promise.all(promises)
}) })
}) })
}, },
saveShopping: function () { saveShopping: function () {
// another choice would be to create ShoppingListRecipes for each recipe - this bundles all related recipe under the parent recipe // another choice would be to create ShoppingListRecipes for each recipe - this bundles all related recipe under the parent recipe
let shopping_recipe = { let shopping_recipe = {
id: this.recipe.id, id: this.recipe.id,
ingredients: this.steps.flatMap(s => s.ingredients.filter(i => i.checked === true).flatMap(i => i.id)).concat(this.related_recipes.flatMap(r => r.steps.flatMap(rs => rs.ingredients.filter(i => i.checked === true).flatMap(i => i.id)))), ingredients: this.steps
servings: this.recipe_servings, .flatMap((s) => s.ingredients.filter((i) => i.checked === true).flatMap((i) => i.id))
mealplan: this.mealplan, .concat(this.related_recipes.flatMap((r) => r.steps.flatMap((rs) => rs.ingredients.filter((i) => i.checked === true).flatMap((i) => i.id)))),
list_recipe: this.list_recipe, servings: this.recipe_servings,
} mealplan: this.mealplan,
let apiClient = new ApiApiFactory() list_recipe: this.list_recipe,
apiClient.shoppingRecipe(this.recipe.id, shopping_recipe) }
.then((result) => { let apiClient = new ApiApiFactory()
StandardToasts.makeStandardToast(this, StandardToasts.SUCCESS_CREATE) apiClient
this.$emit("finish") .shoppingRecipe(this.recipe.id, shopping_recipe)
if (this.mealplan !== undefined && this.mealplan !== null){ .then((result) => {
StandardToasts.makeStandardToast(this, StandardToasts.SUCCESS_CREATE)
this.$emit("finish")
if (this.mealplan !== undefined && this.mealplan !== null) {
useMealPlanStore().plans[this.mealplan.id].shopping = true useMealPlanStore().plans[this.mealplan.id].shopping = true
} }
}) })
.catch((err) => { .catch((err) => {
StandardToasts.makeStandardToast(this, StandardToasts.FAIL_CREATE, err) StandardToasts.makeStandardToast(this, StandardToasts.FAIL_CREATE, err)
}) })
this.$bvModal.hide(`shopping_${this.modal_id}`) this.$bvModal.hide(`shopping_${this.modal_id}`)
}, },
}, },
} }
</script> </script>
<style> <style>
.b-form-spinbutton.form-control { .b-form-spinbutton.form-control {
background-color: #e9ecef; background-color: #e9ecef;
border: 1px solid #ced4da; border: 1px solid #ced4da;
} }
</style> </style>