Exemple #1
0
    def __init__(self, cls_name, cls_bases, cls_dict):
        type_info = cls_dict['_type_info']
        for k, v in type_info.items():

            if issubclass(v, SelfReference):
                type_info[k] = self
            if issubclass(v, XmlAttribute):
                a_of = v.attribute_of
                if a_of is not None:
                    type_info.attributes[k] = type_info[a_of]

        tn = self.Attributes.table_name
        meta = self.Attributes.sqla_metadata
        t = self.Attributes.sqla_table

        # for spyne objects reflecting an existing db table
        if tn is None:
            if t is not None and len(self._type_info) == 0:
                self.Attributes.sqla_metadata = t.metadata
                from spyne.util.sqlalchemy import gen_spyne_info

                gen_spyne_info(self)

        # for spyne objects being converted to a sqlalchemy table
        elif meta is not None and (tn is not None or t is not None) and \
                                                       len(self._type_info) > 0:
            from spyne.util.sqlalchemy import gen_sqla_info

            gen_sqla_info(self, cls_bases)

        type(ModelBase).__init__(self, cls_name, cls_bases, cls_dict)
Exemple #2
0
    def test_nested_sql_array_as_json(self):
        engine = create_engine('sqlite:///:memory:')
        session = sessionmaker(bind=engine)()
        metadata = NewTableModel.Attributes.sqla_metadata = MetaData()
        metadata.bind = engine

        class SomeOtherClass(ComplexModel):
            id = Integer32
            s = Unicode(64)

        class SomeClass(NewTableModel):
            __tablename__ = 'some_class'
            __table_args__ = {"sqlite_autoincrement": True}

            id = Integer32(primary_key=True)
            others = Array(SomeOtherClass, store_as='json')

        gen_sqla_info(SomeClass)

        metadata.create_all()

        soc1 = SomeOtherClass(s='ehe1')
        soc2 = SomeOtherClass(s='ehe2')
        sc = SomeClass(others=[soc1, soc2])

        session.add(sc)
        session.commit()
        session.close()

        sc_db = session.query(SomeClass).get(1)

        assert sc_db.others[0].s == 'ehe1'
        assert sc_db.others[1].s == 'ehe2'

        session.close()
Exemple #3
0
    def test_nested_sql_array_as_json(self):
        engine = create_engine('sqlite:///:memory:')
        session = sessionmaker(bind=engine)()
        metadata = NewTableModel.Attributes.sqla_metadata = MetaData()
        metadata.bind = engine

        class SomeOtherClass(ComplexModel):
            id = Integer32
            s = Unicode(64)

        class SomeClass(NewTableModel):
            __tablename__ = 'some_class'
            __table_args__ = {"sqlite_autoincrement": True}

            id = Integer32(primary_key=True)
            others = Array(SomeOtherClass, store_as='json')

        gen_sqla_info(SomeClass)

        metadata.create_all()

        soc1 = SomeOtherClass(s='ehe1')
        soc2 = SomeOtherClass(s='ehe2')
        sc = SomeClass(others=[soc1, soc2])

        session.add(sc)
        session.commit()
        session.close()

        sc_db = session.query(SomeClass).get(1)

        assert sc_db.others[0].s == 'ehe1'
        assert sc_db.others[1].s == 'ehe2'

        session.close()
Exemple #4
0
    def __init__(self, cls_name, cls_bases, cls_dict):
        type_info = cls_dict['_type_info']
        for k,v in type_info.items():

            if issubclass(v, SelfReference):
                type_info[k] = self
            if issubclass(v, XmlAttribute):
                a_of = v.attribute_of
                if a_of is not None:
                    type_info.attributes[k] = type_info[a_of]

        tn = self.Attributes.table_name
        meta = self.Attributes.sqla_metadata
        t = self.Attributes.sqla_table

        # for spyne objects reflecting an existing db table
        if tn is None:
            if t is not None and len(self._type_info) == 0:
                self.Attributes.sqla_metadata = t.metadata
                from spyne.util.sqlalchemy import gen_spyne_info

                gen_spyne_info(self)

        # for spyne objects being converted to a sqlalchemy table
        elif meta is not None and (tn is not None or t is not None) and \
                                                       len(self._type_info) > 0:
            from spyne.util.sqlalchemy import gen_sqla_info

            gen_sqla_info(self, cls_bases)

        type(ModelBase).__init__(self, cls_name, cls_bases, cls_dict)
