Skip to content

Commit

Permalink
Merge #2602
Browse files Browse the repository at this point in the history
2602: Fix creation of deep structures using import in update mode (backport #2601) r=mergify[bot] a=mergify[bot]

This is an automatic backport of pull request #2601 done by [Mergify](https://mergify.com).


---


<details>
<summary>Mergify commands and options</summary>

<br />

More conditions and actions can be found in the [documentation](https://docs.mergify.com/).

You can also trigger Mergify actions by commenting on this pull request:

- ``@Mergifyio` refresh` will re-evaluate the rules
- ``@Mergifyio` rebase` will rebase this PR on its base branch
- ``@Mergifyio` update` will merge the base branch into this PR
- ``@Mergifyio` backport <destination>` will backport this PR on `<destination>` branch

Additionally, on Mergify [dashboard](https://dashboard.mergify.com/) you can:

- look at your merge queues
- generate the Mergify configuration with the config editor.

Finally, you can contact us on https://mergify.com
</details>

Co-authored-by: Alexander Graf <ghostwheel42@users.noreply.github.com>
  • Loading branch information
bors[bot] and ghostwheel42 committed Dec 27, 2022
2 parents b7e4ff5 + 3b9e0b3 commit 814a3e6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
42 changes: 24 additions & 18 deletions core/admin/mailu/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -909,16 +909,23 @@ def _patch_item(self, data, many, **kwargs): # pylint: disable=unused-argument
# stabilize import of auto-increment primary keys (not required),
# by matching import data to existing items and setting primary key
if not self._primary in data:
for item in getattr(self.recall('parent'), self.recall('field', 'parent')):
existing = self.dump(item, many=False)
this = existing.pop(self._primary)
if data == existing:
instance = item
data[self._primary] = this
break
parent = self.recall('parent')
if parent is not None:
for item in getattr(parent, self.recall('field', 'parent')):
existing = self.dump(item, many=False)
this = existing.pop(self._primary)
if data == existing:
instance = item
data[self._primary] = this
break

# try to load instance
instance = self.instance or self.get_instance(data)

# remember instance as parent for pruning siblings
if not self.Meta.sibling and self.context.get('update'):
self.store('parent', instance)

if instance is None:

if '__delete__' in data:
Expand All @@ -931,9 +938,6 @@ def _patch_item(self, data, many, **kwargs): # pylint: disable=unused-argument
else:

if self.context.get('update'):
# remember instance as parent for pruning siblings
if not self.Meta.sibling:
self.store('parent', instance)
# delete instance from session when marked
if '__delete__' in data:
self.opts.sqla_session.delete(instance)
Expand Down Expand Up @@ -1014,14 +1018,16 @@ def _prune_items(self, items, many, **kwargs): # pylint: disable=unused-argument
del_items = True

if add_items or del_items:
existing = {item[self._primary] for item in items if self._primary in item}
for item in getattr(self.recall('parent'), self.recall('field', 'parent')):
key = getattr(item, self._primary)
if key not in existing:
if add_items:
items.append({self._primary: key})
else:
items.append({self._primary: key, '__delete__': '?'})
parent = self.recall('parent')
if parent is not None:
existing = {item[self._primary] for item in items if self._primary in item}
for item in getattr(parent, self.recall('field', 'parent')):
key = getattr(item, self._primary)
if key not in existing:
if add_items:
items.append({self._primary: key})
else:
items.append({self._primary: key, '__delete__': '?'})

return items

Expand Down
1 change: 1 addition & 0 deletions towncrier/newsfragments/2601.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix creation of deep structures using import in update mode

0 comments on commit 814a3e6

Please sign in to comment.