added auto shopping functionality. fixed bug when there are no matching recipes

This commit is contained in:
AquaticLava 2023-08-01 21:52:59 -06:00
parent df684f591a
commit ecd828008e
4 changed files with 43 additions and 6 deletions

View File

@ -988,6 +988,7 @@ class AutoMealPlanSerializer(serializers.Serializer):
keywords = KeywordSerializer(many=True) keywords = KeywordSerializer(many=True)
servings = CustomDecimalField() servings = CustomDecimalField()
shared = UserSerializer(many=True, required=False, allow_null=True) shared = UserSerializer(many=True, required=False, allow_null=True)
addshopping = serializers.BooleanField()
class ShoppingListRecipeSerializer(serializers.ModelSerializer): class ShoppingListRecipeSerializer(serializers.ModelSerializer):

View File

@ -688,6 +688,8 @@ class AutoPlanViewSet(viewsets.ViewSet):
for keyword in keywords: for keyword in keywords:
recipes = recipes.filter(keywords__name=keyword['name']) recipes = recipes.filter(keywords__name=keyword['name'])
if len(recipes) == 0:
return Response(serializer.data)
recipes = recipes.order_by('?')[:days] recipes = recipes.order_by('?')[:days]
recipes = list(recipes) recipes = list(recipes)
@ -707,7 +709,7 @@ class AutoPlanViewSet(viewsets.ViewSet):
for m in meal_plans: for m in meal_plans:
m.shared.set(shared_pks) m.shared.set(shared_pks)
if request.data.get('addshopping', False) and request.data.get('recipe', None): if request.data.get('addshopping', False):
SLR = RecipeShoppingEditor(user=request.user, space=request.space) SLR = RecipeShoppingEditor(user=request.user, space=request.space)
SLR.create(mealplan=m, servings=servings) SLR.create(mealplan=m, servings=servings)

View File

@ -372,7 +372,8 @@ export default {
date: Date.now(), date: Date.now(),
startDay: null, startDay: null,
endDay: null, endDay: null,
shared: [] shared: [],
addshopping: false
}, },
showDate: new Date(), showDate: new Date(),
plan_entries: [], plan_entries: [],
@ -695,7 +696,8 @@ export default {
"meal_type_id" : autoPlan.meal_types[mealTypeIndex].id, "meal_type_id" : autoPlan.meal_types[mealTypeIndex].id,
"keywords" : autoPlan.keywords[mealTypeIndex], "keywords" : autoPlan.keywords[mealTypeIndex],
"servings" : autoPlan.servings, "servings" : autoPlan.servings,
"shared" : autoPlan.shared "shared" : autoPlan.shared,
"addshopping": autoPlan.addshopping
} }
await apiClient.createAutoPlanViewSet(data) await apiClient.createAutoPlanViewSet(data)

View File

@ -47,6 +47,12 @@
></generic-multiselect> ></generic-multiselect>
<small tabindex="-1" class="form-text text-muted">{{ $t("Share") }}</small> <small tabindex="-1" class="form-text text-muted">{{ $t("Share") }}</small>
</b-form-group> </b-form-group>
<b-input-group v-if="!autoMealPlan">
<b-form-checkbox id="AddToShopping" v-model="mealplan_settings.addshopping"/>
<small tabindex="-1" class="form-text text-muted">{{
$t("AddToShopping")
}}</small>
</b-input-group>
<div class=""> <div class="">
<div class="row m-3 mb-0"> <div class="row m-3 mb-0">
@ -77,11 +83,14 @@ import {BootstrapVue} from "bootstrap-vue"
import GenericMultiselect from "@/components/GenericMultiselect" import GenericMultiselect from "@/components/GenericMultiselect"
import {ApiMixin} from "@/utils/utils" import {ApiMixin} from "@/utils/utils"
import {useUserPreferenceStore} from "@/stores/UserPreferenceStore"; import {useUserPreferenceStore} from "@/stores/UserPreferenceStore";
import VueCookies from "vue-cookies";
const { ApiApiFactory } = require("@/utils/openapi/api") const { ApiApiFactory } = require("@/utils/openapi/api")
const { StandardToasts } = require("@/utils/utils") const { StandardToasts } = require("@/utils/utils")
Vue.use(BootstrapVue) Vue.use(BootstrapVue)
Vue.use(VueCookies)
let MEALPLAN_COOKIE_NAME = "mealplan_settings"
export default { export default {
name: "AutoMealPlanModal", name: "AutoMealPlanModal",
@ -106,16 +115,38 @@ export default {
date: Date.now(), date: Date.now(),
startDay: null, startDay: null,
endDay: null, endDay: null,
shared: [] shared: [],
addshopping: false
},
mealplan_settings: {
addshopping: false,
} }
} }
}, },
mounted: function () {}, watch: {
mealplan_settings: {
handler(newVal) {
this.$cookies.set(MEALPLAN_COOKIE_NAME, this.mealplan_settings)
},
deep: true,
},
},
mounted: function () {
useUserPreferenceStore().updateIfStaleOrEmpty()
},
computed: {
autoMealPlan: function () {
return useUserPreferenceStore().getStaleData()?.mealplan_autoadd_shopping
},
},
methods: { methods: {
genericSelectChanged: function (obj) { genericSelectChanged: function (obj) {
this.AutoPlan.keywords[obj.var] = obj.val this.AutoPlan.keywords[obj.var] = obj.val
}, },
showModal() { showModal() {
if (this.$cookies.isKey(MEALPLAN_COOKIE_NAME)) {
this.mealplan_settings = Object.assign({}, this.mealplan_settings, this.$cookies.get(MEALPLAN_COOKIE_NAME))
}
this.refreshMealTypes() this.refreshMealTypes()
this.AutoPlan.servings = 1 this.AutoPlan.servings = 1
@ -164,6 +195,7 @@ export default {
}, },
createPlan() { createPlan() {
this.$bvModal.hide(`autoplan-modal`) this.$bvModal.hide(`autoplan-modal`)
this.AutoPlan.addshopping = this.mealplan_settings.addshopping
this.$emit("create-plan", this.AutoPlan) this.$emit("create-plan", this.AutoPlan)
}, },
updateStartDay(date){ updateStartDay(date){