def update_sources(remote_sources: List[SDKSource], local_sources: List[Source], session: Session, data_dir: str) -> None: """ Given collections of remote sources, the current local sources and a session to the local database, ensure the state of the local database matches that of the remote sources: * Existing items are updated in the local database. * New items are created in the local database. * Local items not returned in the remote sources are deleted from the local database. """ local_sources_by_uuid = {s.uuid: s for s in local_sources} for source in remote_sources: if source.uuid in local_sources_by_uuid: # Update an existing record. local_source = local_sources_by_uuid[source.uuid] lazy_setattr(local_source, "journalist_designation", source.journalist_designation) lazy_setattr(local_source, "is_flagged", source.is_flagged) lazy_setattr(local_source, "interaction_count", source.interaction_count) lazy_setattr(local_source, "document_count", source.number_of_documents) lazy_setattr(local_source, "is_starred", source.is_starred) lazy_setattr(local_source, "last_updated", parse(source.last_updated)) lazy_setattr(local_source, "public_key", source.key['public']) # Removing the UUID from local_sources_by_uuid ensures # this record won't be deleted at the end of this # function. del local_sources_by_uuid[source.uuid] logger.debug('Updated source {}'.format(source.uuid)) else: # A new source to be added to the database. ns = Source( uuid=source.uuid, journalist_designation=source.journalist_designation, is_flagged=source.is_flagged, interaction_count=source.interaction_count, is_starred=source.is_starred, last_updated=parse(source.last_updated), document_count=source.number_of_documents, public_key=source.key['public'], fingerprint=source.key['fingerprint'], ) session.add(ns) logger.debug('Added new source {}'.format(source.uuid)) # The uuids remaining in local_uuids do not exist on the remote server, so # delete the related records. for deleted_source in local_sources_by_uuid.values(): delete_source_collection(deleted_source.journalist_filename, data_dir) session.delete(deleted_source) logger.debug('Deleted source {}'.format(deleted_source.uuid)) session.commit()
def update_sources(remote_sources: List[SDKSource], local_sources: List[Source], session: Session, data_dir: str) -> None: """ Given collections of remote sources, the current local sources and a session to the local database, ensure the state of the local database matches that of the remote sources: * Existing items are updated in the local database. * New items are created in the local database. * Local items not returned in the remote sources are deleted from the local database. """ local_uuids = {source.uuid for source in local_sources} for source in remote_sources: if source.uuid in local_uuids: # Update an existing record. local_source = [s for s in local_sources if s.uuid == source.uuid][0] local_source.journalist_designation = source.journalist_designation local_source.is_flagged = source.is_flagged local_source.public_key = source.key['public'] local_source.interaction_count = source.interaction_count local_source.document_count = source.number_of_documents local_source.is_starred = source.is_starred local_source.last_updated = parse(source.last_updated) # Removing the UUID from local_uuids ensures this record won't be # deleted at the end of this function. local_uuids.remove(source.uuid) logger.debug('Updated source {}'.format(source.uuid)) else: # A new source to be added to the database. ns = Source(uuid=source.uuid, journalist_designation=source.journalist_designation, is_flagged=source.is_flagged, public_key=source.key['public'], interaction_count=source.interaction_count, is_starred=source.is_starred, last_updated=parse(source.last_updated), document_count=source.number_of_documents) session.add(ns) logger.debug('Added new source {}'.format(source.uuid)) # The uuids remaining in local_uuids do not exist on the remote server, so # delete the related records. for deleted_source in [s for s in local_sources if s.uuid in local_uuids]: for document in deleted_source.collection: delete_single_submission_or_reply_on_disk(document, data_dir) session.delete(deleted_source) logger.debug('Deleted source {}'.format(deleted_source.uuid)) session.commit()
def source(session) -> dict: args = {"uuid": str(uuid4()), "public_key": PUB_KEY} source = Source(journalist_designation="foo-bar", is_flagged=False, interaction_count=0, is_starred=False, last_updated=datetime.now(), document_count=0, **args) args[ "fingerprint"] = source.fingerprint = "B2FF7FB28EED8CABEBC5FB6C6179D97BCFA52E5F" session.add(source) session.commit() args["id"] = source.id args["source"] = source return args
def source(session) -> dict: args = { 'uuid': str(uuid4()), 'public_key': PUB_KEY, } source = Source(journalist_designation='foo-bar', is_flagged=False, interaction_count=0, is_starred=False, last_updated=datetime.now(), document_count=0, **args) args[ 'fingerprint'] = source.fingerprint = 'B2FF7FB28EED8CABEBC5FB6C6179D97BCFA52E5F' session.add(source) session.commit() args['id'] = source.id args['source'] = source return args