def test_bundle_crud(self): """ Create, Fetch, Update, and Delete a Bundle """ coll = api.create_collection("Test Collection") args = { "title": "Water 💧 Bundle", "slug": "h2o", "description": "Sploosh", } # Create: bundle = api.create_bundle(coll.uuid, **args) for attr, value in args.items(): self.assertEqual(getattr(bundle, attr), value) self.assertIsInstance(bundle.uuid, UUID) # Fetch: bundle2 = api.get_bundle(bundle.uuid) self.assertEqual(bundle, bundle2) # Update: new_description = "Water Nation Bending Lessons" bundle3 = api.update_bundle(bundle.uuid, description=new_description) self.assertEqual(bundle3.description, new_description) bundle4 = api.get_bundle(bundle.uuid) self.assertEqual(bundle4.description, new_description) # Delete: api.delete_bundle(bundle.uuid) with self.assertRaises(api.BundleNotFound): api.get_bundle(bundle.uuid)
def test_bundle_crud(self): """ Create, Fetch, Update, and Delete a Bundle """ coll = api.create_collection("Test Collection") args = { "title": "Water 💧 Bundle", "slug": "h2o", "description": "Sploosh", } # Create: bundle = api.create_bundle(coll.uuid, **args) for attr, value in args.items(): assert getattr(bundle, attr) == value assert isinstance(bundle.uuid, UUID) # Fetch: bundle2 = api.get_bundle(bundle.uuid) assert bundle == bundle2 # Update: new_description = "Water Nation Bending Lessons" bundle3 = api.update_bundle(bundle.uuid, description=new_description) assert bundle3.description == new_description bundle4 = api.get_bundle(bundle.uuid) assert bundle4.description == new_description # Delete: api.delete_bundle(bundle.uuid) with pytest.raises(api.BundleNotFound): api.get_bundle(bundle.uuid)
def create_library( collection_uuid, library_type, org, slug, title, description, allow_public_learning, allow_public_read, library_license, ): """ Create a new content library. org: an organizations.models.Organization instance slug: a slug for this library like 'physics-problems' title: title for this library description: description of this library allow_public_learning: Allow anyone to read/learn from blocks in the LMS allow_public_read: Allow anyone to view blocks (including source) in Studio? Returns a ContentLibraryMetadata instance. """ assert isinstance(collection_uuid, UUID) assert isinstance(org, Organization) validate_unicode_slug(slug) # First, create the blockstore bundle: bundle = create_bundle( collection_uuid, slug=slug, title=title, description=description, ) # Now create the library reference in our database: try: ref = ContentLibrary.objects.create( org=org, slug=slug, type=library_type, bundle_uuid=bundle.uuid, allow_public_learning=allow_public_learning, allow_public_read=allow_public_read, license=library_license, ) except IntegrityError: delete_bundle(bundle.uuid) raise LibraryAlreadyExists(slug) CONTENT_LIBRARY_CREATED.send(sender=None, library_key=ref.library_key) return ContentLibraryMetadata( key=ref.library_key, bundle_uuid=bundle.uuid, title=title, type=library_type, description=description, num_blocks=0, version=0, last_published=None, allow_public_learning=ref.allow_public_learning, allow_public_read=ref.allow_public_read, license=library_license, )
def delete_library(library_key): """ Delete a content library """ ref = ContentLibrary.objects.get_by_key(library_key) bundle_uuid = ref.bundle_uuid # We can't atomically delete the ref and bundle at the same time. # Delete the ref first, then the bundle. An error may cause the bundle not # to get deleted, but the library will still be effectively gone from the # system, which is a better state than having a reference to a library with # no backing blockstore bundle. ref.delete() try: delete_bundle(bundle_uuid) except: log.exception("Failed to delete blockstore bundle %s when deleting library. Delete it manually.", bundle_uuid) raise
def create_library(collection_uuid, org, slug, title, description): """ Create a new content library. org: an organizations.models.Organization instance slug: a slug for this library like 'physics-problems' title: title for this library description: description of this library Returns a ContentLibraryMetadata instance. """ assert isinstance(collection_uuid, UUID) assert isinstance(org, Organization) validate_unicode_slug(slug) # First, create the blockstore bundle: bundle = create_bundle( collection_uuid, slug=slug, title=title, description=description, ) # Now create the library reference in our database: try: ref = ContentLibrary.objects.create( org=org, slug=slug, bundle_uuid=bundle.uuid, allow_public_learning=True, allow_public_read=True, ) except IntegrityError: delete_bundle(bundle.uuid) raise LibraryAlreadyExists(slug) return ContentLibraryMetadata( key=ref.library_key, bundle_uuid=bundle.uuid, title=title, description=description, version=0, )