Exemple #5
0
    def __init__(self, cls_name, cls_bases, cls_dict):
        type_info = self._type_info

        for k,v in type_info.items():
            if issubclass(v, SelfReference):
                type_info[k] = self

            elif issubclass(v, XmlData):
                self.Attributes._xml_tag_body_as = k, v

            elif issubclass(v, XmlAttribute):
                a_of = v.attribute_of
                if a_of is not None:
                    type_info.attributes[k] = type_info[a_of]

            elif issubclass(v, Array):
                v2, = v._type_info.values()
                while issubclass(v2, Array):
                    v = v2
                    v2, = v2._type_info.values()

                if issubclass(v2, SelfReference):
                    v._set_serializer(self)

        tn = self.Attributes.table_name
        meta = self.Attributes.sqla_metadata
        t = self.Attributes.sqla_table

        methods = self.Attributes.methods
        if methods is not None:
            methods.sanitize(self)

        # For spyne objects reflecting an existing db table
        if tn is None:
            if t is not None:
                self.Attributes.sqla_metadata = t.metadata
                from spyne.util.sqlalchemy import gen_spyne_info

                gen_spyne_info(self)

        # For spyne objects being converted to a sqlalchemy table
        elif meta is not None and (tn is not None or t is not None) and \
                                                       len(self._type_info) > 0:
            from spyne.util.sqlalchemy import gen_sqla_info

            gen_sqla_info(self, cls_bases)

        super(ComplexModelMeta, self).__init__(cls_name, cls_bases, cls_dict)
Exemple #6
0
    def test_nested_sql(self):
        engine = create_engine('sqlite:///:memory:')
        session = sessionmaker(bind=engine)()
        metadata = NewTableModel.Attributes.sqla_metadata = MetaData()
        metadata.bind = engine

        class SomeOtherClass(NewTableModel):
            __tablename__ = 'some_other_class'
            __table_args__ = {"sqlite_autoincrement": True}

            id = Integer32(primary_key=True)
            s = Unicode(64)

        class SomeClass(NewTableModel):
            __tablename__ = 'some_class'
            __table_args__ = (
                {"sqlite_autoincrement": True},
            )

            id = Integer32(primary_key=True)
            o = SomeOtherClass.customize(store_as='table')

        gen_sqla_info(SomeOtherClass)
        gen_sqla_info(SomeClass)

        metadata.create_all()

        soc = SomeOtherClass(s='ehe')
        sc = SomeClass(o=soc)

        session.add(sc)
        session.commit()
        session.close()

        sc_db = session.query(SomeClass).get(1)
        print sc_db
        assert sc_db.o.s == 'ehe'
        assert sc_db.o_id == 1

        sc_db.o = None
        session.commit()
        session.close()

        sc_db = session.query(SomeClass).get(1)
        assert sc_db.o == None
        assert sc_db.o_id == None
Exemple #7
0
    def __init__(self, cls_name, cls_bases, cls_dict):
        type_info = cls_dict['_type_info']
        for k in type_info:
            if issubclass(type_info[k], SelfReference):
                type_info[k] = self

        if self.Attributes.table_name is None:
            if self.Attributes.sqla_table is not None and len(self._type_info) == 0:
                from spyne.util.sqlalchemy import gen_spyne_info

                gen_spyne_info(self)

        elif self.Attributes.sqla_metadata is not None and len(self._type_info) > 0:
            from spyne.util.sqlalchemy import gen_sqla_info

            gen_sqla_info(self, cls_bases)

        type(ModelBase).__init__(self, cls_name, cls_bases, cls_dict)
Exemple #8
0
    def __init__(self, cls_name, cls_bases, cls_dict):
        type_info = cls_dict['_type_info']
        for k in type_info:
            if issubclass(type_info[k], SelfReference):
                type_info[k] = self

        if self.Attributes.table_name is None:
            if self.Attributes.sqla_table is not None and len(self._type_info) == 0:
                from spyne.util.sqlalchemy import gen_spyne_info

                gen_spyne_info(self)

        elif self.Attributes.sqla_metadata is not None and len(self._type_info) > 0:
            from spyne.util.sqlalchemy import gen_sqla_info

            gen_sqla_info(self, cls_bases)

        type(ModelBase).__init__(self, cls_name, cls_bases, cls_dict)
Exemple #9
0
    def test_nested_sql(self):
        engine = create_engine('sqlite:///:memory:')
        session = sessionmaker(bind=engine)()
        metadata = NewTableModel.Attributes.sqla_metadata = MetaData()
        metadata.bind = engine

        class SomeOtherClass(NewTableModel):
            __tablename__ = 'some_other_class'
            __table_args__ = {"sqlite_autoincrement": True}

            id = Integer32(primary_key=True)
            s = Unicode(64)

        class SomeClass(NewTableModel):
            __tablename__ = 'some_class'
            __table_args__ = ({"sqlite_autoincrement": True}, )

            id = Integer32(primary_key=True)
            o = SomeOtherClass.customize(store_as='table')

        gen_sqla_info(SomeOtherClass)
        gen_sqla_info(SomeClass)

        metadata.create_all()

        soc = SomeOtherClass(s='ehe')
        sc = SomeClass(o=soc)

        session.add(sc)
        session.commit()
        session.close()

        sc_db = session.query(SomeClass).get(1)
        print sc_db
        assert sc_db.o.s == 'ehe'
        assert sc_db.o_id == 1

        sc_db.o = None
        session.commit()
        session.close()

        sc_db = session.query(SomeClass).get(1)
        assert sc_db.o == None
        assert sc_db.o_id == None
