def Type(name, title=None, fieldset=None, **kw): """ Declare new type. This function has to be called within a content class declaration. .. code-block:: python class MyContent(ptah.cms.Content): __type__ = Type('My content') """ info = config.DirectiveInfo(allowed_scope=('class',)) fs = ContentSchema if fieldset is None else fieldset if title is None: title = name.capitalize() typeinfo = TypeInformation(None, name, title, fs, **kw) f_locals = sys._getframe(1).f_locals if '__mapper_args__' not in f_locals: f_locals['__mapper_args__'] = {'polymorphic_identity': typeinfo.__uri__} if '__id__' not in f_locals and '__tablename__' in f_locals: f_locals['__id__'] = sqla.Column( 'id', sqla.Integer, sqla.ForeignKey('ptah_content.id'), primary_key=True) if '__uri_factory__' not in f_locals: f_locals['__uri_factory__'] = ptah.UriFactory('cms-%s'%name) def resolve_content(uri): return typeinfo.cls.__uri_sql_get__.first(uri=uri) resolve_content.__doc__ = 'CMS Content resolver for %s type'%title ptah.register_uri_resolver('cms-%s'%name, resolve_content, depth=2) # config actino and introspection info discr = (TYPES_DIR_ID, name) intr = config.Introspectable(TYPES_DIR_ID, discr, name, TYPES_DIR_ID) intr['name'] = name intr['type'] = typeinfo intr['codeinfo'] = info.codeinfo info.attach( config.ClassAction( register_type_impl, (typeinfo, name, fieldset), kw, discriminator=discr, introspectables=(intr,)) ) return typeinfo
def test_auth_principal(self): import ptah principal = Principal('1', 'user', 'user') def resolver(uri): if uri == 'test:1': return principal ptah.register_uri_resolver('test', resolver) self.init_ptah() self.assertEqual(ptah.auth_service.get_current_principal(), None) ptah.auth_service.set_userid('test:1') self.assertEqual(ptah.auth_service.get_current_principal(), principal)
def test_auth_principal(self): import ptah principal = Principal("1", "user", "user") def resolver(uri): if uri == "test:1": return principal ptah.register_uri_resolver("test", resolver) self._init_ptah() self.assertEqual(ptah.authService.get_current_principal(), None) ptah.authService.set_userid("test:1") self.assertEqual(ptah.authService.get_current_principal(), principal)
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 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 test_uri_registration_conflicts(self): import ptah ptah.register_uri_resolver('test', None) ptah.register_uri_resolver('test', None) self.assertRaises(ConfigurationConflictError, self.init_ptah)
def add(self, data, parent=None, **metadata): blob = self.create(parent) data.seek(0) blob.data = data.read() blob.updateMetadata(**metadata) data.seek(0, os.SEEK_END) blob.size = data.tell() return blob def get(self, uri): """SQL Blob storage resolver""" return self._sql_get.first(uri=uri) def getByParent(self, parent): return self._sql_get_by_parent.first(parent=parent) def replace(self, uri, data, **metadata): # pragma: no cover pass def remove(self, uri): # pragma: no cover pass blobStorage = BlobStorage() ptah.register_uri_resolver("blob+sql", blobStorage.get)
def authenticate(self, creds): login, password = creds['login'], creds['password'] user = User.get(login) if user is not None: if ptah.passwordTool.check(user.password,password): return user def get_principal(self, uri): return User.getByURI(uri) def get_principal_bylogin(self, login): return User.get(login) _sql_search = ptah.QueryFreezer( lambda: Session.query(User) \ .filter(User.email.contains(sqla.sql.bindparam('term'))) .order_by(sqla.sql.asc('email'))) def search(self, term): for user in self._sql_search.all(term = '%%%s%%'%term): yield user provider = PloudProvider() ptah.register_uri_resolver('user-ploud', provider.get_principal) ptah.register_auth_provider('ploud', provider) ptah.register_principal_searcher('ploud', provider.search)
def add(self, data, parent=None, **metadata): blob = self.create(parent) data.seek(0) blob.data = data.read() blob.updateMetadata(**metadata) data.seek(0, os.SEEK_END) blob.size = data.tell() return blob def get(self, uri): """SQL Blob resolver""" return self._sql_get.first(uri=uri) def getByParent(self, parent): return self._sql_get_by_parent.first(parent=parent) def replace(self, uri, data, **metadata): # pragma: no cover pass def remove(self, uri): # pragma: no cover pass blobStorage = BlobStorage() ptah.register_uri_resolver('blob-sql', blobStorage.get)
def add(self, data, parent=None, **metadata): blob = self.create(parent) data.seek(0) blob.data = data.read() blob.updateMetadata(**metadata) data.seek(0, os.SEEK_END) blob.size = data.tell() return blob def get(self, uri): """SQL Blob resolver""" return self._sql_get.first(uri=uri) def getByParent(self, parent): return self._sql_get_by_parent.first(parent=parent) def replace(self, uri, data, **metadata): # pragma: no cover pass def remove(self, uri): # pragma: no cover pass blob_storage = BlobStorage() ptah.register_uri_resolver('blob-sql', blob_storage.get)