diff --git a/cookbook/serializer.py b/cookbook/serializer.py index dca47845..bf89d48e 100644 --- a/cookbook/serializer.py +++ b/cookbook/serializer.py @@ -155,6 +155,7 @@ class FoodInheritFieldSerializer(UniqueFieldsMixin): class UserPreferenceSerializer(serializers.ModelSerializer): # food_inherit_default = FoodInheritFieldSerializer(source='space.food_inherit', read_only=True) food_ignore_default = serializers.SerializerMethodField('get_ignore_default') + plan_share = UserNameSerializer(many=True) def get_ignore_default(self, obj): return FoodInheritFieldSerializer(Food.inherit_fields.difference(obj.space.food_inherit.all()), many=True).data diff --git a/vue/src/apps/MealPlanView/MealPlanView.vue b/vue/src/apps/MealPlanView/MealPlanView.vue index 927ba9eb..d58b2159 100644 --- a/vue/src/apps/MealPlanView/MealPlanView.vue +++ b/vue/src/apps/MealPlanView/MealPlanView.vue @@ -210,7 +210,61 @@ - + + {{ $t('Default') }} + + + + + + + + + + + + + + + + + @@ -404,274 +458,124 @@ export default { this.settings = Object.assign({}, this.settings, this.$cookies.get(SETTINGS_COOKIE_NAME)) } }) - this.$root.$on("change", this.updateEmoji) - this.$i18n.locale = window.CUSTOM_LOCALE + } else { + this.createEntry(edit_entry) + } }, - watch: { - settings: { - handler() { - this.$cookies.set(SETTINGS_COOKIE_NAME, this.settings, "360d") - }, - deep: true, - }, + setShowDate(d) { + this.showDate = d; }, - methods: { - addToShopping(entry) { - if (entry.originalItem.entry.recipe !== null) { - this.shopping_list.push(entry.originalItem.entry) - makeToast(this.$t("Success"), this.$t("Added_To_Shopping_List"), "success") - } else { - makeToast(this.$t("Failure"), this.$t("Cannot_Add_Notes_To_Shopping"), "danger") - } - }, - saveShoppingList() { - let url = window.SHOPPING_URL - let first = true - for (let se of this.shopping_list) { - if (first) { - url += `?r=[${se.recipe.id},${se.servings}]` - first = false - } else { - url += `&r=[${se.recipe.id},${se.servings}]` - } - } - window.open(url) - }, - setStartingDay(days) { - if (this.settings.startingDayOfWeek + days < 0) { - this.settings.startingDayOfWeek = 6 - } else if (this.settings.startingDayOfWeek + days > 6) { - this.settings.startingDayOfWeek = 0 - } else { - this.settings.startingDayOfWeek = this.settings.startingDayOfWeek + days - } - }, - newMealType() { - let apiClient = new ApiApiFactory() - - apiClient - .createMealType({ name: this.$t("Meal_Type") }) - .then((e) => { - this.periodChangedCallback(this.current_period) - }) - .catch((error) => { - StandardToasts.makeStandardToast(StandardToasts.FAIL_UPDATE) - }) - - this.refreshMealTypes() - }, - 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(meal_type.id, meal_type) - .then((e) => { - if (updated === this.meal_types.length - 1) { - this.periodChangedCallback(this.current_period) - } else { - updated++ - } - }) - .catch((error) => { - StandardToasts.makeStandardToast(StandardToasts.FAIL_UPDATE) - }) - }) - }, - editOrSaveMealType(index) { - let meal_type = this.meal_types[index] - if (meal_type.editing) { - this.$set(this.meal_types[index], "editing", false) - let apiClient = new ApiApiFactory() - - apiClient - .updateMealType(this.meal_types[index].id, this.meal_types[index]) - .then((e) => { - this.periodChangedCallback(this.current_period) - StandardToasts.makeStandardToast(StandardToasts.SUCCESS_UPDATE) - }) - .catch((error) => { - StandardToasts.makeStandardToast(StandardToasts.FAIL_UPDATE) - }) - } else { - this.$set(this.meal_types[index], "editing", true) - } - }, - deleteMealType(index) { - let apiClient = new ApiApiFactory() - - apiClient - .destroyMealType(this.meal_types[index].id) - .then((e) => { - this.periodChangedCallback(this.current_period) - StandardToasts.makeStandardToast(StandardToasts.SUCCESS_DELETE) - }) - .catch((error) => { - StandardToasts.makeStandardToast(StandardToasts.FAIL_DELETE) - }) - }, - updateEmoji: function (field, value) { - this.meal_types.forEach((meal_type) => { - if (meal_type.editing) { - meal_type.icon = value - } - }) - }, - editEntry(edit_entry) { - if (edit_entry.id !== -1) { - this.plan_entries.forEach((entry, index) => { - if (entry.id === edit_entry.id) { - this.$set(this.plan_entries, index, edit_entry) - this.saveEntry(this.plan_entries[index]) - } - }) - } else { - this.createEntry(edit_entry) - } - }, - setShowDate(d) { - this.showDate = d - }, - createEntryClick(data) { - this.entryEditing = this.options.entryEditing - this.entryEditing.date = moment(data).format("YYYY-MM-DD") - this.$bvModal.show(`edit-modal`) - }, - findEntry(id) { - return this.plan_entries.filter((entry) => { - return entry.id === id - })[0] - }, - moveEntry(null_object, target_date) { - this.plan_entries.forEach((entry) => { - if (entry.id === this.dragged_item.id) { - entry.date = target_date - this.saveEntry(entry) - } - }) - }, - moveEntryLeft(data) { - this.plan_entries.forEach((entry) => { - if (entry.id === data.id) { - entry.date = moment(entry.date).subtract(1, "d") - this.saveEntry(entry) - } - }) - }, - moveEntryRight(data) { - this.plan_entries.forEach((entry) => { - if (entry.id === data.id) { - entry.date = moment(entry.date).add(1, "d") - this.saveEntry(entry) - } - }) - }, - deleteEntry(data) { - this.plan_entries.forEach((entry, index, list) => { - if (entry.id === data.id) { - let apiClient = new ApiApiFactory() - - apiClient - .destroyMealPlan(entry.id) - .then((e) => { - list.splice(index, 1) - }) - .catch((error) => { - StandardToasts.makeStandardToast(StandardToasts.FAIL_UPDATE) - }) - } - }) - }, - entryClick(data) { - let entry = this.findEntry(data.id) - this.openEntryEdit(entry) - }, - openContextMenu($event, value) { - this.$refs.menu.open($event, value) - }, - openEntryEdit(entry) { - this.$bvModal.show(`edit-modal`) - this.entryEditing = entry - this.entryEditing.date = moment(entry.date).format("YYYY-MM-DD") - if (this.entryEditing.recipe != null) { - this.entryEditing.title_placeholder = this.entryEditing.recipe.name - } - }, - periodChangedCallback(date) { - this.current_period = date - let apiClient = new ApiApiFactory() - - apiClient - .listMealPlans({ - query: { - from_date: moment(date.periodStart).format("YYYY-MM-DD"), - to_date: moment(date.periodEnd).format("YYYY-MM-DD"), - }, - }) - .then((result) => { - this.plan_entries = result.data - }) - this.refreshMealTypes() - }, - refreshMealTypes() { - let apiClient = new ApiApiFactory() - - apiClient.listMealTypes().then((result) => { - result.data.forEach((meal_type) => { - meal_type.editing = false - }) - this.meal_types = result.data - }) - }, - saveEntry(entry) { - entry.date = moment(entry.date).format("YYYY-MM-DD") - - let apiClient = new ApiApiFactory() - - apiClient.updateMealPlan(entry.id, entry).catch((error) => { - StandardToasts.makeStandardToast(StandardToasts.FAIL_UPDATE) - }) - }, - createEntry(entry) { - entry.date = moment(entry.date).format("YYYY-MM-DD") - - let apiClient = new ApiApiFactory() - - apiClient - .createMealPlan(entry) - .catch((error) => { - StandardToasts.makeStandardToast(StandardToasts.FAIL_UPDATE) - }) - .then((entry_result) => { - this.plan_entries.push(entry_result.data) - }) - }, - buildItem(plan_entry) { - //dirty hack to order items within a day - let date = moment(plan_entry.date).add(plan_entry.meal_type.order, "m") - return { - id: plan_entry.id, - startDate: date, - endDate: date, - entry: plan_entry, - } - }, + createEntryClick(data) { + this.entryEditing = this.options.entryEditing + this.entryEditing.date = moment(data).format('YYYY-MM-DD') + this.$bvModal.show(`edit-modal`) }, - directives: { - hover: { - inserted: function (el) { - el.addEventListener("mouseenter", () => { - el.classList.add("shadow") - }) - el.addEventListener("mouseleave", () => { - el.classList.remove("shadow") - }) - }, - }, + findEntry(id) { + return this.plan_entries.filter(entry => { + return entry.id === id + })[0] + }, + moveEntry(null_object, target_date, drag_event) { + this.plan_entries.forEach((entry) => { + if (entry.id === this.dragged_item.id) { + if (drag_event.ctrlKey) { + let new_entry = Object.assign({}, entry) + new_entry.date = target_date + this.createEntry(new_entry) + } else { + entry.date = target_date + this.saveEntry(entry) + } + } + }) + }, + moveEntryLeft(data) { + this.plan_entries.forEach((entry) => { + if (entry.id === data.id) { + entry.date = moment(entry.date).subtract(1, 'd') + this.saveEntry(entry) + } + }) + }, + moveEntryRight(data) { + this.plan_entries.forEach((entry) => { + if (entry.id === data.id) { + entry.date = moment(entry.date).add(1, 'd') + this.saveEntry(entry) + } + }) + }, + deleteEntry(data) { + this.plan_entries.forEach((entry, index, list) => { + if (entry.id === data.id) { + let apiClient = new ApiApiFactory() + + apiClient.destroyMealPlan(entry.id).then(e => { + list.splice(index, 1) + }).catch(error => { + StandardToasts.makeStandardToast(StandardToasts.FAIL_UPDATE) + }) + } + }) + }, + entryClick(data) { + let entry = this.findEntry(data.id) + this.openEntryEdit(entry) + }, + openContextMenu($event, value) { + this.$refs.menu.open($event, value) + }, + openEntryEdit(entry) { + this.$bvModal.show(`edit-modal`) + this.entryEditing = entry + this.entryEditing.date = moment(entry.date).format('YYYY-MM-DD') + if (this.entryEditing.recipe != null) { + this.entryEditing.title_placeholder = this.entryEditing.recipe.name + } + }, + periodChangedCallback(date) { + this.current_period = date + let apiClient = new ApiApiFactory() + + apiClient.listMealPlans({ + query: { + from_date: moment(date.periodStart).format('YYYY-MM-DD'), + to_date: moment(date.periodEnd).format('YYYY-MM-DD') + } + }).then(result => { + this.plan_entries = result.data + }) + this.refreshMealTypes() + }, + refreshMealTypes() { + let apiClient = new ApiApiFactory() + + apiClient.listMealTypes().then(result => { + result.data.forEach((meal_type) => { + meal_type.editing = false + }) + this.meal_types = result.data + }) + }, + saveEntry(entry) { + entry.date = moment(entry.date).format("YYYY-MM-DD") + + let apiClient = new ApiApiFactory() + + apiClient.updateMealPlan(entry.id, entry).catch(error => { + StandardToasts.makeStandardToast(StandardToasts.FAIL_UPDATE) + }) + }, + createEntry(entry) { + entry.date = moment(entry.date).format("YYYY-MM-DD") + + let apiClient = new ApiApiFactory() + + apiClient.createMealPlan(entry).catch(error => { + StandardToasts.makeStandardToast(StandardToasts.FAIL_UPDATE) + }).then((entry_result) => { + this.plan_entries.push(entry_result.data) + }) }, } diff --git a/vue/src/components/MealPlanEditModal.vue b/vue/src/components/MealPlanEditModal.vue index 8103e1c6..7fc8169b 100644 --- a/vue/src/components/MealPlanEditModal.vue +++ b/vue/src/components/MealPlanEditModal.vue @@ -1,21 +1,96 @@