def feature_create(self, feature): """Insert new object to DB which is described in feature :param feature: object description :type feature: Feature :return: inserted object ID """ self.before_feature_create.fire(resource=self, feature=feature) tableinfo = TableInfo.from_layer(self) tableinfo.setup_metadata(self._tablename) obj = tableinfo.model() for f in tableinfo.fields: if f.keyname in feature.fields.keys(): setattr(obj, f.key, feature.fields[f.keyname]) obj.geom = ga_from_shape(feature.geom, srid=self.srs_id) if feature.geom.geom_type.upper() != self.geometry_type: raise ValidationError( _("Geometry type (%s) does not match geometry column type (%s)." ) % (feature.geom.geom_type.upper(), self.geometry_type)) DBSession.add(obj) DBSession.flush() DBSession.refresh(obj) self.after_feature_create.fire(resource=self, feature_id=obj.id) on_data_change.fire(self, feature.geom) return obj.id
def feature_put(self, feature): self.before_feature_update.fire(resource=self, feature=feature) tableinfo = TableInfo.from_layer(self) tableinfo.setup_metadata(self._tablename) obj = tableinfo.model(id=feature.id) for f in tableinfo.fields: if f.keyname in feature.fields: setattr(obj, f.key, feature.fields[f.keyname]) # FIXME: Don't try to write geometry if it exists. # This will not let to write empty geometry, but it is not needed yet. if feature.geom is not None: obj.geom = ga_from_shape(feature.geom, srid=self.srs_id) DBSession.merge(obj) self.after_feature_update.fire(resource=self, feature=feature) on_data_change.fire(self, feature.geom)