Пример #1
0
    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'])
Пример #2
0
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)
Пример #3
0
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)
Пример #4
0
    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"])
Пример #5
0
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)
Пример #6
0
    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)
Пример #7
0
    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)
Пример #8
0
    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)
Пример #9
0
    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)
Пример #10
0
    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)
Пример #11
0
    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)
Пример #12
0
    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)
Пример #13
0
    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')
Пример #14
0
    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)
Пример #15
0
    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')
Пример #16
0
    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)
Пример #17
0
    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)
Пример #18
0
    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")
Пример #19
0
    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)
Пример #20
0
    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)
Пример #21
0
    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)
Пример #22
0
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
Пример #23
0
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
Пример #24
0
    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'])