示例#1
0
    def get_collections(self):
        res = []

        collections = self.db.select(
            gws.SqlSelectArgs(table=self.table,
                              extra_where=['type=%s', self.type]))
        items = []
        documents = []
        uids = [c.uid for c in collections]
        if uids:
            cond = self.link_col + ' IN (%s)' % ','.join('%s'
                                                         for _ in collections)

            items = self.db.select(
                gws.SqlSelectArgs(
                    table=self.item_table,
                    extra_where=[cond] + uids,
                ))

            documents = self.db.select(
                gws.SqlSelectArgs(
                    table=self.document_table,
                    extra_where=[cond] + uids,
                    columns=[
                        'id', self.link_col, 'title', 'mimetype', 'filename',
                        'size'
                    ],
                ))

        for coll in collections:
            coll.apply_data_model(self.data_model)
            props = coll.props
            props.type = self.type

            props.items = []
            props.documents = []

            for item in items:
                if str(item.attr(self.link_col)) == str(coll.uid):
                    itype = item.attr(self.type_col)
                    ip = self.item_prototype(itype)
                    if ip:
                        item.apply_data_model(ip.data_model)
                        # @TODO
                        for a in item.attributes:
                            if a.type == 'bytes':
                                a.value = None
                        iprops = item.props
                        iprops.type = itype
                        props.items.append(iprops)

            for doc in documents:
                if str(doc.attr(self.link_col)) == str(coll.uid):
                    props.documents.append(doc.props)

            res.append(props)

        return res
示例#2
0
    def api_csv_export(self, req: gws.IWebRequest, p: ExportParams) -> ExportResponse:
        au_uid = self._check_au(req, p.auUid)

        features = self.db.select(gws.SqlSelectArgs(
            table=self.plan_table,
            extra_where=[f'_au = %s', au_uid],
            sort='name',
        ))

        helper: gws.ext.helper.csv.Object = t.cast(
            gws.ext.helper.csv.Object,
            self.root.find_first('gws.ext.helper.csv'))
        writer = helper.writer()
        has_headers = False

        for f in features:
            if self.export_data_model:
                f.apply_data_model(self.export_data_model)
            if not has_headers:
                writer.write_headers([a.name for a in f.attributes])
                has_headers = True
            writer.write_attributes(f.attributes)

        return ExportResponse(
            fileName='bauplaene_' + au_uid + '.csv',
            content=writer.as_bytes(),
            mime='text/csv'
        )
示例#3
0
    def api_find_flurstueck(self, req: gws.IWebRequest,
                            p: FindFlurstueckParams) -> FindFlurstueckResponse:
        """Perform a Flurstueck search"""

        conds = []
        params = []

        for key in vars(FindFlurstueckParams):
            v = p.get(key)
            if v and v.strip():
                conds.append(f'{key}=%s')
                params.append(v)

        if not conds:
            return FindFlurstueckResponse(features=[], total=0)

        fs = self.db.select(
            gws.SqlSelectArgs(table=self.data_table,
                              extra_where=[' AND '.join(conds)] + params))

        features = {}
        for f in fs:
            if f.uid not in features:
                props = f.apply_templates(self.templates).props
                del props.attributes
                features[f.uid] = props

        return FindFlurstueckResponse(features=list(features.values()),
                                      total=len(features))
示例#4
0
    def _new_request(self, shape):
        self._prepare_request_table()

        features = self.alkis.db.select(
            gws.SqlSelectArgs({
                'shape':
                shape,
                'table':
                self.alkis.db.configure_table(
                    gws.Data(name=self.alkis.index_schema + '.' +
                             _INDEX_TABLE_NAME, ))
            }))

        if not features:
            return None

        request_id = _rand_id()
        data = []

        for f in features:
            d = {
                a.name: a.value
                for a in f.attributes if a.name in self._data_fields
            }
            d['request_id'] = request_id
            d['selection'] = shape.transformed_to(self.alkis.crs).ewkb_hex
            d['ts'] = gws.lib.date.now()
            data.append(d)

        with self.alkis.db.connect() as conn:
            for d in data:
                conn.insert_one(self.request_table_name, 'request_id', d)

        return request_id
示例#5
0
    def get_features(self, bounds, limit=0) -> t.List[gws.IFeature]:
        features = self.provider.select_features(
            gws.SqlSelectArgs(
                table=self.table,
                shape=gws.gis.shape.from_bounds(bounds),
                geometry_tolerance=0,
                limit=limit,
            ))

        return [f.connect_to(self) for f in features]
