Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sparse indexes when deleting #16

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion drf_kit/signals.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from django.dispatch import Signal
from django.db.models.signals import post_delete
from django.dispatch import Signal, receiver

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -40,3 +41,15 @@ def __exit__(self, *args, **kwargs):
post_soft_delete = Signal(providing_args=["instance"])
pre_undelete = Signal(providing_args=["instance"])
post_undelete = Signal(providing_args=["instance"])


@receiver(post_delete)
def reorder_group(sender, instance, **kwargs):
from drf_kit.models import OrderedModelMixin # pylint: disable=import-outside-toplevel

if not issubclass(sender, OrderedModelMixin):
return

if previous := instance.previous():
# trigger the reordering
previous.save()
Comment on lines +46 to +55
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can register signals for all instances of this model instead of registering to all, and them filtering.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "drf-kit"
version = "1.9.0"
version = "1.10.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be 1.10.1

description = "DRF Toolkit"
authors = ["eduK <[email protected]>"]
packages = [
Expand Down
34 changes: 34 additions & 0 deletions test_app/tests/tests_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,37 @@ def test_create_before_range(self):
self.assertOrder(placement_2, 2)
self.assertOrder(placement_3, 3)
self.assertOrder(placement_4, 0)

def test_delete_within_range(self):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use factories now.

year = 1900

placement_1 = TriWizardPlacement.objects.create(year=year, wizard=self.wizards[0], prize="rock")
placement_2 = TriWizardPlacement.objects.create(year=year, wizard=self.wizards[1], prize="stone")
placement_3 = TriWizardPlacement.objects.create(year=year, wizard=self.wizards[2], prize="wand")
placement_4 = TriWizardPlacement.objects.create(year=year, wizard=self.wizards[3], prize="hug")

placement_2.delete()
joaodaher marked this conversation as resolved.
Show resolved Hide resolved

for placement in [placement_1, placement_3, placement_4]:
placement.refresh_from_db()

self.assertOrder(placement_1, 0)
self.assertOrder(placement_3, 1)
self.assertOrder(placement_4, 2)

def test_delete_from_queryset(self):
year = 1900

placement_1 = TriWizardPlacement.objects.create(year=year, wizard=self.wizards[0], prize="rock")
placement_2 = TriWizardPlacement.objects.create(year=year, wizard=self.wizards[1], prize="stone")
placement_3 = TriWizardPlacement.objects.create(year=year, wizard=self.wizards[2], prize="wand")
placement_4 = TriWizardPlacement.objects.create(year=year, wizard=self.wizards[3], prize="hug")

TriWizardPlacement.objects.filter(id=placement_2.id).delete()

for placement in [placement_1, placement_3, placement_4]:
placement.refresh_from_db()

self.assertOrder(placement_1, 0)
self.assertOrder(placement_3, 1)
self.assertOrder(placement_4, 2)