From df684f591a5bafc5c1cc0551a31b515747f2292f Mon Sep 17 00:00:00 2001 From: AquaticLava Date: Tue, 1 Aug 2023 17:02:05 -0600 Subject: [PATCH] added share functionality. changed random recipe selection to prevent repeating duplicate choices. --- cookbook/views/api.py | 10 +++++++++- vue/src/apps/MealPlanView/MealPlanView.vue | 3 ++- vue/src/components/AutoMealPlanModal.vue | 22 +++++++++++++++++++++- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/cookbook/views/api.py b/cookbook/views/api.py index 749101741..7ebe76c53 100644 --- a/cookbook/views/api.py +++ b/cookbook/views/api.py @@ -675,6 +675,11 @@ class AutoPlanViewSet(viewsets.ViewSet): end_date = serializer.validated_data['end_date'] meal_type = MealType.objects.get(pk=serializer.validated_data['meal_type_id']) servings = serializer.validated_data['servings'] + shared = serializer.get_initial().get('shared', None) + shared_pks = list() + if shared is not None: + for i in range(len(shared)): + shared_pks.append(shared[i]['id']) days = (end_date - start_date).days + 1 recipes = Recipe.objects.all() @@ -688,7 +693,7 @@ class AutoPlanViewSet(viewsets.ViewSet): for i in range(0, days): day = start_date + datetime.timedelta(i) - recipe = random.choice(recipes) + recipe = recipes[i % len(recipes)] args = {'recipe': recipe, 'servings': servings, 'title': recipe.name, 'created_by': request.user, 'meal_type': meal_type, @@ -698,7 +703,10 @@ class AutoPlanViewSet(viewsets.ViewSet): meal_plans.append(m) MealPlan.objects.bulk_create(meal_plans) + for m in meal_plans: + m.shared.set(shared_pks) + if request.data.get('addshopping', False) and request.data.get('recipe', None): SLR = RecipeShoppingEditor(user=request.user, space=request.space) SLR.create(mealplan=m, servings=servings) diff --git a/vue/src/apps/MealPlanView/MealPlanView.vue b/vue/src/apps/MealPlanView/MealPlanView.vue index 9e34cb718..4c5d35791 100644 --- a/vue/src/apps/MealPlanView/MealPlanView.vue +++ b/vue/src/apps/MealPlanView/MealPlanView.vue @@ -371,7 +371,8 @@ export default { servings: 1, date: Date.now(), startDay: null, - endDay: null + endDay: null, + shared: [] }, showDate: new Date(), plan_entries: [], diff --git a/vue/src/components/AutoMealPlanModal.vue b/vue/src/components/AutoMealPlanModal.vue index 2d8084b33..c559f32dc 100644 --- a/vue/src/components/AutoMealPlanModal.vue +++ b/vue/src/components/AutoMealPlanModal.vue @@ -32,6 +32,21 @@ {{ $t("Servings") }} + + + {{ $t("Share") }} +
@@ -61,6 +76,7 @@ import Vue from "vue" import {BootstrapVue} from "bootstrap-vue" import GenericMultiselect from "@/components/GenericMultiselect" import {ApiMixin} from "@/utils/utils" +import {useUserPreferenceStore} from "@/stores/UserPreferenceStore"; const { ApiApiFactory } = require("@/utils/openapi/api") const { StandardToasts } = require("@/utils/utils") @@ -89,7 +105,8 @@ export default { servings: 1, date: Date.now(), startDay: null, - endDay: null + endDay: null, + shared: [] } } }, @@ -104,6 +121,9 @@ export default { this.AutoPlan.servings = 1 this.AutoPlan.startDay = new Date() this.AutoPlan.endDay = this.current_period.periodEnd + useUserPreferenceStore().getData().then(userPreference => { + this.AutoPlan.shared = userPreference.plan_share + }) }, sortMealTypes() { this.meal_types.forEach(function (element, index) {