Merge pull request #2468 from AquaticLava/Auto-Planner

Auto meal plan
This commit is contained in:
vabene1111 2023-08-16 06:18:43 +02:00 committed by GitHub
commit f07dec6062
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 422 additions and 36 deletions

View File

@ -1,35 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="FacetManager">
<facet type="django" name="Django">
<configuration>
<option name="rootFolder" value="$MODULE_DIR$" />
<option name="settingsModule" value="recipes/settings.py" />
<option name="manageScript" value="$MODULE_DIR$/manage.py" />
<option name="environment" value="&lt;map/&gt;" />
<option name="doNotUseTestRunner" value="false" />
<option name="trackFilePattern" value="migrations" />
</configuration>
</facet>
</component>
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/cookbook/tests/resources" />
<excludeFolder url="file://$MODULE_DIR$/staticfiles" />
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
<component name="TemplatesService">
<option name="TEMPLATE_CONFIGURATION" value="Django" />
<option name="TEMPLATE_FOLDERS">
<list>
<option value="$MODULE_DIR$/cookbook/templates" />
</list>
</option>
</component>
<component name="TestRunnerService">
<option name="PROJECT_TEST_RUNNER" value="pytest" />
</component>
</module>

View File

@ -1,3 +1,4 @@
import random
import traceback
import uuid
from datetime import datetime, timedelta
@ -999,6 +1000,17 @@ class MealPlanSerializer(SpacedModelSerializer, WritableNestedModelSerializer):
read_only_fields = ('created_by',)
class AutoMealPlanSerializer(serializers.Serializer):
start_date = serializers.DateField()
end_date = serializers.DateField()
meal_type_id = serializers.IntegerField()
keywords = KeywordSerializer(many=True)
servings = CustomDecimalField()
shared = UserSerializer(many=True, required=False, allow_null=True)
addshopping = serializers.BooleanField()
class ShoppingListRecipeSerializer(serializers.ModelSerializer):
name = serializers.SerializerMethodField('get_name') # should this be done at the front end?
recipe_name = serializers.ReadOnlyField(source='recipe.name')

View File

@ -36,6 +36,7 @@ router.register(r'ingredient', api.IngredientViewSet)
router.register(r'invite-link', api.InviteLinkViewSet)
router.register(r'keyword', api.KeywordViewSet)
router.register(r'meal-plan', api.MealPlanViewSet)
router.register(r'auto-plan', api.AutoPlanViewSet, basename='auto-plan')
router.register(r'meal-type', api.MealTypeViewSet)
router.register(r'recipe', api.RecipeViewSet)
router.register(r'recipe-book', api.RecipeBookViewSet)

View File

