Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit c58bdb6

Browse files
authored
Merge pull request #164 from ambitioninc/develop
Release v5.1.0
2 parents 1cec277 + 6a258a7 commit c58bdb6

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

Diff for: entity/sync.py

+24
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,25 @@ def defer_entity_syncing(wrapped, instance, args, kwargs):
9292
sync_entities.buffer = {}
9393

9494

95+
@wrapt.decorator
96+
def suppress_entity_syncing(wrapped, instance, args, kwargs):
97+
"""
98+
A decorator that can be used to completely suppress syncing of entities as a result of
99+
execution of the decorated method
100+
"""
101+
102+
# Suppress entity syncing for the scope of the decorated method
103+
sync_entities.suppress = True
104+
105+
# Run the method
106+
try:
107+
return wrapped(*args, **kwargs)
108+
109+
# After we run the method return the entity sync state for future use
110+
finally:
111+
sync_entities.suppress = False
112+
113+
95114
def _get_super_entities_by_ctype(model_objs_by_ctype, model_ids_to_sync, sync_all):
96115
"""
97116
Given model objects organized by content type and a dictionary of all model IDs that need
@@ -174,6 +193,9 @@ def sync_entities(*model_objs):
174193
Args:
175194
model_objs (List[Model]): The model objects to sync. If empty, all entities will be synced
176195
"""
196+
if sync_entities.suppress:
197+
# Return false that we did not do anything
198+
return False
177199

178200
# Check if we are deferring processing
179201
if sync_entities.defer:
@@ -196,6 +218,8 @@ def sync_entities(*model_objs):
196218
# This is used by the defer_entity_syncing decorator
197219
sync_entities.defer = False
198220
sync_entities.buffer = {}
221+
# Add a suppress attribute to the sync entities method
222+
sync_entities.suppress = False
199223

200224

201225
def sync_entities_watching(instance):

Diff for: entity/tests/sync_tests.py

+29-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
from django_dynamic_fixture import G
88
from entity.config import EntityRegistry
99
from entity.models import Entity, EntityRelationship, EntityKind
10-
from entity.sync import sync_entities, defer_entity_syncing, transaction_atomic_with_retry, _get_super_entities_by_ctype
10+
from entity.sync import (
11+
sync_entities, defer_entity_syncing, transaction_atomic_with_retry, _get_super_entities_by_ctype,
12+
suppress_entity_syncing,
13+
)
1114
from entity.signal_handlers import turn_on_syncing, turn_off_syncing
1215
from mock import patch, MagicMock, call
1316

@@ -1062,6 +1065,31 @@ def test_method(test):
10621065
self.assertFalse(mock_sync_entities.called)
10631066

10641067

1068+
class SuppressEntitySyncingTests(EntityTestCase):
1069+
"""
1070+
Tests the suppress entity syncing decorator
1071+
"""
1072+
1073+
def test_defer(self):
1074+
@suppress_entity_syncing
1075+
def test_method(test, count):
1076+
# Create some entities
1077+
for i in range(count):
1078+
Account.objects.create()
1079+
1080+
# Assert that we do not have any entities
1081+
test.assertEquals(Entity.objects.all().count(), 0)
1082+
1083+
# Call the test method
1084+
test_method(self, count=5)
1085+
1086+
# Assert that after the method was run we did sync the entities
1087+
self.assertEquals(Entity.objects.all().count(), 0)
1088+
1089+
# Assert that we restored the suppress flag
1090+
self.assertFalse(sync_entities.suppress)
1091+
1092+
10651093
class TransactionAtomicWithRetryTests(EntityTestCase):
10661094
"""
10671095
Test the transaction_atomic_with_retry decorator

Diff for: entity/version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = '5.0.2'
1+
__version__ = '5.1.0'

Diff for: release_notes.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Release Notes
22

3+
- 5.1.0:
4+
- Add decorator to suppress entity syncing
35
- 5.0.2:
46
- Only fetch new entities on a sync_all
57
- 5.0.1:

0 commit comments

Comments
 (0)