basic shopping list load and save
This commit is contained in:
parent
0ff65d35dc
commit
df79c8f889
5
.idea/codeStyles/codeStyleConfig.xml
Normal file
5
.idea/codeStyles/codeStyleConfig.xml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
|
<state>
|
||||||
|
<option name="PREFERRED_PROJECT_CODE_STYLE" value="Default" />
|
||||||
|
</state>
|
||||||
|
</component>
|
@ -139,6 +139,27 @@ class CookLogAdmin(admin.ModelAdmin):
|
|||||||
admin.site.register(CookLog, CookLogAdmin)
|
admin.site.register(CookLog, CookLogAdmin)
|
||||||
|
|
||||||
|
|
||||||
|
class ShoppingListRecipeAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ('id', 'recipe', 'multiplier')
|
||||||
|
|
||||||
|
|
||||||
|
admin.site.register(ShoppingListRecipe, ShoppingListRecipeAdmin)
|
||||||
|
|
||||||
|
|
||||||
|
class ShoppingListEntryAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ('id', 'food', 'unit', 'list_recipe', 'checked')
|
||||||
|
|
||||||
|
|
||||||
|
admin.site.register(ShoppingListEntry, ShoppingListEntryAdmin)
|
||||||
|
|
||||||
|
|
||||||
|
class ShoppingListAdmin(admin.ModelAdmin):
|
||||||
|
list_display = ('id', 'created_by', 'created_at')
|
||||||
|
|
||||||
|
|
||||||
|
admin.site.register(ShoppingList, ShoppingListAdmin)
|
||||||
|
|
||||||
|
|
||||||
class ShareLinkAdmin(admin.ModelAdmin):
|
class ShareLinkAdmin(admin.ModelAdmin):
|
||||||
list_display = ('recipe', 'created_by', 'uuid', 'created_at',)
|
list_display = ('recipe', 'created_by', 'uuid', 'created_at',)
|
||||||
|
|
||||||
|
@ -195,13 +195,6 @@ class MealPlanSerializer(serializers.ModelSerializer):
|
|||||||
|
|
||||||
|
|
||||||
class ShoppingListRecipeSerializer(serializers.ModelSerializer):
|
class ShoppingListRecipeSerializer(serializers.ModelSerializer):
|
||||||
recipe = RecipeSerializer(read_only=True)
|
|
||||||
|
|
||||||
def create(self, validated_data):
|
|
||||||
return ShoppingListRecipe.objects.create(**validated_data)
|
|
||||||
|
|
||||||
def update(self, instance, validated_data):
|
|
||||||
return super(ShoppingListRecipeSerializer, self).update(instance, validated_data)
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ShoppingListRecipe
|
model = ShoppingListRecipe
|
||||||
@ -209,26 +202,24 @@ class ShoppingListRecipeSerializer(serializers.ModelSerializer):
|
|||||||
read_only_fields = ('id',)
|
read_only_fields = ('id',)
|
||||||
|
|
||||||
|
|
||||||
class ShoppingListEntrySerializer(serializers.ModelSerializer):
|
class ShoppingListEntrySerializer(WritableNestedModelSerializer):
|
||||||
|
food = FoodSerializer(allow_null=True)
|
||||||
def create(self, validated_data):
|
unit = UnitSerializer(allow_null=True)
|
||||||
return ShoppingListEntry.objects.create(**validated_data)
|
|
||||||
|
|
||||||
def update(self, instance, validated_data):
|
|
||||||
return super(ShoppingListEntrySerializer, self).update(instance, validated_data)
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ShoppingListEntry
|
model = ShoppingListEntry
|
||||||
fields = ('list_recipe', 'food', 'unit', 'amount', 'order', 'checked')
|
fields = ('id', 'list_recipe', 'food', 'unit', 'amount', 'order', 'checked')
|
||||||
|
read_only_fields = ('id',)
|
||||||
|
|
||||||
|
|
||||||
class ShoppingListSerializer(WritableNestedModelSerializer):
|
class ShoppingListSerializer(WritableNestedModelSerializer):
|
||||||
recipes = ShoppingListRecipeSerializer(many=True, allow_null=True, read_only=True)
|
recipes = ShoppingListRecipeSerializer(many=True, allow_null=True)
|
||||||
entries = ShoppingListEntrySerializer(many=True, allow_null=True)
|
entries = ShoppingListEntrySerializer(many=True, allow_null=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = ShoppingList
|
model = ShoppingList
|
||||||
fields = ('id', 'uuid', 'note', 'recipes', 'entries', 'shared', 'created_by', 'created_at',)
|
fields = ('id', 'uuid', 'note', 'recipes', 'entries', 'shared', 'created_by', 'created_at',)
|
||||||
|
read_only_fields = ('id',)
|
||||||
|
|
||||||
|
|
||||||
class ShareLinkSerializer(serializers.ModelSerializer):
|
class ShareLinkSerializer(serializers.ModelSerializer):
|
||||||
|
@ -136,6 +136,7 @@
|
|||||||
delimiters: ['[[', ']]'],
|
delimiters: ['[[', ']]'],
|
||||||
el: '#id_base_container',
|
el: '#id_base_container',
|
||||||
data: {
|
data: {
|
||||||
|
shopping_list_id: {{ shopping_list_id }},
|
||||||
edit_mode: true,
|
edit_mode: true,
|
||||||
recipe_query: '',
|
recipe_query: '',
|
||||||
recipes: [],
|
recipes: [],
|
||||||
@ -174,6 +175,10 @@
|
|||||||
*/
|
*/
|
||||||
mounted: function () {
|
mounted: function () {
|
||||||
|
|
||||||
|
|
||||||
|
if (this.shopping_list_id) {
|
||||||
|
this.loadShoppingList()
|
||||||
|
} else {
|
||||||
//TODO default share users
|
//TODO default share users
|
||||||
this.shopping_list = {
|
this.shopping_list = {
|
||||||
"recipes": [],
|
"recipes": [],
|
||||||
@ -183,8 +188,9 @@
|
|||||||
"created_by": 1
|
"created_by": 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
this.updateDisplay()
|
this.updateDisplay()
|
||||||
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
/*
|
/*
|
||||||
@ -204,20 +210,39 @@
|
|||||||
solid: true
|
solid: true
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
loadShoppingList: function () {
|
||||||
|
|
||||||
|
this.$http.get("{% url 'api:shoppinglist-detail' shopping_list_id %}").then((response) => {
|
||||||
|
this.shopping_list = response.body
|
||||||
|
this.updateDisplay()
|
||||||
|
}).catch((err) => {
|
||||||
|
console.log(err)
|
||||||
|
this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error loading a resource!' %}' + err.bodyText, 'danger')
|
||||||
|
})
|
||||||
|
},
|
||||||
updateShoppingList: function () {
|
updateShoppingList: function () {
|
||||||
for (let r of this.shopping_list.recipes.filter(item => item.created === true)) {
|
let recipe_promises = []
|
||||||
let old_id = r.id
|
|
||||||
console.log('updating recipe', r)
|
for (let i in this.shopping_list.recipes) {
|
||||||
this.$http.post("{% url 'api:shoppinglistrecipe-list' %}", r, {}).then((response) => {
|
if (this.shopping_list.recipes[i].created) {
|
||||||
r = response
|
console.log('updating recipe', this.shopping_list.recipes[i])
|
||||||
|
recipe_promises.push(this.$http.post("{% url 'api:shoppinglistrecipe-list' %}", this.shopping_list.recipes[i], {}).then((response) => {
|
||||||
|
let old_id = this.shopping_list.recipes[i].id
|
||||||
|
console.log("list recipe create respose ", response.body)
|
||||||
|
this.shopping_list.recipes[i] = response.body
|
||||||
for (let e of this.shopping_list.entries.filter(item => item.list_recipe === old_id)) {
|
for (let e of this.shopping_list.entries.filter(item => item.list_recipe === old_id)) {
|
||||||
e.list_recipe = r.id
|
console.log("found recipe updating ID")
|
||||||
|
e.list_recipe = this.shopping_list.recipes[i].id
|
||||||
}
|
}
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
console.log(err)
|
console.log(err)
|
||||||
this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error updating a resource!' %}' + err.bodyText, 'danger')
|
this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error updating a resource!' %}' + err.bodyText, 'danger')
|
||||||
})
|
}))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Promise.allSettled(recipe_promises).then(() => {
|
||||||
|
console.log("proceeding to update shopping list", this.shopping_list)
|
||||||
|
|
||||||
this.$http.put("{% url 'api:shoppinglist-detail' shopping_list_id %}", this.shopping_list, {}).then((response) => {
|
this.$http.put("{% url 'api:shoppinglist-detail' shopping_list_id %}", this.shopping_list, {}).then((response) => {
|
||||||
console.log(response)
|
console.log(response)
|
||||||
@ -227,6 +252,7 @@
|
|||||||
console.log(err)
|
console.log(err)
|
||||||
this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error updating a resource!' %}' + err.bodyText, 'danger')
|
this.makeToast('{% trans 'Error' %}', '{% trans 'There was an error updating a resource!' %}' + err.bodyText, 'danger')
|
||||||
})
|
})
|
||||||
|
})
|
||||||
},
|
},
|
||||||
addEntry: function () {
|
addEntry: function () {
|
||||||
this.shopping_list.entries.push({
|
this.shopping_list.entries.push({
|
||||||
@ -265,7 +291,7 @@
|
|||||||
let slr = {
|
let slr = {
|
||||||
"created": true,
|
"created": true,
|
||||||
"id": Math.random() * 1000,
|
"id": Math.random() * 1000,
|
||||||
"recipe": recipe,
|
"recipe": recipe.id,
|
||||||
"multiplier": 1
|
"multiplier": 1
|
||||||
}
|
}
|
||||||
this.shopping_list.recipes.push(slr)
|
this.shopping_list.recipes.push(slr)
|
||||||
|
Loading…
Reference in New Issue
Block a user