def sync_from_client(self, objects): result = {} for sob in objects: exists = False conflict = False for o in self.objects: if o.guid == sob.guid: exists = True o.value = sob.value o.deleted = sob.deleted self.counter += 1 o.lastupdate_counter = self.counter elif o.pk == sob.pk: # pk conflict: do nothing because this should not occur on server # client will always sync from server to client first and handle # any pk conflicts before syncing from client to server exists = True conflict = True result['statuscode'] = NOK if not exists: no = Object(pk=sob.pk, name=sob.name, value=sob.value, guid=sob.guid) no.deleted = sob.deleted self.counter += 1 no.lastupdate_counter = self.counter self.objects.append(no) if 'statuscode' not in result: result['statuscode'] = OK result['servercounter'] = self.counter return result
def sync_from_server(self): result = self.server.sync_to_client(self.lastsync_servercounter) for sob in result['objects']: exists = False for o in self.objects: if o.guid == sob.guid or o.pk == sob.pk: exists = True # Handle pk conflict if o.pk == sob.pk: o.guid = sob.guid # Check for conflict (object updated locally since last sync to server) if o.lastupdate_counter > self.lastsync_counter: # Decide how to handle conflict if self.conflict_handling == SERVERPRIORITY: o.value = sob.value o.deleted = sob.deleted elif self.conflict_handling == CLIENTPRIORITY: ''' no change to local object ''' elif self.conflict_handling == TIMESTAMPPRIORITY: if sob.timestampupdated > o.timestampupdated: o.value = sob.value o.deleted = sob.deleted o.timestampupdated = datetime.now() else: # No conflict, update object locally o.value = sob.value o.deleted = sob.deleted if not exists: no = Object(sob.pk, sob.name, sob.value, sob.guid) no.deleted = sob.deleted no.lastupdate_counter = self.counter self.objects.append(no) if result['statuscode'] == OK: self.lastsync_servercounter = result['servercounter']