def get_bundle_version_number(bundle_uuid, draft_name=None): """ Get the current version number of the specified bundle/draft. If a draft is specified, the update timestamp is used in lieu of a version number. """ cache_key = 'bundle_version:{}:{}'.format(bundle_uuid, draft_name or '') version = cache.get(cache_key) if version is not None: return version else: version = 0 # Default to 0 in case bundle/draft is empty or doesn't exist bundle_metadata = blockstore_api.get_bundle(bundle_uuid) if draft_name: draft_uuid = bundle_metadata.drafts.get(draft_name) # pylint: disable=no-member if draft_uuid: draft_metadata = blockstore_api.get_draft(draft_uuid) # Convert the 'updated_at' datetime info an integer value with microsecond accuracy. updated_at_timestamp = ( draft_metadata.updated_at - datetime(1970, 1, 1, tzinfo=UTC)).total_seconds() version = int(updated_at_timestamp * 1e6) # If we're not using a draft or the draft does not exist [anymore], fall # back to the bundle version, if any versions have been published: if version == 0 and bundle_metadata.latest_version: version = bundle_metadata.latest_version cache.set(cache_key, version, timeout=MAX_BLOCKSTORE_CACHE_DELAY) return version
def test_drafts_and_files(self): """ Test creating, reading, writing, committing, and reverting drafts and files. """ coll = api.create_collection("Test Collection") bundle = api.create_bundle(coll.uuid, title="Earth 🗿 Bundle", slug="earth", description="another test bundle") # Create a draft draft = api.get_or_create_bundle_draft(bundle.uuid, draft_name="test-draft") self.assertEqual(draft.bundle_uuid, bundle.uuid) self.assertEqual(draft.name, "test-draft") self.assertGreaterEqual(draft.updated_at.year, 2019) # And retrieve it again: draft2 = api.get_or_create_bundle_draft(bundle.uuid, draft_name="test-draft") self.assertEqual(draft, draft2) # Also test retrieving using get_draft draft3 = api.get_draft(draft.uuid) self.assertEqual(draft, draft3) # Write a file into the bundle: api.write_draft_file(draft.uuid, "test.txt", "initial version") # Now the file should be visible in the draft: draft_contents = api.get_bundle_file_data(bundle.uuid, "test.txt", use_draft=draft.name) self.assertEqual(draft_contents, "initial version") api.commit_draft(draft.uuid) # Write a new version into the draft: api.write_draft_file(draft.uuid, "test.txt", "modified version") published_contents = api.get_bundle_file_data(bundle.uuid, "test.txt") self.assertEqual(published_contents, "initial version") draft_contents2 = api.get_bundle_file_data(bundle.uuid, "test.txt", use_draft=draft.name) self.assertEqual(draft_contents2, "modified version") # Now delete the draft: api.delete_draft(draft.uuid) draft_contents3 = api.get_bundle_file_data(bundle.uuid, "test.txt", use_draft=draft.name) # Confirm the file is now reset: self.assertEqual(draft_contents3, "initial version") # Finaly, test the get_bundle_file* methods: file_info1 = api.get_bundle_file_metadata(bundle.uuid, "test.txt") self.assertEqual(file_info1.path, "test.txt") self.assertEqual(file_info1.size, len("initial version")) self.assertEqual(file_info1.hash_digest, "a45a5c6716276a66c4005534a51453ab16ea63c4") self.assertEqual(api.get_bundle_files(bundle.uuid), [file_info1]) self.assertEqual(api.get_bundle_files_dict(bundle.uuid), { "test.txt": file_info1, })
def test_drafts_and_files(self): """ Test creating, reading, writing, committing, and reverting drafts and files. """ coll = api.create_collection("Test Collection") bundle = api.create_bundle(coll.uuid, title="Earth 🗿 Bundle", slug="earth", description="another test bundle") # Create a draft draft = api.get_or_create_bundle_draft(bundle.uuid, draft_name="test-draft") assert draft.bundle_uuid == bundle.uuid assert draft.name == 'test-draft' assert draft.updated_at.year >= 2019 # And retrieve it again: draft2 = api.get_or_create_bundle_draft(bundle.uuid, draft_name="test-draft") assert draft == draft2 # Also test retrieving using get_draft draft3 = api.get_draft(draft.uuid) assert draft == draft3 # Write a file into the bundle: api.write_draft_file(draft.uuid, "test.txt", b"initial version") # Now the file should be visible in the draft: draft_contents = api.get_bundle_file_data(bundle.uuid, "test.txt", use_draft=draft.name) assert draft_contents == b'initial version' api.commit_draft(draft.uuid) # Write a new version into the draft: api.write_draft_file(draft.uuid, "test.txt", b"modified version") published_contents = api.get_bundle_file_data(bundle.uuid, "test.txt") assert published_contents == b'initial version' draft_contents2 = api.get_bundle_file_data(bundle.uuid, "test.txt", use_draft=draft.name) assert draft_contents2 == b'modified version' # Now delete the draft: api.delete_draft(draft.uuid) draft_contents3 = api.get_bundle_file_data(bundle.uuid, "test.txt", use_draft=draft.name) # Confirm the file is now reset: assert draft_contents3 == b'initial version' # Finaly, test the get_bundle_file* methods: file_info1 = api.get_bundle_file_metadata(bundle.uuid, "test.txt") assert file_info1.path == 'test.txt' assert file_info1.size == len(b'initial version') assert file_info1.hash_digest == 'a45a5c6716276a66c4005534a51453ab16ea63c4' assert list(api.get_bundle_files(bundle.uuid)) == [file_info1] assert api.get_bundle_files_dict(bundle.uuid) == { 'test.txt': file_info1 }
def test_nonexistent_draft(self): """ Request a draft that doesn't exist -> DraftNotFound """ with self.assertRaises(api.DraftNotFound): api.get_draft(BAD_UUID)