Skip to content

Commit

Permalink
Deleting objects during sync now uses overridable syncer method
Browse files Browse the repository at this point in the history
  • Loading branch information
rowanseymour committed Feb 23, 2016
1 parent 26342d4 commit 0f0db65
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
21 changes: 21 additions & 0 deletions casepro/backend/rapidpro.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@


class ContactSyncer(BaseSyncer):
"""
Syncer for contacts
"""
model = Contact

def fetch_local(self, org, identifier):
Expand Down Expand Up @@ -54,8 +57,14 @@ def update_required(self, local, remote):
def lock(self, identifier):
return self.model.lock(identifier)

def delete_local(self, local):
local.release()


class FieldSyncer(BaseSyncer):
"""
Syncer for contact fields
"""
model = Field

def identity(self, local_or_remote):
Expand All @@ -74,6 +83,9 @@ def update_required(self, local, remote):


class GroupSyncer(BaseSyncer):
"""
Syncer for contact groups
"""
model = Group

def local_kwargs(self, org, remote):
Expand All @@ -89,6 +101,9 @@ def update_required(self, local, remote):


class LabelSyncer(BaseSyncer):
"""
Syncer for message labels
"""
model = Label

def local_kwargs(self, org, remote):
Expand All @@ -107,6 +122,9 @@ def update_required(self, local, remote):


class MessageSyncer(BaseSyncer):
"""
Syncer for messages
"""
model = Message

def identity(self, local_or_remote):
Expand Down Expand Up @@ -147,6 +165,9 @@ def update_required(self, local, remote):
def lock(self, identifier):
return self.model.lock(identifier)

def delete_local(self, local):
local.release()


class RapidProBackend(BaseBackend):
"""
Expand Down
27 changes: 15 additions & 12 deletions casepro/dash_ext/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,20 @@ def local_kwargs(self, org, remote):
def update_required(self, local, remote):
"""
Determines whether local instance differs from the remote object and so needs to be updated
:param local: the local instance
:param remote: the remote object
:return: whether the local instance must be updated
"""
return True

def delete_local(self, local):
"""
Deletes a local instance
:param local:
:param remote:
:return:
"""
return True
local.is_active = False
local.save(update_fields=('is_active',))


def sync_local_to_set(org, syncer, remote_objects):
Expand All @@ -79,9 +88,6 @@ def sync_local_to_set(org, syncer, remote_objects):
existing_by_identity = {syncer.identity(g): g for g in syncer.fetch_all_local(org)}
synced_identifiers = set()

# any local active objects that need deactivated
invalid_existing_ids = []

for incoming in remote_objects:
existing = existing_by_identity.get(syncer.identity(incoming))

Expand All @@ -102,7 +108,7 @@ def sync_local_to_set(org, syncer, remote_objects):
num_updated += 1

elif existing.is_active:
invalid_existing_ids.append(existing.pk)
syncer.delete_local(existing)
num_deleted += 1

elif local_kwargs:
Expand All @@ -114,12 +120,9 @@ def sync_local_to_set(org, syncer, remote_objects):
# active local objects which weren't in the remote set need to be deleted
for existing in existing_by_identity.values():
if existing.is_active and syncer.identity(existing) not in synced_identifiers:
invalid_existing_ids.append(existing.pk)
syncer.delete_local(existing)
num_deleted += 1

if invalid_existing_ids:
model.objects.filter(org=org, pk__in=invalid_existing_ids).update(is_active=False)

return num_created, num_updated, num_deleted


Expand Down Expand Up @@ -162,7 +165,7 @@ def sync_local_to_changes(org, syncer, fetches, deleted_fetches, progress_callba
num_updated += 1

elif existing.is_active: # exists locally, but shouldn't now to due to model changes
existing.release()
syncer.delete_local(existing)
num_deleted += 1

elif local_kwargs:
Expand All @@ -179,7 +182,7 @@ def sync_local_to_changes(org, syncer, fetches, deleted_fetches, progress_callba
with syncer.lock(syncer.identity(deleted_remote)):
existing = syncer.fetch_local(org, syncer.identity(deleted_remote))
if existing:
existing.release()
syncer.delete_local(existing)
num_deleted += 1

num_synced += len(deleted_fetch)
Expand Down

0 comments on commit 0f0db65

Please sign in to comment.