def update(self, request, id): """@description-title Update space @description Updates a space with the given ID. @param (int) "{id}" [required=true] The space's ID. @param (string) "name" [required=true] The name of the new space. @param (string) "description" [required=false] A description of the new space. @success (http-status-code) "200" 200 @success (json) "success-json" A JSON object containing information about the updated space. @success-example "success-json" [exkey=update-a-space] placeholder text @error (http-status-code) "404" 404 @error (content) "not-found" The requested space is not found. @error-example "not-found" Not Found """ if id == "-1" or id == Space.UNDEFINED: raise MAASAPIBadRequest("Space cannot be modified: %s" % Space.UNDEFINED) space = Space.objects.get_space_or_404(id, request.user, NodePermission.admin) form = SpaceForm(instance=space, data=request.data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def create(self, request): """Create a space. :param name: Name of the space. :param description: Description of the space. """ form = SpaceForm(data=request.data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def test_creates_space(self): space_name = factory.make_name("space") space_description = factory.make_name("description") form = SpaceForm({ "name": space_name, "description": space_description }) self.assertTrue(form.is_valid(), form.errors) space = form.save() self.assertEqual(space_name, space.get_name()) self.assertEqual(space_description, space.description)
def test__updates_space(self): new_name = factory.make_name("space") new_description = factory.make_name("description") space = factory.make_Space() form = SpaceForm(instance=space, data={ "name": new_name, "description": new_description, }) self.assertTrue(form.is_valid(), form.errors) form.save() self.assertEqual(new_name, reload_object(space).name) self.assertEqual(new_description, reload_object(space).description)
def update(self, request, id): """Update space. :param name: Name of the space. :param description: Description of the space. Returns 404 if the space is not found. """ if id == "-1" or id == Space.UNDEFINED: raise MAASAPIBadRequest("Space cannot be modified: %s" % Space.UNDEFINED) space = Space.objects.get_space_or_404(id, request.user, NodePermission.admin) form = SpaceForm(instance=space, data=request.data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def create(self, request): """@description-title Create a space @description Create a new space. @param (string) "name" [required=true] The name of the new space. @param (string) "description" [required=false] A description of the new space. @success (http-status-code) "200" 200 @success (json) "success-json" A JSON object containing information about the new space. @success-example "success-json" [exkey=create] placeholder text @error (http-status-code) "400" 400 @error (content) "already-exists" Space with this name already exists. """ form = SpaceForm(data=request.data) if form.is_valid(): return form.save() else: raise MAASAPIValidationError(form.errors)
def test_doest_require_name_on_update(self): space = factory.make_Space() form = SpaceForm(instance=space, data={}) self.assertTrue(form.is_valid(), form.errors)
def test_requires_name(self): form = SpaceForm({}) self.assertTrue(form.is_valid(), form.errors)