@ -1,7 +1,9 @@
import datetime
import io
import json
import mimetypes
import pathlib
import random
import re
import threading
import traceback
@ -25,6 +27,7 @@ from django.core.files import File
from django.db.models import Case, Count, Exists, OuterRef, ProtectedError, Q, Subquery, Value, When, Avg, Max
from django.db.models.fields.related import ForeignObjectRel
from django.db.models.functions import Coalesce, Lower
from django.db.models.signals import post_save
from django.http import FileResponse, HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, redirect
from django.urls import reverse
@ -94,7 +97,8 @@ from cookbook.serializer import (AutomationSerializer, BookmarkletImportListSeri
SyncLogSerializer, SyncSerializer, UnitSerializer,
UserFileSerializer, UserSerializer, UserPreferenceSerializer,
UserSpaceSerializer, ViewLogSerializer, AccessTokenSerializer, FoodSimpleSerializer,
RecipeExportSerializer, UnitConversionSerializer, PropertyTypeSerializer, PropertySerializer)
RecipeExportSerializer, UnitConversionSerializer, PropertyTypeSerializer,
PropertySerializer, AutoMealPlanSerializer)
from cookbook.views.import_export import get_integration
from recipes import settings
@ -666,6 +670,67 @@ class MealPlanViewSet(viewsets.ModelViewSet):
return queryset
class AutoPlanViewSet(viewsets.ViewSet):
def create(self, request):
serializer = AutoMealPlanSerializer(data=request.data)
if serializer.is_valid():
keywords = serializer.validated_data['keywords']
start_date = serializer.validated_data['start_date']
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()
meal_plans = list()
for keyword in keywords:
recipes = recipes.filter(keywords__name=keyword['name'])
if len(recipes) == 0:
return Response(serializer.data)
recipes = recipes.order_by('?')[:days]
recipes = list(recipes)
for i in range(0, days):
day = start_date + datetime.timedelta(i)
recipe = recipes[i % len(recipes)]
args = {'recipe': recipe, 'servings': servings, 'title': recipe.name,
'created_by': request.user,
'meal_type': meal_type,
'note': '', 'date': day, 'space': request.space}
m = MealPlan(**args)
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):
SLR = RecipeShoppingEditor(user=request.user, space=request.space)
SLR.create(mealplan=m, servings=servings)
else:
post_save.send(
sender=m.__class__,
instance=m,
created=True,
update_fields=None,
)
return Response(serializer.data)
return Response(serializer.errors, 400)
class MealTypeViewSet(viewsets.ModelViewSet):
"""
returns list of meal types created by the

View File

@ -279,12 +279,26 @@
:create_date="mealplan_default_date"
@reload-meal-types="refreshMealTypes"
></meal-plan-edit-modal>
<auto-meal-plan-modal
:modal_title="'Auto create meal plan'"
:current_period="current_period"
@create-plan="doAutoPlan"
></auto-meal-plan-modal>
<div class="row d-none d-lg-block">
<div class="col-12 float-right">
<button class="btn btn-success shadow-none" @click="createEntryClick(new Date())"><i
class="fas fa-calendar-plus"></i> {{ $t("Create") }}
</button>
<button class="btn btn-primary shadow-none" @click="createAutoPlan(new Date())"><i
class="fas fa-calendar-plus"></i> {{ $t("Auto_Planner") }}
</button>
<button class="btn btn-primary shadow-none" @click="deleteAll()"><i
class="fas fa-calendar-plus"></i> {{ "DEBUG:deleteAll" }}
</button>
<button class="btn btn-primary shadow-none" @click="refreshEntries()"><i
class="fas fa-calendar-plus"></i> {{ "DEBUG:RefreshMeals" }}
</button>
<a class="btn btn-primary shadow-none" :href="iCalUrl"><i class="fas fa-download"></i>
{{ $t("Export_To_ICal") }}
</a>
@ -297,6 +311,7 @@
<a class="dropdown-item" @click="createEntryClick(new Date())"><i
class="fas fa-calendar-plus fa-fw"></i> {{ $t("Create") }}</a>
</template>
</bottom-navigation-bar>
</div>
</template>
@ -322,6 +337,8 @@ import {CalendarView, CalendarMathMixin} from "vue-simple-calendar/src/component
import {ApiApiFactory} from "@/utils/openapi/api"
import BottomNavigationBar from "@/components/BottomNavigationBar.vue";
import {useMealPlanStore} from "@/stores/MealPlanStore";
import axios from "axios";
import AutoMealPlanModal from "@/components/AutoMealPlanModal";
const {makeToast} = require("@/utils/utils")
@ -334,6 +351,7 @@ let SETTINGS_COOKIE_NAME = "mealplan_settings"
export default {
name: "MealPlanView",
components: {
AutoMealPlanModal,
MealPlanEditModal,
MealPlanCard,
CalendarView,
@ -347,6 +365,16 @@ export default {
mixins: [CalendarMathMixin, ApiMixin, ResolveUrlMixin],
data: function () {
return {
AutoPlan: {
meal_types: [],
keywords: [[]],
servings: 1,
date: Date.now(),
startDay: null,
endDay: null,
shared: [],
addshopping: false
},
showDate: new Date(),
plan_entries: [],
recipe_viewed: {},
@ -656,6 +684,39 @@ export default {
this.$bvModal.show(`id_meal_plan_edit_modal`)
})
},
createAutoPlan() {
this.$bvModal.show(`autoplan-modal`)
},
async autoPlanThread(autoPlan, mealTypeIndex) {
let apiClient = new ApiApiFactory()
let data = {
"start_date" : moment(autoPlan.startDay).format("YYYY-MM-DD"),
"end_date" : moment(autoPlan.endDay).format("YYYY-MM-DD"),
"meal_type_id" : autoPlan.meal_types[mealTypeIndex].id,
"keywords" : autoPlan.keywords[mealTypeIndex],
"servings" : autoPlan.servings,
"shared" : autoPlan.shared,
"addshopping": autoPlan.addshopping
}
await apiClient.createAutoPlanViewSet(data)
},
async doAutoPlan(autoPlan) {
for (let i = 0; i < autoPlan.meal_types.length; i++) {
if (autoPlan.keywords[i].length === 0) continue
await this.autoPlanThread(autoPlan, i)
}
this.refreshEntries()
},
refreshEntries(){//todo Remove method
let date = this.current_period
useMealPlanStore().refreshFromAPI(moment(date.periodStart).format("YYYY-MM-DD"), moment(date.periodEnd).format("YYYY-MM-DD"))
},
deleteAll(){//todo Remove method, only used in debugging
for (let i = 0; i < useMealPlanStore().plan_list.length; i++) {
useMealPlanStore().deleteObject(useMealPlanStore().plan_list[i])
}
}
},
directives: {

View File

@ -0,0 +1,219 @@
<template>
<b-modal :id="modal_id" size="lg" :title="modal_title" hide-footer aria-label="" @show="showModal">
<h5>{{ $t("Meal_Types") }}</h5>
<div>
<div>
<b-card no-body class="mt-1 p-2"
v-for="(meal_type, k) in AutoPlan.meal_types" :key="meal_type.id">
<b-card-header class="p-2 border-0">
<div class="row">
<div class="col-10">
<h5 class="mt-1 mb-1">
{{ meal_type.icon }} {{
meal_type.name
}}
</h5>
</div>
</div>
<div class="col-12">
<generic-multiselect
@change="genericSelectChanged"
:initial_selection="AutoPlan.keywords[meal_type]"
:parent_variable="`${k}`"
:model="Models.KEYWORD"
:placeholder="$t('Keywords, leave blank to exclude meal type')"
:limit="50"
/>
</div>
</b-card-header>
</b-card>
</div>
<div class="row-cols-1 m-3">
<b-form-input class="w-25 m-2 mb-0" :value = "AutoPlan.servings" :type="'number'" @input="updateServings"></b-form-input>
<small tabindex="-1" class="m-2 mt-0 form-text text-muted">{{ $t("Servings") }}</small>
</div>
<b-form-group class="mt-3">
<generic-multiselect
required
@change="AutoPlan.shared = $event.val"
parent_variable="entryEditing.shared"
:label="'display_name'"
:model="Models.USER_NAME"
style="flex-grow: 1; flex-shrink: 1; flex-basis: 0"
v-bind:placeholder="$t('Share')"
:limit="10"
:multiple="true"
:initial_selection="AutoPlan.shared"
></generic-multiselect>
<small tabindex="-1" class="form-text text-muted">{{ $t("Share") }}</small>
</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="row m-3 mb-0">
<b-form-datepicker class="col" :value-as-date="true" :value="AutoPlan.startDay" @input="updateStartDay"></b-form-datepicker>
<div class="col"></div>
<b-form-datepicker class="col" :value-as-date="true" :value="AutoPlan.endDay" @input="updateEndDay"></b-form-datepicker>
</div>
<div class="row align-top m-3 mt-0">
<small tabindex="-1" class="col align-text-top text-muted">{{ $t("Start Day") }}</small>
<div class="col"></div>
<small tabindex="-1" class="col align-self-end text-muted">{{ $t("End Day") }}</small>
</div>
</div>
</div>
<div class="row mt-3 mb-3">
<div class="col-12">
<b-button class="float-right" variant="primary" @click="createPlan">{{ $t("Create Meal Plan") }}</b-button>
<b-button class="" variant="danger" @click="exitPlan">{{ $t("Exit") }}</b-button>
</div>
</div>
</b-modal>
</template>
<script>
import Vue from "vue"
import {BootstrapVue} from "bootstrap-vue"
import GenericMultiselect from "@/components/GenericMultiselect"
import {ApiMixin} from "@/utils/utils"
import {useUserPreferenceStore} from "@/stores/UserPreferenceStore";
import VueCookies from "vue-cookies";
const { ApiApiFactory } = require("@/utils/openapi/api")
const { StandardToasts } = require("@/utils/utils")
Vue.use(BootstrapVue)
Vue.use(VueCookies)
let MEALPLAN_COOKIE_NAME = "mealplan_settings"
export default {
name: "AutoMealPlanModal",
components: {
GenericMultiselect
},
props: {
modal_title: String,
modal_id: {
type: String,
default: "autoplan-modal",
},
current_period: Object
},
mixins: [ApiMixin],
data() {
return {
AutoPlan: {
meal_types: [],
keywords: [[]],
servings: 1,
date: Date.now(),
startDay: null,
endDay: null,
shared: [],
addshopping: false
},
mealplan_settings: {
addshopping: false,
}
}
},
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: {
genericSelectChanged: function (obj) {
this.AutoPlan.keywords[obj.var] = obj.val
},
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.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) {
element.order = index
})
let updated = 0
this.meal_types.forEach((meal_type) => {
let apiClient = new ApiApiFactory()
apiClient
.updateMealType(this.AutoPlan.meal_type, meal_type)
.then((e) => {
if (updated === this.meal_types.length - 1) {
this.periodChangedCallback(this.current_period)
} else {
updated++
}
})
.catch((err) => {
StandardToasts.makeStandardToast(this, StandardToasts.FAIL_UPDATE, err)
})
})
},
refreshMealTypes() {
let apiClient = new ApiApiFactory()
Promise.resolve(apiClient.listMealTypes().then((result) => {
result.data.forEach((meal_type) => {
meal_type.editing = false
})
this.AutoPlan.meal_types = result.data
})).then( () => {
let mealArray = this.AutoPlan.meal_types
for (let i = 0; i < mealArray.length; i++) {
this.AutoPlan.keywords[i] = [];
}}
)
},
createPlan() {
this.$bvModal.hide(`autoplan-modal`)
this.AutoPlan.addshopping = this.mealplan_settings.addshopping
this.$emit("create-plan", this.AutoPlan)
},
updateStartDay(date){
this.AutoPlan.startDay = date
},
updateEndDay(date){
this.AutoPlan.endDay = date
},
updateServings(numberOfServings) {
this.AutoPlan.servings = numberOfServings
},
exitPlan() {
this.$bvModal.hide(`autoplan-modal`)
}
},
}
</script>
<style scoped></style>

View File

@ -5356,6 +5356,39 @@ export const ApiApiAxiosParamCreator = function (configuration?: Configuration)
options: localVarRequestOptions,
};
},
/**
*
* @param {any} [body]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
createAutoPlanViewSet: async (body?: any, options: any = {}): Promise<RequestArgs> => {
const localVarPath = `/api/auto-plan/`;
// use dummy base URL string because the URL constructor only accepts absolute URLs.
const localVarUrlObj = new URL(localVarPath, DUMMY_BASE_URL);
let baseOptions;
if (configuration) {
baseOptions = configuration.baseOptions;
}
const localVarRequestOptions = { method: 'POST', ...baseOptions, ...options};
const localVarHeaderParameter = {} as any;
const localVarQueryParameter = {} as any;
localVarHeaderParameter['Content-Type'] = 'application/json';
setSearchParams(localVarUrlObj, localVarQueryParameter, options.query);
let headersFromBaseOptions = baseOptions && baseOptions.headers ? baseOptions.headers : {};
localVarRequestOptions.headers = {...localVarHeaderParameter, ...headersFromBaseOptions, ...options.headers};
localVarRequestOptions.data = serializeDataIfNeeded(body, localVarRequestOptions, configuration)
return {
url: toPathString(localVarUrlObj),
options: localVarRequestOptions,
};
},
/**
*
* @param {Automation} [automation]
@ -14686,6 +14719,16 @@ export const ApiApiFp = function(configuration?: Configuration) {
const localVarAxiosArgs = await localVarAxiosParamCreator.createAccessToken(accessToken, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
*
* @param {any} [body]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
async createAutoPlanViewSet(body?: any, options?: any): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<any>> {
const localVarAxiosArgs = await localVarAxiosParamCreator.createAutoPlanViewSet(body, options);
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
},
/**
*
* @param {Automation} [automation]
@ -17464,6 +17507,15 @@ export const ApiApiFactory = function (configuration?: Configuration, basePath?:
createAccessToken(accessToken?: AccessToken, options?: any): AxiosPromise<AccessToken> {
return localVarFp.createAccessToken(accessToken, options).then((request) => request(axios, basePath));
},
/**
*
* @param {any} [body]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
*/
createAutoPlanViewSet(body?: any, options?: any): AxiosPromise<any> {
return localVarFp.createAutoPlanViewSet(body, options).then((request) => request(axios, basePath));
},
/**
*
* @param {Automation} [automation]
@ -19981,6 +20033,17 @@ export class ApiApi extends BaseAPI {
return ApiApiFp(this.configuration).createAccessToken(accessToken, options).then((request) => request(this.axios, this.basePath));
}
/**
*
* @param {any} [body]
* @param {*} [options] Override http request option.
* @throws {RequiredError}
* @memberof ApiApi
*/
public createAutoPlanViewSet(body?: any, options?: any) {
return ApiApiFp(this.configuration).createAutoPlanViewSet(body, options).then((request) => request(this.axios, this.basePath));
}
/**
*
* @param {Automation} [automation]