- add, remove = [], []
- for f in values.keys():
- if is_boolean_group(f):
- target = add if values.pop(f) else remove
- target.append(get_boolean_group(f))
- elif is_boolean_groups(f):
- if not values.pop(f):
- remove.extend(get_boolean_groups(f))
- elif is_selection_groups(f):
- remove.extend(get_selection_groups(f))
- selected = values.pop(f)
- if selected:
- add.append(selected)
- # update values *only* if groups are being modified, otherwise
- # we introduce spurious changes that might break the super.write() call.
- if add or remove:
- # remove groups in 'remove' and add groups in 'add'
- values['groups_id'] = [(3, id) for id in remove] + [(4, id) for id in add]
+ if 'groups_id' not in values and (add or rem):
+ # remove group ids in `rem` and add group ids in `add`
+ values1['groups_id'] = zip(repeat(3), rem) + zip(repeat(4), add)
+
+ return values1