def test_allowed_types(self): self.login() db = self.layer.app['plone'] site = create_content( 'Site', id='plone', title='Plone') site.__name__ = 'plone' db['plone'] = site contenttype_directive( self.layer.app.app.config, 'TestType', Folder, None, behaviors=None, add_permission=None, allowed_types=['Item']) self.layer.app.app.config.execute_actions() load_cached_schema() obj = create_content_in_container(site, 'TestType', 'foobar') constrains = IConstrainTypes(obj, None) self.assertEqual(constrains.get_allowed_types(), ['Item']) self.assertTrue(constrains.is_type_allowed('Item')) with self.assertRaises(NotAllowedContentType): create_content_in_container(obj, 'TestType', 'foobar') create_content_in_container(obj, 'Item', 'foobar')
def test_allowed_types(self): self.login() db = self.layer.app['plone'] site = create_content('Site', id='plone', title='Plone') site.__name__ = 'plone' db['plone'] = site contenttype_directive(self.layer.app.app.config, 'TestType', Folder, None, behaviors=None, add_permission=None, allowed_types=['Item']) self.layer.app.app.config.execute_actions() load_cached_schema() obj = create_content_in_container(site, 'TestType', 'foobar') constrains = IConstrainTypes(obj, None) self.assertEqual(constrains.get_allowed_types(), ['Item']) self.assertTrue(constrains.is_type_allowed('Item')) with self.assertRaises(NotAllowedContentType): create_content_in_container(obj, 'TestType', 'foobar') create_content_in_container(obj, 'Item', 'foobar')
def install(self, site, request): registry = request.site_settings registry.for_interface(ILayers).active_layers |= {USERS_LAYER} user = get_authenticated_user_id(request) create_content_in_container(site, 'UserManager', 'users', creators=(user, ), title='Users') create_content_in_container(site, 'GroupManager', 'groups', creators=(user, ), title='Groups')
def test_SerializeToJson(self): root = self.layer.new_root() site = root['plone'] self.login() obj = create_content_in_container(site, 'Item', 'foobar') adapter = getMultiAdapter((obj, self.request), interface=IResourceSerializeToJson) self.assertTrue(isinstance(adapter, SerializeToJson)) self.assertFalse(isinstance(adapter, SerializeFolderToJson))
def test_DefaultResourceFieldDeserializer(self): root = self.layer.new_root() site = root['plone'] self.login() obj = create_content_in_container(site, 'Item', 'foobar') adapter = getMultiAdapter((schema.Text(), obj, self.request), interface=IResourceFieldDeserializer) self.assertTrue( isinstance(adapter, deserialize_content_fields.DefaultResourceFieldDeserializer))
def test_SerializeToJson(self): root = self.layer.new_root() site = root['plone'] self.login() obj = create_content_in_container(site, 'Item', 'foobar') adapter = getMultiAdapter((obj, self.request), interface=IResourceSerializeToJson) self.assertTrue(isinstance(adapter, SerializeToJson)) self.assertFalse(isinstance(adapter, SerializeFolderToJson))
def test_DefaultFieldSerializer(self): root = self.layer.new_root() site = root['plone'] self.login() obj = create_content_in_container(site, 'Item', 'foobar') adapter = getMultiAdapter((schema.Text(), obj, self.request), interface=IResourceFieldSerializer) self.assertTrue( isinstance(adapter, serialize_content_field.DefaultFieldSerializer))
def test_get_index_data(self): self.login() db = self.layer.app['plone'] site = create_content('Site', id='plone', title='Plone') site.__name__ = 'plone' db['plone'] = site ob = create_content_in_container(site, 'Item', 'foobar') data = ICatalogDataAdapter(ob) fields = data() self.assertTrue('portal_type' in fields) self.assertTrue('uuid' in fields) self.assertTrue('path' in fields) self.assertTrue('title' in fields)
def test_get_index_data(self): self.login() db = self.layer.app['plone'] site = create_content( 'Site', id='plone', title='Plone') site.__name__ = 'plone' db['plone'] = site ob = create_content_in_container(site, 'Item', 'foobar') data = ICatalogDataAdapter(ob) fields = data() self.assertTrue('portal_type' in fields) self.assertTrue('uuid' in fields) self.assertTrue('path' in fields) self.assertTrue('title' in fields)
async def __call__(self): """To create a content.""" data = await self.get_data() type_ = data.get('@type', None) id_ = data.get('id', None) behaviors = data.get('@behaviors', None) if not type_: return ErrorResponse('RequiredParam', _("Property '@type' is required")) # Generate a temporary id if the id is not given if not id_: new_id = None else: new_id = id_ user = get_authenticated_user_id(self.request) # Create object try: obj = create_content_in_container(self.context, type_, new_id, id=new_id, creators=(user, ), contributors=(user, )) except PreconditionFailed as e: return ErrorResponse('PreconditionFailed', str(e), status=412) except ConflictIdOnContainer as e: return ErrorResponse('ConflictId', str(e), status=409) except ValueError as e: return ErrorResponse('CreatingObject', str(e), status=400) for behavior in behaviors or (): obj.add_behavior(behavior) # Update fields deserializer = queryMultiAdapter((obj, self.request), IResourceDeserializeFromJson) if deserializer is None: return ErrorResponse('DeserializationError', 'Cannot deserialize type {}'.format( obj.portal_type), status=501) try: await deserializer(data, validate_all=True) except DeserializationError as e: return ErrorResponse('DeserializationError', str(e), exc=e, status=400) # Local Roles assign owner as the creator user roleperm = IPrincipalRoleManager(obj) roleperm.assignRoleToPrincipal('plone.Owner', user) await notify(ObjectFinallyCreatedEvent(obj)) absolute_url = queryMultiAdapter((obj, self.request), IAbsoluteURL) headers = { 'Access-Control-Expose-Headers': 'Location', 'Location': absolute_url() } serializer = queryMultiAdapter((obj, self.request), IResourceSerializeToJson) return Response(response=serializer(), headers=headers, status=201)
async def __call__(self): """To create a content.""" data = await self.get_data() type_ = data.get('@type', None) id_ = data.get('id', None) behaviors = data.get('@behaviors', None) if '__acl__' in data: # we don't allow to change the permisions on this patch del data['__acl__'] if not type_: return ErrorResponse( 'RequiredParam', _("Property '@type' is required")) # Generate a temporary id if the id is not given if not id_: new_id = None else: new_id = id_ user = get_authenticated_user_id(self.request) # Create object try: obj = create_content_in_container( self.context, type_, new_id, id=new_id, creators=(user,), contributors=(user,)) except PreconditionFailed as e: return ErrorResponse( 'PreconditionFailed', str(e), status=412) except ConflictIdOnContainer as e: return ErrorResponse( 'ConflictId', str(e), status=409) except ValueError as e: return ErrorResponse( 'CreatingObject', str(e), status=400) for behavior in behaviors or (): obj.add_behavior(behavior) # Update fields deserializer = queryMultiAdapter((obj, self.request), IResourceDeserializeFromJson) if deserializer is None: return ErrorResponse( 'DeserializationError', 'Cannot deserialize type {}'.format(obj.portal_type), status=501) try: await deserializer(data, validate_all=True) except DeserializationError as e: return ErrorResponse( 'DeserializationError', str(e), exc=e, status=400) # Local Roles assign owner as the creator user roleperm = IPrincipalRoleManager(obj) roleperm.assign_role_to_principal( 'plone.Owner', user) await notify(ObjectFinallyCreatedEvent(obj, data)) absolute_url = queryMultiAdapter((obj, self.request), IAbsoluteURL) headers = { 'Access-Control-Expose-Headers': 'Location', 'Location': absolute_url() } serializer = queryMultiAdapter( (obj, self.request), IResourceSerializeToJson ) return Response(response=serializer(), headers=headers, status=201)