food edit modal - reset children inheritance
This commit is contained in:
parent
a0508684d9
commit
dcad389010
@ -515,31 +515,37 @@ class Food(ExportModelOperationsMixin('food'), TreeModel, PermissionModelMixin):
|
|||||||
return super().delete()
|
return super().delete()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def reset_inheritance(space=None):
|
def reset_inheritance(space=None, food=None):
|
||||||
# resets inherited fields to the space defaults and updates all inherited fields to root object values
|
# resets inherited fields to the space defaults and updates all inherited fields to root object values
|
||||||
inherit = space.food_inherit.all()
|
if food:
|
||||||
|
inherit = list(food.inherit_fields.all().values('id', 'field'))
|
||||||
|
filter = Q(id=food.id, space=space)
|
||||||
|
tree_filter = Q(path__startswith=food.path, space=space)
|
||||||
|
else:
|
||||||
|
inherit = list(space.food_inherit.all().values('id', 'field'))
|
||||||
|
filter = tree_filter = Q(space=space)
|
||||||
|
|
||||||
# remove all inherited fields from food
|
# remove all inherited fields from food
|
||||||
Through = Food.objects.filter(space=space).first().inherit_fields.through
|
Through = Food.objects.filter(tree_filter).first().inherit_fields.through
|
||||||
Through.objects.all().delete()
|
Through.objects.all().delete()
|
||||||
# food is going to inherit attributes
|
# food is going to inherit attributes
|
||||||
if space.food_inherit.all().count() > 0:
|
if len(inherit) > 0:
|
||||||
# ManyToMany cannot be updated through an UPDATE operation
|
# ManyToMany cannot be updated through an UPDATE operation
|
||||||
for i in inherit:
|
for i in inherit:
|
||||||
Through.objects.bulk_create([
|
Through.objects.bulk_create([
|
||||||
Through(food_id=x, foodinheritfield_id=i.id)
|
Through(food_id=x, foodinheritfield_id=i['id'])
|
||||||
for x in Food.objects.filter(space=space).values_list('id', flat=True)
|
for x in Food.objects.filter(tree_filter).values_list('id', flat=True)
|
||||||
])
|
])
|
||||||
|
|
||||||
inherit = inherit.values_list('field', flat=True)
|
inherit = [x['field'] for x in inherit]
|
||||||
if 'ignore_shopping' in inherit:
|
if 'ignore_shopping' in inherit:
|
||||||
# get food at root that have children that need updated
|
# get food at root that have children that need updated
|
||||||
Food.include_descendants(queryset=Food.objects.filter(depth=1, numchild__gt=0, space=space, ignore_shopping=True)).update(ignore_shopping=True)
|
Food.include_descendants(queryset=Food.objects.filter(Q(depth=1, numchild__gt=0, ignore_shopping=True) & filter)).update(ignore_shopping=True)
|
||||||
Food.include_descendants(queryset=Food.objects.filter(depth=1, numchild__gt=0, space=space, ignore_shopping=False)).update(ignore_shopping=False)
|
Food.include_descendants(queryset=Food.objects.filter(Q(depth=1, numchild__gt=0, ignore_shopping=False) & filter)).update(ignore_shopping=False)
|
||||||
if 'supermarket_category' in inherit:
|
if 'supermarket_category' in inherit:
|
||||||
# when supermarket_category is null or blank assuming it is not set and not intended to be blank for all descedants
|
# when supermarket_category is null or blank assuming it is not set and not intended to be blank for all descedants
|
||||||
# find top node that has category set
|
# find top node that has category set
|
||||||
category_roots = Food.exclude_descendants(queryset=Food.objects.filter(supermarket_category__isnull=False, numchild__gt=0, space=space))
|
category_roots = Food.exclude_descendants(queryset=Food.objects.filter(Q(supermarket_category__isnull=False, numchild__gt=0) & filter))
|
||||||
for root in category_roots:
|
for root in category_roots:
|
||||||
root.get_descendants().update(supermarket_category=root.supermarket_category)
|
root.get_descendants().update(supermarket_category=root.supermarket_category)
|
||||||
|
|
||||||
|
@ -419,13 +419,19 @@ class FoodSerializer(UniqueFieldsMixin, WritableNestedModelSerializer, ExtendedR
|
|||||||
validated_data['name'] = name.strip()
|
validated_data['name'] = name.strip()
|
||||||
# assuming if on hand for user also onhand for shopping_share users
|
# assuming if on hand for user also onhand for shopping_share users
|
||||||
onhand = validated_data.get('food_onhand', None)
|
onhand = validated_data.get('food_onhand', None)
|
||||||
|
reset_inherit = self.initial_data.get('reset_inherit', False)
|
||||||
if not onhand is None:
|
if not onhand is None:
|
||||||
shared_users = [user := self.context['request'].user] + list(user.userpreference.shopping_share.all())
|
shared_users = [user := self.context['request'].user] + list(user.userpreference.shopping_share.all())
|
||||||
if onhand:
|
if onhand:
|
||||||
validated_data['onhand_users'] = list(self.instance.onhand_users.all()) + shared_users
|
validated_data['onhand_users'] = list(self.instance.onhand_users.all()) + shared_users
|
||||||
else:
|
else:
|
||||||
validated_data['onhand_users'] = list(set(self.instance.onhand_users.all()) - set(shared_users))
|
validated_data['onhand_users'] = list(set(self.instance.onhand_users.all()) - set(shared_users))
|
||||||
return super(FoodSerializer, self).update(instance, validated_data)
|
|
||||||
|
# update before resetting inheritance
|
||||||
|
saved_instance = super(FoodSerializer, self).update(instance, validated_data)
|
||||||
|
if reset_inherit and (r := self.context.get('request', None)):
|
||||||
|
Food.reset_inheritance(food=saved_instance, space=r.space)
|
||||||
|
return saved_instance
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Food
|
model = Food
|
||||||
|
@ -1029,6 +1029,7 @@ export default {
|
|||||||
if (!this.searchFiltered()) {
|
if (!this.searchFiltered()) {
|
||||||
params.options.query.last_viewed = this.ui.recently_viewed
|
params.options.query.last_viewed = this.ui.recently_viewed
|
||||||
}
|
}
|
||||||
|
console.log(params)
|
||||||
return params
|
return params
|
||||||
},
|
},
|
||||||
searchFiltered: function (ignore_string = false) {
|
searchFiltered: function (ignore_string = false) {
|
||||||
|
@ -271,29 +271,27 @@ export default {
|
|||||||
if (type_match && field?.condition) {
|
if (type_match && field?.condition) {
|
||||||
const value = this.item1[field?.condition?.field]
|
const value = this.item1[field?.condition?.field]
|
||||||
const preference = getUserPreference(field?.condition?.field)
|
const preference = getUserPreference(field?.condition?.field)
|
||||||
console.log("condition", field?.condition?.condition)
|
checks = false
|
||||||
switch (field?.condition?.condition) {
|
switch (field?.condition?.condition) {
|
||||||
case "field_exists":
|
case "field_exists":
|
||||||
if ((value != undefined) === field.condition.value) {
|
if ((value != undefined) === field.condition.value) {
|
||||||
checks = true
|
checks = true
|
||||||
} else {
|
|
||||||
checks = false
|
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case "preference__array_exists":
|
case "preference__array_exists":
|
||||||
if (preference?.length > 0 === field.condition.value) {
|
if (preference?.length > 0 === field.condition.value) {
|
||||||
checks = true
|
checks = true
|
||||||
} else {
|
|
||||||
checks = false
|
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
case "preference_equals":
|
case "preference_equals":
|
||||||
if (preference === field.condition.value) {
|
if (preference === field.condition.value) {
|
||||||
checks = true
|
checks = true
|
||||||
} else {
|
|
||||||
checks = false
|
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
|
case "gt":
|
||||||
|
if (value > field.condition.value) {
|
||||||
|
checks = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return type_match && checks
|
return type_match && checks
|
||||||
|
@ -326,5 +326,6 @@
|
|||||||
"make_now": "Make Now",
|
"make_now": "Make Now",
|
||||||
"recipe_filter": "Recipe Filter",
|
"recipe_filter": "Recipe Filter",
|
||||||
"book_filter_help": "Include recipes from recipe filter instead of assigning each recipe",
|
"book_filter_help": "Include recipes from recipe filter instead of assigning each recipe",
|
||||||
"filter": "Filter"
|
"reset_children": "Reset Child Inheritance",
|
||||||
|
"reset_children_help": "Overwrite all children with values from inherited fields."
|
||||||
}
|
}
|
||||||
|
@ -76,7 +76,7 @@ export class Models {
|
|||||||
// REQUIRED: unordered array of fields that can be set during create
|
// REQUIRED: unordered array of fields that can be set during create
|
||||||
create: {
|
create: {
|
||||||
// if not defined partialUpdate will use the same parameters, prepending 'id'
|
// if not defined partialUpdate will use the same parameters, prepending 'id'
|
||||||
params: [["name", "description", "recipe", "food_onhand", "supermarket_category", "inherit", "inherit_fields", "ignore_shopping"]],
|
params: [["name", "description", "recipe", "food_onhand", "supermarket_category", "inherit", "inherit_fields", "ignore_shopping", "reset_inherit"]],
|
||||||
|
|
||||||
form: {
|
form: {
|
||||||
show_help: true,
|
show_help: true,
|
||||||
@ -135,6 +135,14 @@ export class Models {
|
|||||||
label: i18n.t("InheritFields"),
|
label: i18n.t("InheritFields"),
|
||||||
condition: { field: "food_children_exist", value: true, condition: "preference_equals" },
|
condition: { field: "food_children_exist", value: true, condition: "preference_equals" },
|
||||||
},
|
},
|
||||||
|
reset_inherit: {
|
||||||
|
form_field: true,
|
||||||
|
type: "checkbox",
|
||||||
|
field: "reset_inherit",
|
||||||
|
label: i18n.t("reset_children"),
|
||||||
|
help_text: i18n.t("reset_children_help"),
|
||||||
|
condition: { field: "numchild", value: 0, condition: "gt" },
|
||||||
|
},
|
||||||
form_function: "FoodCreateDefault",
|
form_function: "FoodCreateDefault",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
Loading…
Reference in New Issue
Block a user