-
Notifications
You must be signed in to change notification settings - Fork 22
Add support for logging reverse relations in Binder history #277
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
Merged
knokko
merged 2 commits into
CodeYellowBV:master
from
Jvdputten:history-reverse-relation-support
Feb 12, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| from django.test import TestCase, Client | ||
| from django.contrib.auth.models import User | ||
| import json | ||
| from binder import history | ||
| from .testapp.models import ReverseParent, ReverseChild | ||
| from binder.models import install_history_signal_handlers, BinderModel | ||
|
|
||
|
|
||
| class ReverseRelationHistoryTest(TestCase): | ||
| def setUp(self): | ||
| u = User.objects.create_superuser("testuser", "test@example.com", "test") | ||
| self.client = Client() | ||
| self.client.login(username="testuser", password="test") | ||
| # Ensure signals are installed | ||
| install_history_signal_handlers(BinderModel) | ||
|
|
||
| def test_reverse_relation_history(self): | ||
| with history.atomic(source="test"): | ||
| parent = ReverseParent.objects.create(name="Mom") | ||
|
|
||
| with history.atomic(source="test"): | ||
| child = ReverseChild.objects.create(name="Kid", parent=parent) | ||
|
|
||
| # Check Parent history | ||
| changesets = history.Changeset.objects.filter( | ||
| changes__model="ReverseParent", | ||
| changes__oid=parent.pk, | ||
| changes__field="children", | ||
| ).distinct() | ||
| self.assertTrue(changesets.exists()) | ||
|
|
||
| change = history.Change.objects.filter( | ||
| model="ReverseParent", oid=parent.pk, field="children" | ||
| ).last() | ||
|
|
||
| # Before should be empty or [] | ||
| before = json.loads(change.before) | ||
| after = json.loads(change.after) | ||
|
|
||
| self.assertEqual(before, []) | ||
| self.assertIn(child.pk, after) | ||
|
|
||
| def test_reverse_relation_move(self): | ||
| with history.atomic(source="test"): | ||
| p1 = ReverseParent.objects.create(name="P1") | ||
| p2 = ReverseParent.objects.create(name="P2") | ||
| c1 = ReverseChild.objects.create(name="C1", parent=p1) | ||
|
|
||
| # Move C1 to P2 | ||
| with history.atomic(source="test"): | ||
| c1.parent = p2 | ||
| c1.save() | ||
|
|
||
| # P1 should show removal | ||
| change_p1 = history.Change.objects.filter( | ||
| model="ReverseParent", oid=p1.pk, field="children" | ||
| ).last() | ||
| self.assertIn(c1.pk, json.loads(change_p1.before)) | ||
| self.assertNotIn(c1.pk, json.loads(change_p1.after)) | ||
|
|
||
| # P2 should show addition | ||
| change_p2 = history.Change.objects.filter( | ||
| model="ReverseParent", oid=p2.pk, field="children" | ||
| ).last() | ||
| self.assertNotIn(c1.pk, json.loads(change_p2.before)) | ||
| self.assertIn(c1.pk, json.loads(change_p2.after)) | ||
|
|
||
| def test_reverse_relation_delete(self): | ||
| with history.atomic(source="test"): | ||
| p1 = ReverseParent.objects.create(name="P1") | ||
| c1 = ReverseChild.objects.create(name="C1", parent=p1) | ||
|
|
||
| c1_pk = c1.pk | ||
|
|
||
| # Delete child | ||
| with history.atomic(source="test"): | ||
| c1.delete() | ||
|
|
||
| # P1 should show removal | ||
| change_p1 = history.Change.objects.filter( | ||
| model="ReverseParent", oid=p1.pk, field="children" | ||
| ).last() | ||
| self.assertIn(c1_pk, json.loads(change_p1.before)) | ||
| self.assertNotIn(c1_pk, json.loads(change_p1.after)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| from django.test import TestCase, Client | ||
| from django.contrib.auth.models import User | ||
| import json | ||
| from binder import history | ||
| from .testapp.models import ReverseParentNoChildHistory, ReverseChildNoHistory | ||
| from binder.models import install_history_signal_handlers, BinderModel | ||
|
|
||
|
|
||
| class ReverseRelationNoChildHistoryTest(TestCase): | ||
| def setUp(self): | ||
| u = User.objects.create_superuser("testuser", "test@example.com", "test") | ||
| self.client = Client() | ||
| self.client.login(username="testuser", password="test") | ||
| # Ensure signals are installed | ||
| install_history_signal_handlers(BinderModel) | ||
|
|
||
| def test_reverse_relation_no_child_history(self): | ||
| with history.atomic(source="test"): | ||
| parent = ReverseParentNoChildHistory.objects.create(name="Mom") | ||
|
|
||
| with history.atomic(source="test"): | ||
| child = ReverseChildNoHistory.objects.create(name="Kid", parent=parent) | ||
|
|
||
| # Check Parent history | ||
| changesets = history.Changeset.objects.filter( | ||
| changes__model="ReverseParentNoChildHistory", | ||
| changes__oid=parent.pk, | ||
| changes__field="children", | ||
| ).distinct() | ||
| self.assertTrue(changesets.exists()) | ||
|
|
||
| change = history.Change.objects.filter( | ||
| model="ReverseParentNoChildHistory", oid=parent.pk, field="children" | ||
| ).last() | ||
|
|
||
| # Before should be empty or [] | ||
| before = json.loads(change.before) | ||
| after = json.loads(change.after) | ||
|
|
||
| self.assertEqual(before, []) | ||
| self.assertIn(child.pk, after) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| from django.db import models | ||
| from binder.models import BinderModel | ||
|
|
||
|
|
||
| class ReverseParent(BinderModel): | ||
| name = models.CharField(max_length=64) | ||
|
|
||
| class Binder: | ||
| history = True | ||
| include_reverse_relations = ["children"] | ||
|
|
||
| class Meta: | ||
| app_label = "testapp" | ||
|
|
||
|
|
||
| class ReverseChild(BinderModel): | ||
| name = models.CharField(max_length=64) | ||
| parent = models.ForeignKey( | ||
| ReverseParent, | ||
| on_delete=models.CASCADE, | ||
| related_name="children", | ||
| null=True, | ||
| blank=True, | ||
| ) | ||
|
|
||
| class Binder: | ||
| history = True | ||
|
|
||
| class Meta: | ||
| app_label = "testapp" | ||
|
|
||
|
|
||
| class ReverseParentNoChildHistory(BinderModel): | ||
| name = models.CharField(max_length=64) | ||
|
|
||
| class Binder: | ||
| history = True | ||
| include_reverse_relations = ["children"] | ||
|
|
||
| class Meta: | ||
| app_label = "testapp" | ||
|
|
||
|
|
||
| class ReverseChildNoHistory(BinderModel): | ||
| name = models.CharField(max_length=64) | ||
| parent = models.ForeignKey( | ||
| ReverseParentNoChildHistory, | ||
| on_delete=models.CASCADE, | ||
| related_name="children", | ||
| null=True, | ||
| blank=True, | ||
| ) | ||
|
|
||
| class Binder: | ||
| history = False | ||
|
|
||
| class Meta: | ||
| app_label = "testapp" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.