def test_sqlschema_extra_fields(self): import ptah class Test2(ptah.get_base()): __tablename__ = 'test2' id = sqla.Column('id', sqla.Integer, primary_key=True) name = sqla.Column(sqla.Unicode(), info={ 'title': 'Test title', 'missing': 'missing value', 'description': 'Description', 'field_type': 'textarea', 'vocabulary': ['1', '2'] }) fieldset = ptah.generate_fieldset(Test2) field = fieldset['name'] self.assertEqual(field.title, 'Test title') self.assertEqual(field.description, 'Description') self.assertEqual(field.missing, 'missing value') self.assertEqual(field.__field__, 'textarea') self.assertEqual(field.vocabulary, ['1', '2'])
def register_type_impl( config, cls, tinfo, name, fieldset=None, fieldNames=None, **kw): # generate schema fieldset = kw.get('fieldset') if fieldset is None: kw['fieldset'] = ptah.generate_fieldset( cls, fieldNames=fieldNames, namesFilter=names_filter) log.info("Generating fieldset for %s content type.", cls) if 'global_allow' not in kw and not issubclass(cls, Content): kw['global_allow'] = False tinfo.__dict__.update(kw) tinfo.cls = cls config.get_cfg_storage(TYPES_DIR_ID)[tinfo.__uri__] = tinfo # sql query for content resolver cls.__uri_sql_get__ = ptah.QueryFreezer( lambda: ptah.get_session().query(cls) \ .filter(cls.__uri__ == sqla.sql.bindparam('uri'))) # build cms actions build_class_actions(cls)
def register_type_impl(config, cls, tinfo, name, fieldset=None, fieldNames=None, **kw): # generate schema fieldset = kw.get('fieldset') if fieldset is None: kw['fieldset'] = ptah.generate_fieldset(cls, fieldNames=fieldNames, namesFilter=names_filter) log.info("Generating fieldset for %s content type.", cls) if 'global_allow' not in kw and not issubclass(cls, Content): kw['global_allow'] = False tinfo.__dict__.update(kw) tinfo.cls = cls config.get_cfg_storage(TYPES_DIR_ID)[tinfo.__uri__] = tinfo # sql query for content resolver cls.__uri_sql_get__ = ptah.QueryFreezer( lambda: ptah.get_session().query(cls) \ .filter(cls.__uri__ == sqla.sql.bindparam('uri'))) # build cms actions build_class_actions(cls)
def test_sqlschema_extra_fields(self): import ptah class Test2(ptah.get_base()): __tablename__ = "test2" id = sqla.Column("id", sqla.Integer, primary_key=True) name = sqla.Column( sqla.Unicode(), info={ "title": "Test title", "missing": "missing value", "description": "Description", "field_type": "textarea", "vocabulary": ["1", "2"], }, ) fieldset = ptah.generate_fieldset(Test2) field = fieldset["name"] self.assertEqual(field.title, "Test title") self.assertEqual(field.description, "Description") self.assertEqual(field.missing, "missing value") self.assertEqual(field.__field__, "textarea") self.assertEqual(field.vocabulary, ["1", "2"])
def register_type_impl( config, cls, tinfo, name, fieldset, permission = ptah.NOT_ALLOWED, fieldNames=None, **kw): # generate schema if fieldset is None: fieldset = ptah.generate_fieldset( cls, fieldNames=fieldNames, namesFilter=names_filter) log.info("Generating fieldset for %s content type.", cls) if 'global_allow' not in kw and not issubclass(cls, Content): kw['global_allow'] = False tinfo.__dict__.update(kw) if fieldset is not None: tinfo.fieldset = fieldset tinfo.cls = cls tinfo.permission = permission config.storage[TYPES_DIR_ID][tinfo.__uri__] = tinfo # build cms actions build_class_actions(cls)
def test_sqlschema_fields(self): import ptah class Test(ptah.get_base()): __tablename__ = 'test' id = sqla.Column('id', sqla.Integer, primary_key=True) name = sqla.Column(sqla.Unicode()) count = sqla.Column(sqla.Integer()) score = sqla.Column(sqla.Float()) date = sqla.Column(sqla.Date()) datetime = sqla.Column(sqla.DateTime()) boolean = sqla.Column(sqla.Boolean()) fieldset = ptah.generate_fieldset(Test) # no primary keya self.assertNotIn('id', fieldset) self.assertEqual(fieldset['name'].__field__, 'text') self.assertEqual(fieldset['count'].__field__, 'int') self.assertEqual(fieldset['score'].__field__, 'float') self.assertEqual(fieldset['date'].__field__, 'date') self.assertEqual(fieldset['datetime'].__field__, 'datetime') self.assertEqual(fieldset['boolean'].__field__, 'bool') self.assertEqual(fieldset['name'].title, 'Name') fieldset = ptah.generate_fieldset(Test, fieldNames=('name', 'count')) self.assertEqual(len(fieldset), 2) self.assertIn('name', fieldset) self.assertIn('count', fieldset) fieldset = ptah.generate_fieldset(Test, fieldNames=('id', 'name'), skipPrimaryKey=False) self.assertEqual(len(fieldset), 2) self.assertIn('name', fieldset) self.assertIn('id', fieldset) self.assertTrue(fieldset['id'].readonly) # no table class TestNoTable(Test): pass fieldset = ptah.generate_fieldset(TestNoTable) self.assertEqual(len(fieldset), 6)
def test_sqlschema_fields(self): import ptah class Test(ptah.get_base()): __tablename__ = 'test' id = sqla.Column('id', sqla.Integer, primary_key=True) name = sqla.Column(sqla.Unicode()) count = sqla.Column(sqla.Integer()) score = sqla.Column(sqla.Float()) date = sqla.Column(sqla.Date()) datetime = sqla.Column(sqla.DateTime()) boolean = sqla.Column(sqla.Boolean()) fieldset = ptah.generate_fieldset(Test) # no primary keya self.assertNotIn('id', fieldset) self.assertEqual(fieldset['name'].__field__, 'text') self.assertEqual(fieldset['count'].__field__, 'int') self.assertEqual(fieldset['score'].__field__, 'float') self.assertEqual(fieldset['date'].__field__, 'date') self.assertEqual(fieldset['datetime'].__field__, 'datetime') self.assertEqual(fieldset['boolean'].__field__, 'bool') self.assertEqual(fieldset['name'].title, 'Name') fieldset = ptah.generate_fieldset(Test, fieldNames=('name', 'count')) self.assertEqual(len(fieldset), 2) self.assertIn('name', fieldset) self.assertIn('count', fieldset) fieldset = ptah.generate_fieldset( Test, fieldNames=('id', 'name'), skipPrimaryKey=False) self.assertEqual(len(fieldset), 2) self.assertIn('name', fieldset) self.assertIn('id', fieldset) self.assertTrue(fieldset['id'].readonly) # no table class TestNoTable(Test): pass fieldset = ptah.generate_fieldset(TestNoTable) self.assertEqual(len(fieldset), 6)
def test_sqlschema_fields(self): import ptah class Test(ptah.get_base()): __tablename__ = "test" id = sqla.Column("id", sqla.Integer, primary_key=True) name = sqla.Column(sqla.Unicode()) count = sqla.Column(sqla.Integer()) score = sqla.Column(sqla.Float()) date = sqla.Column(sqla.Date()) datetime = sqla.Column(sqla.DateTime()) boolean = sqla.Column(sqla.Boolean()) fieldset = ptah.generate_fieldset(Test) # no primary keya self.assertNotIn("id", fieldset) self.assertEqual(fieldset["name"].__field__, "text") self.assertEqual(fieldset["count"].__field__, "int") self.assertEqual(fieldset["score"].__field__, "float") self.assertEqual(fieldset["date"].__field__, "date") self.assertEqual(fieldset["datetime"].__field__, "datetime") self.assertEqual(fieldset["boolean"].__field__, "bool") self.assertEqual(fieldset["name"].title, "Name") fieldset = ptah.generate_fieldset(Test, fieldNames=("name", "count")) self.assertEqual(len(fieldset), 2) self.assertIn("name", fieldset) self.assertIn("count", fieldset) fieldset = ptah.generate_fieldset(Test, fieldNames=("id", "name"), skipPrimaryKey=False) self.assertEqual(len(fieldset), 2) self.assertIn("name", fieldset) self.assertIn("id", fieldset) self.assertTrue(fieldset["id"].readonly) # no table class TestNoTable(Test): pass fieldset = ptah.generate_fieldset(TestNoTable) self.assertEqual(len(fieldset), 6)
def test_sqlschema_skip(self): import ptah class Test34(ptah.get_base()): __tablename__ = 'test34' id = sqla.Column('id', sqla.Integer, primary_key=True) name = sqla.Column(sqla.Unicode(), info={'skip': True}) fieldset = ptah.generate_fieldset(Test34) self.assertNotIn('name', fieldset)
def test_sqlschema_custom_factory(self): import ptah class Test32(ptah.get_base()): __tablename__ = "test32" id = sqla.Column("id", sqla.Integer, primary_key=True) name = sqla.Column(sqla.Unicode(), info={"field_type": ptah.form.IntegerField}) fieldset = ptah.generate_fieldset(Test32) m_field = fieldset["name"] self.assertIsInstance(m_field, ptah.form.IntegerField)
def test_sqlschema_custom_factory(self): import ptah class Test32(ptah.get_base()): __tablename__ = 'test32' id = sqla.Column('id', sqla.Integer, primary_key=True) name = sqla.Column(sqla.Unicode(), info={'field_type': form.IntegerField}) fieldset = ptah.generate_fieldset(Test32) m_field = fieldset['name'] self.assertIsInstance(m_field, form.IntegerField)
def test_sqlschema_custom_type(self): import ptah class Test31(ptah.get_base()): __tablename__ = 'test31' id = sqla.Column('id', sqla.Integer, primary_key=True) name = sqla.Column(sqla.Unicode(), info={'field_type': 'int'}) fieldset = ptah.generate_fieldset(Test31) m_field = fieldset['name'] self.assertEqual(m_field.__field__, 'int')
def test_sqlschema_unknown(self): import ptah class Test2(ptah.get_base()): __tablename__ = 'test5' id = sqla.Column('id', sqla.Integer, primary_key=True) name = sqla.Column(sqla.Unicode()) json = sqla.Column(ptah.JsonListType()) fieldset = ptah.generate_fieldset(Test2) self.assertNotIn('json', fieldset)
def test_sqlschema_custom_factory(self): import ptah class Test32(ptah.get_base()): __tablename__ = 'test32' id = sqla.Column('id', sqla.Integer, primary_key=True) name = sqla.Column(sqla.Unicode(), info={'field_type': ptah.form.IntegerField}) fieldset = ptah.generate_fieldset(Test32) m_field = fieldset['name'] self.assertIsInstance(m_field, ptah.form.IntegerField)
def test_sqlschema_custom_type(self): import ptah class Test31(ptah.get_base()): __tablename__ = "test31" id = sqla.Column("id", sqla.Integer, primary_key=True) name = sqla.Column(sqla.Unicode(), info={"field_type": "int"}) fieldset = ptah.generate_fieldset(Test31) m_field = fieldset["name"] self.assertEqual(m_field.__field__, "int")
def test_sqlschema_custom(self): import ptah field = ptah.form.TextField('name', title='Custom') class Test3(ptah.get_base()): __tablename__ = 'test3' id = sqla.Column('id', sqla.Integer, primary_key=True) name = sqla.Column(sqla.Unicode(), info={'field': field}) fieldset = ptah.generate_fieldset(Test3) m_field = fieldset['name'] self.assertEqual(m_field.name, 'name') self.assertEqual(m_field.title, 'Custom') self.assertIs(m_field, field)
def test_sqlschema_custom(self): import ptah field = ptah.form.TextField("name", title="Custom") class Test3(ptah.get_base()): __tablename__ = "test3" id = sqla.Column("id", sqla.Integer, primary_key=True) name = sqla.Column(sqla.Unicode(), info={"field": field}) fieldset = ptah.generate_fieldset(Test3) m_field = fieldset["name"] self.assertEqual(m_field.name, "name") self.assertEqual(m_field.title, "Custom") self.assertIs(m_field, field)
def test_sqlschema_custom(self): import ptah field = form.TextField('name', title = 'Custom') class Test3(ptah.get_base()): __tablename__ = 'test3' id = sqla.Column('id', sqla.Integer, primary_key=True) name = sqla.Column(sqla.Unicode(), info={'field': field}) fieldset = ptah.generate_fieldset(Test3) m_field = fieldset['name'] self.assertEqual(m_field.name, 'name') self.assertEqual(m_field.title, 'Custom') self.assertIs(m_field, field)
def register_sqla_type(config, cls, tinfo, name, **kw): base = ptah.get_base() if not issubclass(cls, base): return # generate schema fieldset = tinfo.fieldset if fieldset is None: fieldset = ptah.generate_fieldset(cls, fieldNames=kw.get('fieldNames'), namesFilter=kw.get('namesFilter')) log.info("Generating fieldset for %s content type.", cls) if fieldset is not None: tinfo.fieldset = fieldset if tinfo.add_method is None: tinfo.add_method = sqla_add_method # install __uri__ property if not hasattr(cls, '__uri__') or hasattr(cls, '__uri_reinstall__'): pname = None for cl in cls.__table__.columns: if cl.primary_key: pname = cl.name break l = len(tinfo.name) + 1 cls.__uri_reinstall__ = True cls.__uri__ = UriProperty(tinfo.name, cl.name) cls.__uri_sql_get__ = ptah.QueryFreezer( lambda: ptah.get_session().query(cls) \ .filter(getattr(cls, pname) == sqla.sql.bindparam('uri'))) def resolver(uri): """Content resolver for %s type'""" % tinfo.name return cls.__uri_sql_get__.first(uri=uri[l:]) storage = config.get_cfg_storage(ID_RESOLVER) if tinfo.name in storage: raise ConfigurationError('Resolver for "%s" already registered' % tinfo.name) storage[tinfo.name] = resolver
def register_sqla_type(config, cls, tinfo, name, **kw): base = ptah.get_base() if not issubclass(cls, base): return # generate schema fieldset = tinfo.fieldset if fieldset is None: fieldset = ptah.generate_fieldset( cls, fieldNames=kw.get('fieldNames'), namesFilter=kw.get('namesFilter')) log.info("Generating fieldset for %s content type.", cls) if fieldset is not None: tinfo.fieldset = fieldset if tinfo.add_method is None: tinfo.add_method = sqla_add_method # install __uri__ property if not hasattr(cls, '__uri__') or hasattr(cls, '__uri_reinstall__'): pname = None for cl in cls.__table__.columns: if cl.primary_key: pname = cl.name break l = len(tinfo.name)+1 cls.__uri_reinstall__ = True cls.__uri__ = UriProperty(tinfo.name, cl.name) cls.__uri_sql_get__ = ptah.QueryFreezer( lambda: ptah.get_session().query(cls) \ .filter(getattr(cls, pname) == sqla.sql.bindparam('uri'))) def resolver(uri): """Content resolver for %s type'"""%tinfo.name return cls.__uri_sql_get__.first(uri=uri[l:]) storage = config.get_cfg_storage(ID_RESOLVER) if tinfo.name in storage: raise ConfigurationError( 'Resolver for "%s" already registered'%tinfo.name) storage[tinfo.name] = resolver
def test_sqlschema_extra_fields(self): import ptah class Test2(ptah.get_base()): __tablename__ = 'test2' id = sqla.Column('id', sqla.Integer, primary_key=True) name = sqla.Column( sqla.Unicode(), info={'title': 'Test title', 'missing': 'missing value', 'description': 'Description', 'field_type': 'textarea', 'vocabulary': ['1','2']}) fieldset = ptah.generate_fieldset(Test2) field = fieldset['name'] self.assertEqual(field.title, 'Test title') self.assertEqual(field.description, 'Description') self.assertEqual(field.missing, 'missing value') self.assertEqual(field.__field__, 'textarea') self.assertEqual(field.vocabulary, ['1', '2'])