Пример #1
0
    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,
        })
Пример #2
0
    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
        }
Пример #3
0
def revert_changes(library_key):
    """
    Revert all pending changes to the specified library, restoring it to the
    last published version.
    """
    ref = ContentLibrary.objects.get_by_key(library_key)
    bundle = get_bundle(ref.bundle_uuid)
    if DRAFT_NAME in bundle.drafts:  # pylint: disable=unsupported-membership-test
        draft_uuid = bundle.drafts[DRAFT_NAME]  # pylint: disable=unsubscriptable-object
        delete_draft(draft_uuid)
    else:
        return  # If there is no draft, no action is needed.
    LibraryBundle(library_key, ref.bundle_uuid, draft_name=DRAFT_NAME).cache.clear()