Exemple #10
0
    def test_table(self):
        class SomeClass(ComplexModel):
            __metadata__ = MetaData()
            __tablename__ = 'some_class'

            i = Integer(primary_key=True)

        t = gen_sqla_info(SomeClass)

        assert t.c['i'].type.__class__ is sqlalchemy.DECIMAL
Exemple #11
0
    def test_table(self):
        class SomeClass(ComplexModel):
            __metadata__ = MetaData()
            __tablename__ = 'some_class'

            i = Integer(primary_key=True)

        t = gen_sqla_info(SomeClass)

        assert t.c['i'].type.__class__ is sqlalchemy.DECIMAL
Exemple #12
0
    def test_nested_sql_array_as_xml_no_ns(self):
        engine = create_engine('sqlite:///:memory:')
        session = sessionmaker(bind=engine)()
        metadata = NewTableModel.Attributes.sqla_metadata = MetaData()
        metadata.bind = engine

        class SomeOtherClass(ComplexModel):
            id = Integer32
            s = Unicode(64)

        class SomeClass(NewTableModel):
            __tablename__ = 'some_class'
            __table_args__ = {"sqlite_autoincrement": True}

            id = Integer32(primary_key=True)
            others = Array(SomeOtherClass, store_as=xml(no_ns=True))

        gen_sqla_info(SomeClass)

        metadata.create_all()

        soc1 = SomeOtherClass(s='ehe1')
        soc2 = SomeOtherClass(s='ehe2')
        sc = SomeClass(others=[soc1, soc2])

        session.add(sc)
        session.commit()
        session.close()

        sc_xml = session.connection().execute("select others from some_class") \
                                                               .fetchall()[0][0]

        from lxml import etree
        assert etree.fromstring(sc_xml).tag == 'SomeOtherClassArray'

        session.close()
Exemple #13
0
    def test_nested_sql_array_as_xml_no_ns(self):
        engine = create_engine('sqlite:///:memory:')
        session = sessionmaker(bind=engine)()
        metadata = NewTableModel.Attributes.sqla_metadata = MetaData()
        metadata.bind = engine

        class SomeOtherClass(ComplexModel):
            id = Integer32
            s = Unicode(64)

        class SomeClass(NewTableModel):
            __tablename__ = 'some_class'
            __table_args__ = {"sqlite_autoincrement": True}

            id = Integer32(primary_key=True)
            others = Array(SomeOtherClass, store_as=xml(no_ns=True))

        gen_sqla_info(SomeClass)

        metadata.create_all()

        soc1 = SomeOtherClass(s='ehe1')
        soc2 = SomeOtherClass(s='ehe2')
        sc = SomeClass(others=[soc1, soc2])

        session.add(sc)
        session.commit()
        session.close()

        sc_xml = session.connection().execute("select others from some_class") \
                                                               .fetchall()[0][0]

        from lxml import etree
        assert etree.fromstring(sc_xml).tag == 'SomeOtherClassArray'

        session.close()
Exemple #14
0
    def test_table_args(self):
        class SomeClass(ComplexModel):
            __metadata__ = MetaData()
            __tablename__ = 'some_class'
            __table_args__ = (UniqueConstraint('j'), )

            i = Integer(primary_key=True)
            j = Unicode(64)

        t = gen_sqla_info(SomeClass)

        assert isinstance(t.c['j'].type, sqlalchemy.Unicode)

        for c in t.constraints:
            if isinstance(c, UniqueConstraint):
                assert list(c.columns) == [t.c.j]
                break
        else:
            raise Exception("UniqueConstraint is missing.")
Exemple #15
0
    def test_table_args(self):
        class SomeClass(ComplexModel):
            __metadata__ = MetaData()
            __tablename__ = 'some_class'
            __table_args__ = (
                UniqueConstraint('j'),
            )

            i = Integer(primary_key=True)
            j = Unicode(64)

        t = gen_sqla_info(SomeClass)

        assert isinstance(t.c['j'].type, sqlalchemy.Unicode)

        for c in t.constraints:
            if isinstance(c, UniqueConstraint):
                assert list(c.columns) == [t.c.j]
                break
        else:
            raise Exception("UniqueConstraint is missing.")
