Skip to content

Instantly share code, notes, and snippets.

@vskrachkov
Last active February 25, 2018 20:43
Show Gist options
  • Save vskrachkov/5436b8797c5f36834760389079b61393 to your computer and use it in GitHub Desktop.
Save vskrachkov/5436b8797c5f36834760389079b61393 to your computer and use it in GitHub Desktop.
# models mixin
class SyncMixin:
@classmethod
def get_sync_type(cls) -> int:
"""Returns sync type."""
raise NotImplemented()
@classmethod
def get_sync_model(cls):
"""Returns django.db.Model child class.
This model will be used for building queryset for syncronization.
"""
return cls
@classmethod
def get_sync_queryset(cls):
"""Returns queryset for selecting rows that updated
or created and need synchronization.
"""
raise NotImplemented()
@classmethod
def get_deleted_rows_ids():
"""Rerurns ids of the deleted rows."""
raise NotImplemented()
# push sync
def run_push_sync():
# get models list for synchronization
models = get_push_sync_model()
for model in models:
# check that exist rows that need synchronization
if model.need_sync():
# get queryset for retirieving updated or created rows
q = model.get_sync_quryset()
# get ids of the deleted rows
deleted_ids = model.get_deleted_ids()
data = serialize(model_name=model.get_model_name(), q)
push_data(updated=data, deleted=deleted_ids)
# import sync
def run_import_sync():
models = get_import_sync_models()
data_to_save, data_to_delete = import_data(models)
succeed, fail = save_data(data_to_save)
succeed, fail = delete_data(data_to_delete)
# import sync utils
def _get_model(model_label):
try:
splitted = model_label.split('.')
if len(splitted) == 2:
_, model_name = splitted
elif len(splitted) == 1:
model_name = splitted[0]
else:
assert False, 'Invalid model label'
return get_model(model_name)
except (LookupError, TypeError, AssertionError):
raise DeserializationError(
'Invalid model identifier: {}'.format(model_label))
def deserialize(serialized):
Deserializer.func_globals['_get_model'] = _get_model
return Deserializer(object_list=serialized, ignorenonexistent=True)
def save_data(data):
for model in data:
model_name = model['name']
rows = model['rows']
for deserialized in deserialize(rows):
deserialized.save()
def delete_data(data):
for model in data:
model_name = model['name']
ids = model['ids']
model_cls = _get_model(model_name)
model_cls.objects.delete(pk__in=ids)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment