def test_container_simple_move(self): container = Container(__name__ = 'container', __path__ = '/container/') content = Content(title='Content') ptah.cms.Session.add(container) ptah.cms.Session.add(content) ptah.cms.Session.flush() container['content'] = content content_uri = content.__uri__ container_uri = container.__uri__ transaction.commit() container = ptah.resolve(container_uri) content = ptah.resolve(content_uri) container['moved'] = content self.assertEqual(content.__name__, 'moved') self.assertEqual(content.__path__, '/container/moved/') self.assertEqual(content.__parent__, container) self.assertEqual(content.__parent_uri__, container.__uri__) transaction.commit() container = ptah.resolve(container_uri) self.assertEqual(container.keys(), ['moved']) self.assertEqual(container['moved'].__uri__, content_uri)
def test_container_delete(self): container = Container(__name__ = 'container', __path__ = '/container/') content = Content(title='Content') ptah.cms.Session.add(container) ptah.cms.Session.add(content) ptah.cms.Session.flush() container['content'] = content content_uri = content.__uri__ container_uri = container.__uri__ transaction.commit() container = ptah.resolve(container_uri) del container['content'] self.assertEqual(container.keys(), []) self.assertEqual(container.values(), []) transaction.commit() self.assertTrue(ptah.resolve(content_uri) is None) container = ptah.resolve(container_uri) self.assertEqual(container.keys(), []) self.assertRaises(KeyError, container.__delitem__, 'content') self.assertRaises(KeyError, container.__delitem__, Content())
def generateThumbnail(theme, width=210): blob = ptah.resolve(theme.preview) if not blob: return data = StringIO(blob.read()) image = PIL.Image.open(data) if image.mode == '1': image = image.convert('L') elif image.mode == 'P': image = image.convert('RGBA') # get width, height orig_size = image.size scale = float(width)/orig_size[0] height = int(round(orig_size[1] * scale)) #convert image pilfilter = PIL.Image.NEAREST pilfilter = PIL.Image.ANTIALIAS image.thumbnail((width, height), pilfilter) newfile = StringIO() image.save(newfile, 'jpeg', quality=88) blob = ptah.resolve(theme.thumbnail) if blob is None: blob = ptah.cms.blob_storage.create(theme) theme.thumbnail = blob.__uri__ blob.write(newfile.getvalue()) blob.updateMetadata(mimetype = 'image/jpeg')
def test_container_getitem(self): container = Container(__name__="container", __path__="/container/") content1 = Content(title="Content1") content2 = Content(title="Content2") container["content1"] = content1 container["content2"] = content2 self.assertEqual(container["content1"].__uri__, content1.__uri__) self.assertEqual(container["content2"].__uri__, content2.__uri__) self.assertEqual(container.get("content1").__uri__, content1.__uri__) self.assertEqual(container.get("content2").__uri__, content2.__uri__) ptah.cms.Session.add(container) ptah.cms.Session.add(content1) ptah.cms.Session.add(content2) ptah.cms.Session.flush() c_u = container.__uri__ c1_u = content1.__uri__ c2_u = content2.__uri__ transaction.commit() container = ptah.resolve(c_u) self.assertEqual(container["content1"].__uri__, c1_u) self.assertEqual(container["content2"].__uri__, c2_u) transaction.commit() container = ptah.resolve(c_u) self.assertEqual(container.get("content1").__uri__, c1_u) self.assertEqual(container.get("content2").__uri__, c2_u)
def test_container_insert_subtree(self): container = Container(__name__="container", __path__="/container/") folder = Container(title="Folder") content = Content(title="Content") ptah.cms.Session.add(container) ptah.cms.Session.add(folder) ptah.cms.Session.add(content) folder["content"] = content container["folder"] = folder content_uri = content.__uri__ folder_uri = folder.__uri__ container_uri = container.__uri__ transaction.commit() container = ptah.resolve(container_uri) content = ptah.resolve(content_uri) folder = ptah.resolve(folder_uri) self.assertEqual(container.keys(), ["folder"]) self.assertEqual(folder.keys(), ["content"]) self.assertEqual(folder.__path__, "/container/folder/") self.assertEqual(content.__path__, "/container/folder/content/") transaction.commit()
def test_container_simple_move(self): container = Container(__name__="container", __path__="/container/") content = Content(title="Content") ptah.cms.Session.add(container) ptah.cms.Session.add(content) ptah.cms.Session.flush() container["content"] = content content_uri = content.__uri__ container_uri = container.__uri__ transaction.commit() container = ptah.resolve(container_uri) content = ptah.resolve(content_uri) container["moved"] = content self.assertEqual(content.__name__, "moved") self.assertEqual(content.__path__, "/container/moved/") self.assertEqual(content.__parent__, container) self.assertEqual(content.__parent_uri__, container.__uri__) transaction.commit() container = ptah.resolve(container_uri) self.assertEqual(container.keys(), ["moved"]) self.assertEqual(container["moved"].__uri__, content_uri)
def test_container_insert_subtree(self): container = Container(__name__ = 'container', __path__ = '/container/') folder = Container(title='Folder') content = Content(title='Content') ptah.cms.Session.add(container) ptah.cms.Session.add(folder) ptah.cms.Session.add(content) folder['content'] = content container['folder'] = folder content_uri = content.__uri__ folder_uri = folder.__uri__ container_uri = container.__uri__ transaction.commit() container = ptah.resolve(container_uri) content = ptah.resolve(content_uri) folder = ptah.resolve(folder_uri) self.assertEqual(container.keys(), ['folder']) self.assertEqual(folder.keys(), ['content']) self.assertEqual(folder.__path__, '/container/folder/') self.assertEqual(content.__path__, '/container/folder/content/') transaction.commit()
def test_container_simple_move(self): container = self.Container(__name__='container', __path__='/container/') content = self.Content(title='Content') ptah.cms.Session.add(container) ptah.cms.Session.add(content) ptah.cms.Session.flush() container['content'] = content content_uri = content.__uri__ container_uri = container.__uri__ transaction.commit() container = ptah.resolve(container_uri) content = ptah.resolve(content_uri) container['moved'] = content self.assertEqual(content.__name__, 'moved') self.assertEqual(content.__path__, '/container/moved/') self.assertEqual(content.__parent__, container) self.assertEqual(content.__parent_uri__, container.__uri__) transaction.commit() container = ptah.resolve(container_uri) self.assertEqual(container.keys(), ['moved']) self.assertEqual(container['moved'].__uri__, content_uri)
def test_container_delete_recursive(self): container = self.Container(__name__='container', __path__='/container/') folder = self.Container(title='Folder') content = self.Content(title='Content') ptah.cms.Session.add(container) ptah.cms.Session.add(folder) ptah.cms.Session.add(content) ptah.cms.Session.flush() container['folder'] = folder folder['content'] = content content_uri = content.__uri__ container_uri = container.__uri__ folder_uri = folder.__uri__ transaction.commit() container = ptah.resolve(container_uri) del container['folder'] transaction.commit() self.assertTrue(ptah.resolve(content_uri) is None) self.assertTrue(ptah.resolve(folder_uri) is None)
def test_container_getitem(self): container = Container(__name__ = 'container', __path__ = '/container/') content1 = Content(title='Content1') content2 = Content(title='Content2') container['content1'] = content1 container['content2'] = content2 self.assertEqual(container['content1'].__uri__, content1.__uri__) self.assertEqual(container['content2'].__uri__, content2.__uri__) self.assertEqual(container.get('content1').__uri__, content1.__uri__) self.assertEqual(container.get('content2').__uri__, content2.__uri__) ptah.cms.Session.add(container) ptah.cms.Session.add(content1) ptah.cms.Session.add(content2) ptah.cms.Session.flush() c_u = container.__uri__ c1_u = content1.__uri__ c2_u = content2.__uri__ transaction.commit() container = ptah.resolve(c_u) self.assertEqual(container['content1'].__uri__, c1_u) self.assertEqual(container['content2'].__uri__, c2_u) transaction.commit() container = ptah.resolve(c_u) self.assertEqual(container.get('content1').__uri__, c1_u) self.assertEqual(container.get('content2').__uri__, c2_u)
def generateThumbnail(theme, width=210): blob = ptah.resolve(theme.preview) if not blob: return data = StringIO(blob.read()) image = PIL.Image.open(data) if image.mode == '1': image = image.convert('L') elif image.mode == 'P': image = image.convert('RGBA') # get width, height orig_size = image.size scale = float(width) / orig_size[0] height = int(round(orig_size[1] * scale)) #convert image pilfilter = PIL.Image.NEAREST pilfilter = PIL.Image.ANTIALIAS image.thumbnail((width, height), pilfilter) newfile = StringIO() image.save(newfile, 'jpeg', quality=88) blob = ptah.resolve(theme.thumbnail) if blob is None: blob = ptah.cms.blob_storage.create(theme) theme.thumbnail = blob.__uri__ blob.write(newfile.getvalue()) blob.updateMetadata(mimetype='image/jpeg')
def test_container_delete(self): container = self.Container(__name__='container', __path__='/container/') content = self.Content(title='Content') ptah.cms.Session.add(container) ptah.cms.Session.add(content) ptah.cms.Session.flush() container['content'] = content content_uri = content.__uri__ container_uri = container.__uri__ transaction.commit() container = ptah.resolve(container_uri) del container['content'] self.assertEqual(container.keys(), []) self.assertEqual(container.values(), []) transaction.commit() self.assertTrue(ptah.resolve(content_uri) is None) container = ptah.resolve(container_uri) self.assertEqual(container.keys(), []) self.assertRaises(KeyError, container.__delitem__, 'content') self.assertRaises(KeyError, container.__delitem__, Content())
def test_container_insert_subtree(self): container = self.Container(__name__='container', __path__='/container/') folder = self.Container(title='Folder') content = self.Content(title='Content') ptah.cms.Session.add(container) ptah.cms.Session.add(folder) ptah.cms.Session.add(content) folder['content'] = content container['folder'] = folder content_uri = content.__uri__ folder_uri = folder.__uri__ container_uri = container.__uri__ transaction.commit() container = ptah.resolve(container_uri) content = ptah.resolve(content_uri) folder = ptah.resolve(folder_uri) self.assertEqual(container.keys(), ['folder']) self.assertEqual(folder.keys(), ['content']) self.assertEqual(folder.__path__, '/container/folder/') self.assertEqual(content.__path__, '/container/folder/content/') transaction.commit()
def test_container_getitem(self): container = self.Container(__name__='container', __path__='/container/') content1 = self.Content(title='Content1') content2 = self.Content(title='Content2') container['content1'] = content1 container['content2'] = content2 self.assertEqual(container['content1'].__uri__, content1.__uri__) self.assertEqual(container['content2'].__uri__, content2.__uri__) self.assertEqual(container.get('content1').__uri__, content1.__uri__) self.assertEqual(container.get('content2').__uri__, content2.__uri__) ptah.cms.Session.add(container) ptah.cms.Session.add(content1) ptah.cms.Session.add(content2) ptah.cms.Session.flush() c_u = container.__uri__ c1_u = content1.__uri__ c2_u = content2.__uri__ transaction.commit() container = ptah.resolve(c_u) self.assertEqual(container['content1'].__uri__, c1_u) self.assertEqual(container['content2'].__uri__, c2_u) transaction.commit() container = ptah.resolve(c_u) self.assertEqual(container.get('content1').__uri__, c1_u) self.assertEqual(container.get('content2').__uri__, c2_u)
def __call__(self): self.tinfo = ptah.resolve(self.params['turi']) with ptah.sa_session() as sa: self.session = sa self.context = ptah.resolve(self.params['uri']) return super(EditRecord, self).__call__()
def test_superuser_resolver(self): import ptah from ptah.authentication import SUPERUSER self._init_ptah() user = ptah.resolve(ptah.SUPERUSER_URI) self.assertIs(user, SUPERUSER) self.assertIsNone(ptah.resolve("ptah+auth:unknown"))
def test_superuser_resolver(self): import ptah from ptah.authentication import SUPERUSER self.init_ptah() user = ptah.resolve(ptah.SUPERUSER_URI) self.assertIs(user, SUPERUSER) self.assertIsNone(ptah.resolve('ptah-auth:unknown')) self.assertEqual(repr(user), '<ptah Superuser>')
def test_blob_write(self): import ptah, ptah.cms blob_uri = ptah.cms.blobStorage.add(StringIO("blob data")).__uri__ blob = ptah.resolve(blob_uri) blob.write("new data") transaction.commit() blob = ptah.resolve(blob_uri) self.assertEqual(blob.read(), "new data")
def test_blob_write(self): import ptahcms blob_uri = ptahcms.blob_storage.add( BytesIO(bytes_('blob data', 'utf-8'))).__uri__ blob = ptah.resolve(blob_uri) blob.write(bytes_('new data', 'utf-8')) transaction.commit() blob = ptah.resolve(blob_uri) self.assertEqual(blob.read(), bytes_('new data', 'utf-8'))
def test_blob_write(self): import ptah blob_uri = ptah.cms.blob_storage.add( BytesIO(bytes_('blob data','utf-8'))).__uri__ blob = ptah.resolve(blob_uri) blob.write(bytes_('new data','utf-8')) transaction.commit() blob = ptah.resolve(blob_uri) self.assertEqual(blob.read(), bytes_('new data','utf-8'))
def get_user_type(registry=None): cfg = ptah.get_settings(CFG_ID_CROWD, registry) tp = cfg['type'] if not tp.startswith('type:'): tp = 'type:{0}'.format(tp) return ptah.resolve(tp)
def __init__(self, context, request): self.tinfo = context.__type__ self.roles = [r for r in ptah.get_roles().values() if not r.system] self.local_roles = local_roles = context.__local_roles__ self.local_principals = [ptah.resolve(principalUri) for principalUri in self.local_roles] for p in list(ptah.search_principals('')): p = ptah.resolve(p.__uri__) if hasattr(p, 'properties'): if p.properties.get('roles') and p not in self.local_principals: self.local_principals.append(p) self.principals = sorted(self.local_principals, key=lambda p: str(p)) super(ShareForm, self).__init__(context, request)
def test_container_keys_with_local(self): container = self.Container(__name__='container', __path__='/container/') content = self.Content(title='Content') container['content'] = content ptah.cms.Session.add(container) ptah.cms.Session.add(content) ptah.cms.Session.flush() content_uri = content.__uri__ container_uri = container.__uri__ transaction.commit() container = ptah.resolve(container_uri) c2 = Content(title='Content2') container['content2'] = c2 self.assertEqual(container.keys(), ['content', 'content2']) self.assertEqual([c.__uri__ for c in container.values()], [content_uri, c2.__uri__]) del container['content'] self.assertEqual(container.keys(), ['content2']) self.assertEqual([c.__uri__ for c in container.values()], [c2.__uri__])
def test_uri_resolver(self): import ptah import sqlalchemy as sqla @ptah.type('mycontent', 'MyContent') class MyContentSql(ptah.get_base()): __tablename__ = 'tinfo_sql_test7' id = sqla.Column(sqla.Integer, primary_key=True) test = sqla.Column(sqla.Unicode) self.init_ptah() id = None uri = None with ptah.sa_session() as sa: item = MyContentSql(test='title') sa.add(item) sa.flush() id = item.id uri = item.__uri__ self.assertEqual(uri, 'mycontent:%s'%id) item = ptah.resolve(uri) self.assertTrue(item.id == id)
def test_container_keys_with_local(self): container = Container(__name__ = 'container', __path__ = '/container/') content = Content(title='Content') container['content'] = content ptah.cms.Session.add(container) ptah.cms.Session.add(content) ptah.cms.Session.flush() content_uri = content.__uri__ container_uri = container.__uri__ transaction.commit() container = ptah.resolve(container_uri) c2 = Content(title='Content2') container['content2'] = c2 self.assertEqual(container.keys(), ['content', 'content2']) self.assertEqual([c.__uri__ for c in container.values()], [content_uri, c2.__uri__]) del container['content'] self.assertEqual(container.keys(), ['content2']) self.assertEqual([c.__uri__ for c in container.values()], [c2.__uri__])
def get_user_type(registry=None): cfg = ptah.get_settings(CFG_ID_CROWD, registry) tp = cfg["type"] if not tp.startswith("type:"): tp = "type:{0}".format(tp) return ptah.resolve(tp)
def test_container_basics(self): container = Container(__name__="container", __path__="/container/") content = Content(title="Content") ptah.cms.Session.add(container) ptah.cms.Session.add(content) ptah.cms.Session.flush() self.assertEqual(content.__path__, "") self.assertRaises(ValueError, container.__setitem__, "content", object()) self.assertRaises(ValueError, container.__setitem__, "content", container) container["content"] = content self.assertEqual(content.__name__, "content") self.assertEqual(content.__path__, "/container/content/") self.assertEqual(content.__parent__, container) self.assertEqual(content.__parent_uri__, container.__uri__) content_uri = content.__uri__ container_uri = container.__uri__ transaction.commit() container = ptah.resolve(container_uri) self.assertTrue("content" in container) self.assertEqual(container.keys(), ["content"]) self.assertEqual(container["content"].__uri__, content_uri) self.assertEqual(container.get("content").__uri__, content_uri) self.assertEqual(container.get("unknown"), None) self.assertRaises(KeyError, container.__getitem__, "unknown") content = container["content"] self.assertRaises(KeyError, container.__setitem__, "content", content)
def test_container_keys_with_local(self): container = Container(__name__="container", __path__="/container/") content = Content(title="Content") container["content"] = content ptah.cms.Session.add(container) ptah.cms.Session.add(content) ptah.cms.Session.flush() content_uri = content.__uri__ container_uri = container.__uri__ transaction.commit() container = ptah.resolve(container_uri) c2 = Content(title="Content2") container["content2"] = c2 self.assertEqual(container.keys(), ["content", "content2"]) self.assertEqual([c.__uri__ for c in container.values()], [content_uri, c2.__uri__]) del container["content"] self.assertEqual(container.keys(), ["content2"]) self.assertEqual([c.__uri__ for c in container.values()], [c2.__uri__])
def test_uri_resolver(self): import ptah import sqlalchemy as sqla @ptah.tinfo('mycontent', 'MyContent') class MyContentSql(ptah.get_base()): __tablename__ = 'tinfo_sql_test7' id = sqla.Column(sqla.Integer, primary_key=True) test = sqla.Column(sqla.Unicode) self.init_ptah() id = None uri = None sa = ptah.get_session() item = MyContentSql(test='title') sa.add(item) sa.flush() id = item.id uri = item.__uri__ self.assertEqual(uri, 'mycontent:%s'%id) item = ptah.resolve(uri) self.assertTrue(item.id == id)
def create(self): data, errors = self.extract() if errors: self.add_error_message(errors) return # create user cfg = ptah.get_settings(ptahcrowd.CFG_ID_CROWD, self.request.registry) tp = cfg['type'] if not tp.startswith('type:'): tp = 'type:{0}'.format(tp) tinfo = ptah.resolve(tp) user = tinfo.create(fullname=data['fullname'], username=data['username'], email=data['email'], validated=data['validated'], suspended=data['suspended']) user.password = ptah.pwd_tool.encode(data['password']) tinfo.add(user) # notify system self.request.registry.notify(ptah.events.PrincipalAddedEvent(user)) self.request.add_message('User has been created.', 'success') return HTTPFound(location='.')
def create(self, tname, name=None, **params): tinfo = ptah.resolve(tname) if tinfo is None: raise NotFound('Type information is not found') tinfo.check_context(self) item = tinfo.create() if not name: name = item.__uri__ if '/' in name: raise Error("Names cannot contain '/'") if name.startswith(' '): raise Error("Names cannot starts with ' '") if name in self: raise Error("Name already in use.") self[name] = item item.update(**params) return item
def test_loadapi_load_with_parents(self): content = Content(title='Content') container = Container(__name__='container', __path__='/container/') c_uri = content.__uri__ co_uri = container.__uri__ ptah.cms.Session.add(container) ptah.cms.Session.add(content) transaction.commit() container = ptah.resolve(co_uri) container['content'] = ptah.resolve(c_uri) transaction.commit() content = ptah.cms.load(c_uri) self.assertEqual(content.__parent__.__uri__, co_uri)
def create(self): data, errors = self.extract() if errors: self.add_error_message(errors) return # create user cfg = ptah.get_settings(ptahcrowd.CFG_ID_CROWD, self.request.registry) tp = cfg['type'] if not tp.startswith('type:'): tp = 'type:{0}'.format(tp) tinfo = ptah.resolve(tp) user = tinfo.create( fullname=data['fullname'], username=data['username'], email=data['email'], validated=data['validated'], suspended=data['suspended']) user.password = ptah.pwd_tool.encode(data['password']) tinfo.add(user) # notify system self.request.registry.notify(ptah.events.PrincipalAddedEvent(user)) self.request.add_message('User has been created.', 'success') return HTTPFound(location='.')
def create(self): data, errors = self.extract() if errors: self.message(errors, "form-error") return # create user cfg = ptah.get_settings(ptahcrowd.CFG_ID_CROWD, self.request.registry) tp = cfg["type"] if not tp.startswith("type:"): tp = "type:{0}".format(tp) tinfo = ptah.resolve(tp) user = tinfo.create( name=data["name"], login=data["login"], email=data["login"], validated=data["validated"], suspended=data["suspended"], ) user.password = ptah.pwd_tool.encode(data["password"]) tinfo.add(user) # notify system self.request.registry.notify(ptah.events.PrincipalAddedEvent(user)) self.message("User has been created.", "success") return HTTPFound(location=".")
def modify_handler(self): data, errors = self.extract() if errors: return errors with ptah.sa_session() as sa: user = ptah.resolve(self.params['__uri__']) # update attrs user.name = data['name'] user.login = data['login'] user.email = data['login'] if data['password'] is not ptah.form.null: user.password = ptah.pwd_tool.encode(data['password']) # update props user.validated = data['validated'] user.suspended = data['suspended'] # add roles and groups info props = user.properties props['roles'] = data['roles'] props['groups'] = data['groups'] info = self.protocol.get_user_data(user) # done self.close('User properties has been updated.') self.protocol.broadcast('updated', info)
def test_app_factory(self): import ptah.cms self._init_ptah() factory = ptah.cms.ApplicationFactory('/test', 'root', 'Root App') self.assertTrue(factory.id == 'test') self.assertTrue(factory.path == '/test/') self.assertTrue(factory.name == 'root') self.assertTrue(factory.title == 'Root App') self.assertTrue(factory.policy is ptah.cms.ApplicationPolicy) self._setRequest(self._makeRequest()) root = factory(self.request) r_uri = root.__uri__ self.assertTrue(isinstance(root, ptah.cms.ApplicationRoot)) self.assertTrue(root.title == 'Root App') self.assertTrue(root.__name__ == 'root') self.assertTrue(root.__root_path__ == '/test/') self.assertTrue(root.__resource_url__(None, {}) == '/test/') self.assertTrue(self.request.root is root) transaction.commit() root = factory(self.request) self.assertEqual(root.__uri__, r_uri) transaction.commit() root = ptah.resolve(r_uri) self.assertEqual(root.__uri__, r_uri) factory = ptah.cms.ApplicationFactory('', 'root', 'Root App') self.assertTrue(factory.default_root)
def test_uri_resolver(self): import ptah import sqlalchemy as sqla @ptah.tinfo("mycontent", "MyContent") class MyContentSql(ptah.get_base()): __tablename__ = "tinfo_sql_test7" id = sqla.Column(sqla.Integer, primary_key=True) test = sqla.Column(sqla.Unicode) self.init_ptah() id = None uri = None sa = ptah.get_session() item = MyContentSql(test="title") sa.add(item) sa.flush() id = item.id uri = item.__uri__ self.assertEqual(uri, "mycontent:%s" % id) item = ptah.resolve(uri) self.assertTrue(item.id == id)
def __init__(self, context, request): self.tinfo = context.__type__ self.roles = [r for r in ptah.get_roles().values() if not r.system] self.local_roles = local_roles = context.__local_roles__ self.local_principals = [ ptah.resolve(principalUri) for principalUri in self.local_roles ] for p in list(ptah.search_principals('')): p = ptah.resolve(p.__uri__) if hasattr(p, 'properties'): if p.properties.get( 'roles') and p not in self.local_principals: self.local_principals.append(p) self.principals = sorted(self.local_principals, key=lambda p: str(p)) super(ShareForm, self).__init__(context, request)
def test_container_move_self_recursevly(self): container = Container(__name__="container", __path__="/container/") folder = Container(title="Folder") ptah.cms.Session.add(container) ptah.cms.Session.add(folder) ptah.cms.Session.flush() container["folder"] = folder folder_uri = folder.__uri__ container_uri = container.__uri__ transaction.commit() container = ptah.resolve(container_uri) folder = ptah.resolve(folder_uri) self.assertRaises(TypeError, folder.__setitem__, "subfolder", container)
def data(self): """ Download data. """ blob = ptah.resolve(self.blobref) if blob is None: raise cms.NotFound() return {'mimetype': blob.mimetype, 'filename': blob.filename, 'data': blob.read()}
def test_uri_registration(self): import ptah def resolver1(uri): return 'Resolved1' def resolver2(uri): return 'Resolved2' ptah.register_uri_resolver('test1', resolver1) ptah.register_uri_resolver('test2', resolver2) self._init_ptah() self.assertEqual(ptah.resolve('test1:uri'), 'Resolved1') self.assertEqual(ptah.resolve('test2:uri'), 'Resolved2') self.assertEqual(ptah.resolve(None), None) self.assertEqual(ptah.resolve('unknown'), None) self.assertEqual(ptah.resolve('unknown:uri'), None)
def thumbnailImage(context, request): blob = ptah.resolve(context.thumbnail) if blob is None: return HTTPNotFound() response = request.response response.content_type = blob.mimetype.encode('utf-8') response.body = blob.read() return response
def test_uri_registration_decorator(self): import ptah @ptah.resolver('test1') def resolver1(uri): return 'Resolved1' @ptah.resolver('test2') def resolver2(uri): return 'Resolved2' self.init_ptah() self.assertEqual(ptah.resolve('test1:uri'), 'Resolved1') self.assertEqual(ptah.resolve('test2:uri'), 'Resolved2') self.assertEqual(ptah.resolve(None), None) self.assertEqual(ptah.resolve('unknown'), None) self.assertEqual(ptah.resolve('unknown:uri'), None)
def test_uri_registration(self): import ptah def resolver1(uri): return 'Resolved1' def resolver2(uri): return 'Resolved2' ptah.register_uri_resolver('test1', resolver1) ptah.register_uri_resolver('test2', resolver2) self.init_ptah() self.assertEqual(ptah.resolve('test1:uri'), 'Resolved1') self.assertEqual(ptah.resolve('test2:uri'), 'Resolved2') self.assertEqual(ptah.resolve(None), None) self.assertEqual(ptah.resolve('unknown'), None) self.assertEqual(ptah.resolve('unknown:uri'), None)
def update(self): self.user = ptah.resolve(self.context.userid) self.manage_url = get_manage_url(self.request) mod = self.request.context while not isinstance(mod, PtahModule): mod = getattr(mod, '__parent__', None) if mod is None: # pragma: no cover break self.module = mod
def test_container_move_self_recursevly(self): container = self.Container(__name__='container', __path__='/container/') folder = self.Container(title='Folder') ptah.cms.Session.add(container) ptah.cms.Session.add(folder) ptah.cms.Session.flush() container['folder'] = folder folder_uri = folder.__uri__ container_uri = container.__uri__ transaction.commit() container = ptah.resolve(container_uri) folder = ptah.resolve(folder_uri) self.assertRaises(TypeError, folder.__setitem__, 'subfolder', container)
def tinfo(self): tname = self.request.params.get('tname', None) if not tname: raise HTTPNotFound() tinfo = ptah.resolve('type:%s' % tname) if not tinfo: raise HTTPNotFound() if not tinfo in self.context.__type__.list_types(self.context): raise HTTPForbidden() return tinfo
def test_uri_resolver_pyramid(self): import ptah def resolver1(uri): return 'Resolved-pyramid' config = testing.setUp() config.include('ptah') config.ptah_uri_resolver('test1', resolver1) config.commit() self.assertEqual(ptah.resolve('test1:uri'), 'Resolved-pyramid')
def test_blob_resolver(self): import ptah blob = ptah.cms.blob_storage.add(BytesIO(bytes_('blob data', 'utf-8'))) blob_uri = blob.__uri__ transaction.commit() blob = ptah.resolve(blob_uri) self.assertEqual(blob.__uri__, blob_uri) self.assertEqual(blob.read(), bytes_('blob data', 'utf-8'))
def logout(request): """Logout action""" uid = ptah.auth_service.get_userid() if uid is not None: ptah.auth_service.set_userid(None) request.registry.notify(ptah.events.LoggedOutEvent(ptah.resolve(uid))) request.add_message(const.LOGOUT_SUCCESSFUL, 'info') headers = security.forget(request) return HTTPFound(headers=headers, location=request.application_url) else: return HTTPFound(location=request.application_url)
def test_tinfo_type_resolver(self): import ptah @ptah.tinfo('mycontent2', 'MyContent') class MyContent(object): pass self.init_ptah() tinfo_uri = MyContent.__type__.__uri__ self.assertEqual(tinfo_uri, 'type:mycontent2') self.assertIs(ptah.resolve(tinfo_uri), MyContent.__type__)
def test_container_simple_rename_subtree(self): container = self.Container(__name__='container', __path__='/container/') folder1 = self.Container(title='Folder1') folder2 = self.Container(title='Folder2') content = self.Content(title='Content') ptah.cms.Session.add(container) ptah.cms.Session.add(folder1) ptah.cms.Session.add(folder2) ptah.cms.Session.add(content) ptah.cms.Session.flush() container['folder1'] = folder1 folder1['folder2'] = folder2 folder2['content'] = content content_uri = content.__uri__ container_uri = container.__uri__ folder1_uri = folder1.__uri__ folder2_uri = folder2.__uri__ transaction.commit() container = ptah.resolve(container_uri) container['new-folder'] = container['folder1'] transaction.commit() folder1 = ptah.resolve(folder1_uri) folder2 = ptah.resolve(folder2_uri) content = ptah.resolve(content_uri) container = ptah.resolve(container_uri) self.assertEqual(container.keys(), ['new-folder']) self.assertEqual(folder1.__path__, '/container/new-folder/') self.assertEqual(folder2.__path__, '/container/new-folder/folder2/') self.assertEqual(content.__path__, '/container/new-folder/folder2/content/')
def crowd_user_roles(context, uid, registry): """ crowd roles provider return user default roles and user group roles""" roles = set() props = getattr(ptah.resolve(uid), 'properties', None) if props is not None: roles.update(props.get('roles',())) for grp in props.get('groups',()): roles.update(ptah.get_local_roles(grp, context)) return roles
def themeFileDownload(context, request): blob = ptah.resolve(context.data) if not blob: return ' ' response = request.response response.content_type = blob.mimetype.encode('utf-8') if blob.filename: response.headerlist = { 'Content-Disposition': 'filename="%s"' % blob.filename.encode('utf-8') } response.body = blob.read() return response
def test_tinfo_type_resolver(self): import ptah, ptah.cms global MyContent class MyContent(ptah.cms.Content): __type__ = ptah.cms.Type('mycontent2', 'MyContent') self.init_ptah() tinfo_uri = MyContent.__type__.__uri__ self.assertEqual(tinfo_uri, 'cms-type:mycontent2') self.assertIs(ptah.resolve(tinfo_uri), MyContent.__type__)