示例#6
0
 def run(self, args, layer=None):
     n, u = args.tolerance or self.tolerance
     geometry_tolerance = n * args.resolution if u == 'px' else n
     return self.db.select(
         gws.SqlSelectArgs(
             table=self.table,
             keyword=args.keyword,
             shape=self.context_shape(args),
             limit=args.limit,
             geometry_tolerance=geometry_tolerance,
             extra_where=self.extra_where,
         ))
示例#7
0
 def run(self, args, layer=None):
     n, u = args.tolerance or self.tolerance
     geometry_tolerance = n * args.resolution if u == 'px' else n
     return self.provider.select_features(
         gws.SqlSelectArgs(
             table=self.table,
             keyword=args.keyword,
             shape=self.context_shape(args),
             sort=self.var('sort'),
             limit=args.limit,
             geometry_tolerance=geometry_tolerance,
             extra_where=self._filter_to_sql(args.filter),
         ))
示例#8
0
    def api_get_features(self, req: gws.IWebRequest, p: GetFeaturesParams) -> GetFeaturesResponse:
        au_uid = self._check_au(req, p.auUid)

        features = self.db.select(gws.SqlSelectArgs(
            table=self.plan_table,
            extra_where=[f'_au = %s', au_uid],
            sort='name',
        ))
        for f in features:
            f.apply_templates(self.templates)
            g = f.attr('_geom_p') or f.attr('_geom_l') or f.attr('_geom_x')
            if g:
                f.shape = gws.lib.shape.from_wkb_hex(g, self.plan_table.geometry_crs)
            f.attributes = []

        return GetFeaturesResponse(features=[f.props for f in features])
示例#9
0
    def api_get_details(self, req: gws.IWebRequest,
                        p: GetDetailsParams) -> GetDetailsResponse:
        fs_key = self.data_table.key_column

        fs = self.db.select(
            gws.SqlSelectArgs(table=self.data_table,
                              extra_where=[f'{fs_key} = %s', p.fsUid]))
        if not fs:
            raise gws.base.web.error.NotFound()

        feature = None
        persons = []
        docs = self._all_documents()

        for f in fs:
            if not feature:
                feature = f
            data = f.attr_dict

            if data['pn']:
                persons.append(
                    PersonProps(uid=str(data['pn']),
                                title=self.title_template.render({
                                    'feature': feature,
                                    'data': data,
                                }).content,
                                description=self.details_template.render({
                                    'feature':
                                    feature,
                                    'data':
                                    data,
                                }).content,
                                documents=[
                                    d for d in docs
                                    if d.personUid == str(data['pn'])
                                ]))

        fprops = feature.apply_templates(self.templates).props
        del fprops.attributes

        return GetDetailsResponse(feature=fprops, persons=persons)
示例#10
0
    def api_load_data(self, req: gws.IWebRequest,
                      p: LoadDataParams) -> LoadDataResponse:
        tbl = self._get_table(p.tableUid)
        if not tbl:
            raise gws.base.web.error.NotFound()

        features = self.db.select(
            gws.SqlSelectArgs(table=tbl.table,
                              sort=tbl.sort,
                              extra_where=['true']))

        attributes = [
            gws.Attribute(
                name=r.name,
                title=r.title,
                type=r.type,
                editable=r.editable,
            ) for r in tbl.data_model.rules
        ]

        records = []

        for f in features:
            f.apply_data_model(tbl.data_model)
            attr_dict = f.attr_dict
            records.append([attr_dict.get(a.name) for a in attributes])

        return LoadDataResponse(
            tableUid=tbl.uid,
            key=tbl.table.key_column,
            attributes=attributes,
            records=records,
            widths=tbl.widths or None,
            withFilter=tbl.with_filter,
            withAdd=tbl.with_add,
            withDelete=tbl.with_delete,
        )
示例#11
0
 def get_document(self, collection_uid, document_uid):
     documents = self.db.select(
         gws.SqlSelectArgs(table=self.document_table, uids=[document_uid]))
     for doc in documents:
         return doc
示例#12
0
 def get_collection_ids(self):
     colls = self.db.select(
         gws.SqlSelectArgs(table=self.table,
                           extra_where=['type=%s', self.type]))
     return [str(c.uid) for c in colls]