def _sync_deleted_items(self, deleted_items): adapter = get_adapter(self.model) db_conn = adapter.conn_ops['db'](settings.MONGODB_CONF) update_table = adapter.conn_ops['table'] for obj in deleted_items: to_obj = adapter.convert_to(obj) cond = {'pk': to_obj['pk']} if 'pk' in to_obj else {'id': to_obj['id']} db_conn.delete_item(update_table, cond)
def sync_to_production(modeladmin, request=None): adapter = get_adapter(modeladmin.model) db_conn = adapter.conn_ops['db'](settings.MONGODB_CONF) update_table = adapter.conn_ops['table'] need_sync_items = (modeladmin.queryset(request) if request else modeladmin.model.objects).filter(sync_status=0) sync_successed, sync_failed = 0, 0 for i, obj in enumerate(need_sync_items): can_upsert = obj.published and not obj.hided to_obj = {} try: to_obj = adapter.convert_to(obj) if not to_obj: msg = 'convert to failed %s, id: %s' % (modeladmin.model, obj.pk) logger.warn(msg) sync_failed += 1 else: cond = { 'pk': to_obj['pk'] } if 'pk' in to_obj else { 'id': to_obj['id'] } if can_upsert: db_conn.upsert_item(update_table, cond, to_obj, upsert=True) else: db_conn.delete_item(update_table, cond) obj.sync_status = 1 obj.save() sync_successed += 1 except Exception: msg = 'sync failed %s, id: %s' % (modeladmin.model, obj.pk) logger.exception(msg) sync_failed += 1 success_msg = _( "%(count)d %(items)s successfully sync data to production environment.\n" ) % { 'count': sync_successed, 'items': model_ngettext(modeladmin.opts, sync_successed) } failed_msg = _( "%(count)d %(items)s fail sync data to production environment.\n") % { 'count': sync_failed, 'items': model_ngettext(modeladmin.opts, sync_failed) } if request: modeladmin.message_user(request, success_msg + failed_msg) else: print success_msg + failed_msg
def _sync_deleted_items(self, deleted_items): adapter = get_adapter(self.model) db_conn = adapter.conn_ops['db'](settings.MONGODB_CONF) update_table = adapter.conn_ops['table'] for obj in deleted_items: to_obj = adapter.convert_to(obj) cond = { 'pk': to_obj['pk'] } if 'pk' in to_obj else { 'id': to_obj['id'] } db_conn.delete_item(update_table, cond)
def sync_to_production(modeladmin, request=None): adapter = get_adapter(modeladmin.model) db_conn = adapter.conn_ops['db'](settings.MONGODB_CONF) update_table = adapter.conn_ops['table'] need_sync_items = (modeladmin.queryset(request) if request else modeladmin.model.objects).filter(sync_status=0) sync_successed, sync_failed = 0, 0 for i, obj in enumerate(need_sync_items): can_upsert = obj.published and not obj.hided to_obj = {} try: to_obj = adapter.convert_to(obj) if not to_obj: msg = 'convert to failed %s, id: %s' % (modeladmin.model, obj.pk) logger.warn(msg) sync_failed += 1 else: cond = {'pk': to_obj['pk']} if 'pk' in to_obj else {'id': to_obj['id']} if can_upsert: db_conn.upsert_item(update_table, cond, to_obj, upsert=True) else: db_conn.delete_item(update_table, cond) obj.sync_status = 1 obj.save() sync_successed += 1 except Exception: msg = 'sync failed %s, id: %s' % (modeladmin.model, obj.pk) logger.exception(msg) sync_failed += 1 success_msg = _("%(count)d %(items)s successfully sync data to production environment.\n") % { 'count': sync_successed, 'items': model_ngettext(modeladmin.opts, sync_successed) } failed_msg = _("%(count)d %(items)s fail sync data to production environment.\n") % { 'count': sync_failed, 'items': model_ngettext(modeladmin.opts, sync_failed) } if request: modeladmin.message_user(request, success_msg + failed_msg) else: print success_msg + failed_msg
def sync_from_production(modeladmin, request): model_cls = modeladmin.model adapter = get_adapter(model_cls) db_conn = adapter.conn_ops['db'](settings.MONGODB_CONF) start_time = get_last_sync_timestamp(model_cls) need_sync_items = getattr(db_conn, adapter.conn_ops['method'])(start_time) for item in need_sync_items: to_model = adapter.convert_from(model_cls(), item) to_model.save() count = len(need_sync_items) if count: modeladmin.message_user(request, _("%(count)d %(items)s successfully sync data from production environment.") % { 'count': count, 'items': model_ngettext(modeladmin.opts, count) }) else: modeladmin.message_user(request, _("No %(items)s need to sync to production environment.") % { 'items': model_ngettext(modeladmin.opts, count), })
def sync_obj(obj, cls): adapter = get_adapter(cls) db_conn = adapter.conn_ops['db'](settings.MONGODB_CONF) update_table = adapter.conn_ops['table'] to_obj = adapter.convert_to(obj) cond = {'pk': to_obj['pk']} if 'pk' in to_obj else {'id': to_obj['id']} if obj.published: try: db_conn.upsert_item(update_table, cond, to_obj, upsert=True) except: trace_stack = '\n'.join(traceback.format_exception(*sys.exc_info())) print 'cond: %s, obj:%s, e: %s' % (cond, to_obj, trace_stack) else: try: db_conn.delete_item(update_table, cond) except: trace_stack = '\n'.join(traceback.format_exception(*sys.exc_info())) print 'cond: %s, e: %s' % (cond, trace_stack) obj.sync_status = 1 obj.save()