Exemplo n.º 1
0
    def _create_layer(self, public=False, none_area=False, attr_list=False,
                      exclude_properties=False):
        """ This function is central for this test class. It creates
        a layer with two features, and associates a restriction area
        to it. """
        import transaction
        import sqlahelper
        from sqlalchemy import func
        from sqlalchemy import Column, Table, types, ForeignKey
        from sqlalchemy.ext.declarative import declarative_base
        from geoalchemy import (GeometryDDL, GeometryExtensionColumn,
                                Point, WKTSpatialElement)
        from c2cgeoportal.models import DBSession, Layer, RestrictionArea

        self.__class__._table_index = self.__class__._table_index + 1
        id = self.__class__._table_index

        engine = sqlahelper.get_engine()

        if not self.metadata:
            self.metadata = declarative_base(bind=engine).metadata

        tablename = "table_%d" % id

        table = Table('%s_child' % tablename, self.metadata,
                      Column('id', types.Integer, primary_key=True),
                      Column('name', types.Unicode),
                      schema='public'
                      )
        table.create()

        ins = table.insert().values(name=u'c1é')
        c1_id = engine.connect().execute(ins).inserted_primary_key[0]
        ins = table.insert().values(name=u'c2é')
        c2_id = engine.connect().execute(ins).inserted_primary_key[0]

        table = Table(
            tablename, self.metadata,
            Column('id', types.Integer, primary_key=True),
            Column('child_id', types.Integer,
                   ForeignKey('public.%s_child.id' % tablename)),
            Column('name', types.Unicode),
            GeometryExtensionColumn('geom', Point(srid=21781)),
            schema='public'
        )
        GeometryDDL(table)
        table.create()

        ins = table.insert().values(
            child_id=c1_id,
            name='foo',
            geom=func.ST_GeomFromText('POINT(5 45)', 21781)
        )
        engine.connect().execute(ins).inserted_primary_key[0]
        ins = table.insert().values(
            child_id=c2_id,
            name='bar',
            geom=func.ST_GeomFromText('POINT(6 46)', 21781)
        )
        engine.connect().execute(ins).inserted_primary_key[0]
        if attr_list:
            ins = table.insert().values(
                child_id=c2_id,
                name='aaa,bbb,foo',
                geom=func.ST_GeomFromText('POINT(6 46)', 21781)
            )
            engine.connect().execute(ins).inserted_primary_key[0]

        layer = Layer()
        layer.id = id
        layer.geoTable = tablename
        layer.public = public

        if exclude_properties:
            layer.excludeProperties = 'name'

        DBSession.add(layer)

        if not public:
            ra = RestrictionArea()
            ra.name = u'__test_ra'
            ra.layers = [layer]
            ra.roles = [self.role]
            ra.readwrite = True
            if not none_area:
                poly = 'POLYGON((4 44, 4 46, 6 46, 6 44, 4 44))'
                ra.area = WKTSpatialElement(poly, srid=21781)
            DBSession.add(ra)

        self.layer_ids.append(self.__class__._table_index)

        transaction.commit()

        return id
Exemplo n.º 2
0
    def _create_layer(self,
                      public=False,
                      none_area=False,
                      attr_list=False,
                      exclude_properties=False):
        """ This function is central for this test class. It creates
        a layer with two features, and associates a restriction area
        to it. """
        import transaction
        import sqlahelper
        from sqlalchemy import func
        from sqlalchemy import Column, Table, types, ForeignKey
        from sqlalchemy.ext.declarative import declarative_base
        from geoalchemy import (GeometryDDL, GeometryExtensionColumn, Point,
                                WKTSpatialElement)
        from c2cgeoportal.models import DBSession, Layer, RestrictionArea

        self.__class__._table_index = self.__class__._table_index + 1
        id = self.__class__._table_index

        engine = sqlahelper.get_engine()

        if not self.metadata:
            self.metadata = declarative_base(bind=engine).metadata

        tablename = "table_%d" % id

        table = Table('%s_child' % tablename,
                      self.metadata,
                      Column('id', types.Integer, primary_key=True),
                      Column('name', types.Unicode),
                      schema='public')
        table.create()

        ins = table.insert().values(name=u'c1é')
        c1_id = engine.connect().execute(ins).inserted_primary_key[0]
        ins = table.insert().values(name=u'c2é')
        c2_id = engine.connect().execute(ins).inserted_primary_key[0]

        table = Table(tablename,
                      self.metadata,
                      Column('id', types.Integer, primary_key=True),
                      Column('child_id', types.Integer,
                             ForeignKey('public.%s_child.id' % tablename)),
                      Column('name', types.Unicode),
                      GeometryExtensionColumn('geom', Point(srid=21781)),
                      schema='public')
        GeometryDDL(table)
        table.create()

        ins = table.insert().values(child_id=c1_id,
                                    name='foo',
                                    geom=func.ST_GeomFromText(
                                        'POINT(5 45)', 21781))
        engine.connect().execute(ins).inserted_primary_key[0]
        ins = table.insert().values(child_id=c2_id,
                                    name='bar',
                                    geom=func.ST_GeomFromText(
                                        'POINT(6 46)', 21781))
        engine.connect().execute(ins).inserted_primary_key[0]
        if attr_list:
            ins = table.insert().values(child_id=c2_id,
                                        name='aaa,bbb,foo',
                                        geom=func.ST_GeomFromText(
                                            'POINT(6 46)', 21781))
            engine.connect().execute(ins).inserted_primary_key[0]

        layer = Layer()
        layer.id = id
        layer.geoTable = tablename
        layer.public = public

        if exclude_properties:
            layer.excludeProperties = 'name'

        DBSession.add(layer)

        if not public:
            ra = RestrictionArea()
            ra.name = u'__test_ra'
            ra.layers = [layer]
            ra.roles = [self.role]
            ra.readwrite = True
            if not none_area:
                poly = 'POLYGON((4 44, 4 46, 6 46, 6 44, 4 44))'
                ra.area = WKTSpatialElement(poly, srid=21781)
            DBSession.add(ra)

        self.layer_ids.append(self.__class__._table_index)

        transaction.commit()

        return id