Exemple #16
0
    def __init__(self, cls_name, cls_bases, cls_dict):
        type_info = self._type_info

        extends = self.__extends__
        if extends is not None and self.__orig__ is None:
            eattr = extends.Attributes;
            if eattr._subclasses is None:
                eattr._subclasses = []
            eattr._subclasses.append(self)
            if self.Attributes._subclasses is eattr._subclasses:
                self.Attributes._subclasses = None

        for k, v in type_info.items():
            if issubclass(v, SelfReference):
                self.replace_field(k, self.customize(*v.customize_args,
                                                          **v.customize_kwargs))

            elif issubclass(v, XmlData):
                self.Attributes._xml_tag_body_as = k, v

            elif issubclass(v, XmlAttribute):
                a_of = v.attribute_of
                if a_of is not None:
                    type_info.attributes[k] = type_info[a_of]

            elif issubclass(v, Array):
                v2, = v._type_info.values()
                while issubclass(v2, Array):
                    v = v2
                    v2, = v2._type_info.values()

                if issubclass(v2, SelfReference):
                    v._set_serializer(self)

        # FIXME: Implement this better
        new_type_info = []
        for k, v in self._type_info.items():
            if v.Attributes.order == None:
                new_type_info.append(k)

        for k, v in self._type_info.items():
            if v.Attributes.order is not None:
                new_type_info.insert(v.Attributes.order, k)

        assert len(self._type_info) == len(new_type_info)
        self._type_info.keys()[:] = new_type_info

        tn = self.Attributes.table_name
        meta = self.Attributes.sqla_metadata
        t = self.Attributes.sqla_table

        methods = _gen_methods(self, cls_dict)
        if len(methods) > 0:
            self.Attributes.methods = methods

        methods = self.Attributes.methods
        if methods is not None:
            assert isinstance(methods, _MethodsDict)
            methods.sanitize(self)

        # For spyne objects reflecting an existing db table
        if tn is None:
            if t is not None:
                self.Attributes.sqla_metadata = t.metadata
                from spyne.util.sqlalchemy import gen_spyne_info

                gen_spyne_info(self)

        # For spyne objects being converted to a sqlalchemy table
        elif meta is not None and (tn is not None or t is not None) and \
                                                       len(self._type_info) > 0:
            from spyne.util.sqlalchemy import gen_sqla_info

            gen_sqla_info(self, cls_bases)

        super(ComplexModelMeta, self).__init__(cls_name, cls_bases, cls_dict)
Exemple #17
0
    def __init__(self, cls_name, cls_bases, cls_dict):
        type_info = self._type_info

        extends = self.__extends__
        if extends is not None and self.__orig__ is None:
            eattr = extends.Attributes;
            if eattr._subclasses is None:
                eattr._subclasses = []
            eattr._subclasses.append(self)
            if self.Attributes._subclasses is eattr._subclasses:
                self.Attributes._subclasses = None

        for k,v in type_info.items():
            if issubclass(v, SelfReference):
                type_info[k] = self

            elif issubclass(v, XmlData):
                self.Attributes._xml_tag_body_as = k, v

            elif issubclass(v, XmlAttribute):
                a_of = v.attribute_of
                if a_of is not None:
                    type_info.attributes[k] = type_info[a_of]

            elif issubclass(v, Array):
                v2, = v._type_info.values()
                while issubclass(v2, Array):
                    v = v2
                    v2, = v2._type_info.values()

                if issubclass(v2, SelfReference):
                    v._set_serializer(self)

        # FIXME: Implement this better
        new_type_info = []
        for k, v in self._type_info.items():
            if v.Attributes.order == None:
                new_type_info.append(k)

        for k, v in self._type_info.items():
            if v.Attributes.order is not None:
                new_type_info.insert(v.Attributes.order, k)

        assert len(self._type_info) == len(new_type_info)
        self._type_info.keys()[:] = new_type_info

        tn = self.Attributes.table_name
        meta = self.Attributes.sqla_metadata
        t = self.Attributes.sqla_table

        methods = self.Attributes.methods
        if methods is not None:
            assert isinstance(methods, _MethodsDict)
            methods.sanitize(self)

        # For spyne objects reflecting an existing db table
        if tn is None:
            if t is not None:
                self.Attributes.sqla_metadata = t.metadata
                from spyne.util.sqlalchemy import gen_spyne_info

                gen_spyne_info(self)

        # For spyne objects being converted to a sqlalchemy table
        elif meta is not None and (tn is not None or t is not None) and \
                                                       len(self._type_info) > 0:
            from spyne.util.sqlalchemy import gen_sqla_info

            gen_sqla_info(self, cls_bases)

        super(ComplexModelMeta, self).__init__(cls_name, cls_bases, cls_dict)