def load_link_tables(self, reg_exp=None, source=TABLES | VIEWS): self.cbo_ref_table.clear() self.cbo_ref_table.addItem("") ref_tables = [] #Table source if (TABLES & source) == TABLES: ref_tables.extend(pg_tables(exclude_lookups=True)) #View source if (VIEWS & source) == VIEWS: ref_tables.extend(pg_views()) source_tables = [] for t in ref_tables: #Ensure we are dealing with tables in the current profile if not t in self._current_profile_tables: continue #Assert if the table is in the list of omitted tables if t in self._omit_ref_tables: continue if not reg_exp is None: if reg_exp.indexIn(t) >= 0: source_tables.append(t) else: source_tables.append(t) source_tables = source_tables + pg_views() self.cbo_ref_table.addItems(source_tables)
def control_digitize_toolbar(self, curr_layer): if not curr_layer is None: table, column = self._layer_table_column(curr_layer) if table not in pg_views(): # Make sure digitizing toolbar is enabled self.iface.digitizeToolBar().setEnabled(True) self.set_canvas_crs(curr_layer) elif table in pg_views(): self.iface.digitizeToolBar().setEnabled(False)
def control_digitize_toolbar(self, curr_layer): if not curr_layer is None: try: table, column = self._layer_table_column( curr_layer ) if table not in pg_views(): # Make sure digitizing toolbar is enabled self.iface.digitizeToolBar().setEnabled(True) self.set_canvas_crs(curr_layer) elif table in pg_views(): self.iface.digitizeToolBar().setEnabled(False) except Exception: pass
def update_table(self, key, child): """ Updates the table attribute in sub-child of the dataSource node of the template. :param key: The attribute name inside a child element(table). :type key: String :param child: The sub-child element of the DataSource element. :type child: """ # if it is view, upgrade the view and # replace new view and cols ref_old_new_cols = None if child.attrib[key] in pg_views(): old_view = child.attrib[key] if 'new_' in old_view: old_view = old_view.lstrip('new_') upgraded_view = self.upgrade_view(old_view) if upgraded_view is not None: ref_old_new_cols = self.old_new_columns( child.attrib[key], upgraded_view) child.attrib[key] = upgraded_view self.old_new_cols_list.append(ref_old_new_cols) # if it is table, get new table from dic # replace old with new and get new cols, old cols else: if child.attrib[key] in self.old_new_tables.keys(): ref_old_new_cols = self.old_new_columns( child.attrib[key], self.old_new_tables[child.attrib[key]]) child.attrib[key] = self.old_new_tables[child.attrib[key]] return ref_old_new_cols
def profile_user_tables(profile, include_views=True, admin=False): """ Returns user accessible tables from current profile and pg_views . :param profile: Current Profile :type profile: Class :param include_views: A Boolean that includes or excludes Views :type include_views: Boolean :return: Dictionary of user tables with name and short name as a key and value. :param admin: A Boolean that enables administrative unit table if True. :type admin: Boolean :rtype: Dictionary """ from stdm.data.pg_utils import (pg_views) if not admin: tables = [(e.name, e.short_name) for e in profile.entities.values() if e.TYPE_INFO in [ 'ENTITY', 'ENTITY_SUPPORTING_DOCUMENT', 'SOCIAL_TENURE', 'SUPPORTING_DOCUMENT', 'VALUE_LIST', 'ASSOCIATION_ENTITY' ]] else: tables = [(e.name, e.short_name) for e in profile.entities.values() if e.TYPE_INFO in [ 'ENTITY', 'ENTITY_SUPPORTING_DOCUMENT', 'ADMINISTRATIVE_SPATIAL_UNIT', 'SOCIAL_TENURE', 'SUPPORTING_DOCUMENT', 'ASSOCIATION_ENTITY', 'VALUE_LIST' ]] tables = dict(tables) if include_views: for view in pg_views(): tables[view] = view return tables
def onShowViews(self, state): """ Slot raised to show STDM database views. """ if state: self._reset_referenced_table_combo() self.cboDataSource.clear() self.cboDataSource.addItem('') for value in pg_views(): self.cboDataSource.addItem(value, value)
def layer_source(self, layer): """ Gets the layer source. :param layer: The layer :type layer: Any :return: Layer Source or None :rtype: """ if layer is None: return None source = layer.source() if source is None: return False source_value = dict(re.findall('(\S+)="?(.*?)"? ', source)) try: table = source_value['table'].split('.') table_name = table[1].strip('"') if table_name in pg_views(): return False entity = self._curr_profile.entity_by_name(table_name) if entity is None: return False else: self.active_entity = entity self.active_table = table_name # get all spatial columns of the entity. spatial_columns = [ c.name for c in entity.columns.values() if c.TYPE_INFO == 'GEOMETRY' ] # get all fields excluding the geometry. layer_fields = [ field.name() for field in layer.pendingFields() ] # get the currently being used geometry column active_sp_cols = [ col for col in spatial_columns if col not in layer_fields ] if len(active_sp_cols) == 1: self.active_sp_col = active_sp_cols[0] return True except KeyError: return False
def active_layer_source(self): """ Get the layer table name if the source is from the database. :return: The a Boolean True if a valid source is found or False if not. Alternatively, None if there is no active layer. :rtype: Boolean or NoneType """ active_layer = self.iface.activeLayer() if active_layer is None: return None source = active_layer.source() if source is None: return False source_value = dict(re.findall('(\S+)="?(.*?)"? ', source)) try: table = source_value['table'].split('.') table_name = table[1].strip('"') if table_name in pg_views(): return False entity = self._curr_profile.entity_by_name(table_name) if entity is None: return False else: self.active_entity = entity self.active_table = table_name # get all spatial columns of the entity. spatial_columns = [ c.name for c in entity.columns.values() if c.TYPE_INFO == 'GEOMETRY' ] # get all fields excluding the geometry. layer_fields = [ field.name() for field in active_layer.pendingFields() ] # get the currently being used geometry column active_sp_cols = [ col for col in spatial_columns if col not in layer_fields ] if len(active_sp_cols) == 1: self.active_sp_col = active_sp_cols[0] return True except KeyError: return False
def init_spatial_form(self, spatial_column, curr_layer): """ Initializes the Layer form. :param curr_layer: The layer for which the widgets are set. :type curr_layer: QgsVectorLayer :return: None :rtype: NoneType """ table, column = self._layer_table_column(curr_layer) if table not in pg_views() and curr_layer is not None: self.stdm_fields.init_form(table, spatial_column, curr_layer)
def profile_and_user_views(profile, check_party=False): """ Gets current profile and user views. If views has a valid profile prefix and not valid current profile entity, it will be excluded. :param profile: The profile object :type profile: Object :param check_party: A boolean to filter out party views based on multi_party If check party is True and multi_party is True, it excludes party views from the return :type check_party: Boolean :return: List of profile and user views :rtype: List """ from stdm.data.pg_utils import ( pg_views ) from stdm.data.configuration.stdm_configuration import ( StdmConfiguration ) source_tables = [] social_tenure = profile.social_tenure for view, entity in social_tenure.views.iteritems(): # For party views, if check party is true, add party views. # If multi-party is false. Otherwise, add all party views - this is not # recommended for composer data source. if entity in social_tenure.parties: if check_party: if not social_tenure.multi_party: source_tables.append(view) else: source_tables.append(view) else: source_tables.append(view) stdm_config = StdmConfiguration.instance() all_str_views = [] for prof in stdm_config.profiles.values(): all_str_views.extend(prof.social_tenure.views.keys()) for value in pg_views(): #if value not in all_str_views: #and value not in source_tables: #if value[:2] == profile.prefix: if value not in source_tables: source_tables.append(value) return source_tables
def profile_and_user_views(profile, check_party=False): """ Gets current profile and user views. If views has a valid profile prefix and not valid current profile entity, it will be excluded. :param profile: The profile object :type profile: Object :param check_party: A boolean to filter out party views based on multi_party If check party is True and multi_party is True, it excludes party views from the return :type check_party: Boolean :return: List of profile and user views :rtype: List """ from stdm.data.pg_utils import (pg_views) from stdm.data.configuration.stdm_configuration import (StdmConfiguration) source_tables = [] stdm_config = StdmConfiguration.instance() social_tenure = profile.social_tenure for value in pg_views(): if 'vw_social_tenure_relationship' in value: # if a value exist on the left side of vw, assess further if len(value.split('_vw')) > 0: entity = value.split('_vw')[0] # we are more sure this could be entity if '_' in entity and len(entity.split('_')) > 0: # if the prefix exist in the configuration if entity.split('_')[0] in \ stdm_config.prefixes(): # Check if the entity is in the current profile entity_obj = profile.entity_by_name(entity) if entity_obj is not None: if check_party and entity_obj in social_tenure.parties: if not social_tenure.multi_party: source_tables.append(value) else: source_tables.append(value) # it means this is not a valid entity so add it else: source_tables.append(value) # it is a user view; add it to the combo list. else: source_tables.append(value) # it is a user view; add it to the combo list. else: source_tables.append(value) else: source_tables.append(value) return source_tables
def init_spatial_form(self, spatial_column, curr_layer): """ Initializes the Layer form. :param curr_layer: The layer for which the widgets are set. :type curr_layer: QgsVectorLayer :return: None :rtype: NoneType """ table, column = self._layer_table_column(curr_layer) if table not in pg_views() and not curr_layer is None: try: self.stdm_fields.init_form(table, spatial_column, curr_layer) except Exception as ex: LOGGER.debug(unicode(ex))
def profile_user_tables(profile, include_views=True, admin=False, sort=False, include_read_only: bool = True): """ Returns user accessible tables from current profile and pg_views . :param profile: Current Profile :type profile: Class :param include_views: A Boolean that includes or excludes Views :type include_views: Boolean :return: Dictionary of user tables with name and short name as a key and value. :param admin: A Boolean that enables administrative unit table if True. :type admin: Boolean :param include_read_only: if set to False, then only user editable tables will be returned :rtype: Dictionary """ from stdm.data.pg_utils import (pg_views) valid_types = [ 'ENTITY', 'ENTITY_SUPPORTING_DOCUMENT', 'SOCIAL_TENURE', 'SUPPORTING_DOCUMENT', 'VALUE_LIST', 'ASSOCIATION_ENTITY' ] if admin: valid_types.append('ADMINISTRATIVE_SPATIAL_UNIT') entities = [ e for e in profile.entities.values() if e.TYPE_INFO in valid_types ] if not include_read_only: entities = [e for e in entities if e.user_editable] tables = OrderedDict([(e.name, e.ui_display()) for e in entities]) if include_views: for view in pg_views(): tables[view] = view if sort: names = list(tables.keys()) names = sorted(names) sorted_table = OrderedDict() for name in names: sorted_table[name] = tables[name] return sorted_table return tables
def update_table(self, key, child): """ Updates the table attribute in sub-child of the dataSource node of the template. :param key: The attribute name inside a child element(table). :type key: String :param child: The sub-child element of the DataSource element. :type child: """ # if it is view, upgrade the view and # replace new view and cols ref_old_new_cols = None if child.attrib[key] in pg_views(): old_view = child.attrib[key] if 'new_' in old_view: old_view = old_view.lstrip( 'new_' ) upgraded_view = self.upgrade_view( old_view ) if not upgraded_view is None: ref_old_new_cols = self.old_new_columns( child.attrib[key], upgraded_view ) child.attrib[key] = upgraded_view self.old_new_cols_list.append(ref_old_new_cols) # if it is table, get new table from dic # replace old with new and get new cols, old cols else: if child.attrib[key] in self.old_new_tables.keys(): ref_old_new_cols = self.old_new_columns( child.attrib[key], self.old_new_tables[ child.attrib[key] ] ) child.attrib[key] = self.old_new_tables[ child.attrib[key] ] return ref_old_new_cols
def init_spatial_form(self, spatial_column, curr_layer): """ Initializes the Layer form. :param curr_layer: The layer for which the widgets are set. :type curr_layer: QgsVectorLayer :return: None :rtype: NoneType """ table, column = self._layer_table_column(curr_layer) if table not in pg_views() and not curr_layer is None: try: self.stdm_fields.init_form( table, spatial_column, curr_layer ) except Exception as ex: LOGGER.debug(unicode(ex))
def user_non_profile_views(): """ Gets and return user based views excluding profile views in the configuration. :return: User views :rtype: List """ from stdm.data.configuration.stdm_configuration import (StdmConfiguration) from stdm.data.pg_utils import (pg_views) source_tables = [] stdm_config = StdmConfiguration.instance() all_str_views = [] for prof in stdm_config.profiles.values(): all_str_views.extend(prof.social_tenure.views.keys()) for value in pg_views(): if value not in all_str_views: source_tables.append(value) return source_tables
def view_details(self, view): """ Gets the view definition/query used to create it. :param view: The name of the view. :type view: String :return: The definition/query. :rtype: String """ if view in pg_views(): t = text('SELECT definition ' 'FROM pg_views ' 'WHERE viewname=:view_name;') result = _execute(t, view_name=view) definition = [] for row in result: definition.append(row[0]) return definition[0]
def view_details(self, view): """ Gets the view definition/query used to create it. :param view: The name of the view. :type view: String :return: The definition/query. :rtype: String """ if view in pg_views(): t = text('SELECT definition ' 'FROM pg_views ' 'WHERE viewname=:view_name;' ) result = _execute(t, view_name=view) definition = [] for row in result: definition.append(row[0]) return definition[0]
def _populate_layers(self): self.stdm_layers_combo.clear() if self._curr_profile is None: return self.spatial_units = self._curr_profile.social_tenure.spatial_units # Get entities containing geometry # columns based on the config info config_entities = self._curr_profile.entities self.geom_entities = [ ge for ge in config_entities.values() if ge.TYPE_INFO == 'ENTITY' and ge.has_geometry_column() ] self._profile_spatial_layers = [] self.sp_tables = spatial_tables() for e in self.geom_entities: table_name = e.name if table_name in self.sp_tables: for i, gc in enumerate(e.geometry_columns()): column_name = gc.name display_name = gc.layer_display() if i > 0: display_name = u'{}.{}'.format(display_name, gc.name) self._add_geometry_column_to_combo( table_name, column_name, display_name, gc ) # Add geometry entity to the collection self._profile_spatial_layers.append( table_name ) # Append the corresponding(profile) view to the list of entity names str_views = self._curr_profile.social_tenure.views.keys() for str_view in str_views: if str_view in self.sp_tables: self.str_view_geom_columns = table_column_names( str_view, True ) if len(self.str_view_geom_columns) > 0: # Pick the first column for i, geom_col in enumerate(self.str_view_geom_columns): view_layer_name = str_view if i > 0: view_layer_name = '{}.{}'.format( view_layer_name, geom_col ) self._add_geometry_column_to_combo( str_view, geom_col, view_layer_name, self._curr_profile.social_tenure ) # Append view to the list of spatial layers self._profile_spatial_layers.append( str_view ) # add old config views and custom views. for sp_table in self.sp_tables: if sp_table in pg_views() and sp_table not in str_views and \ sp_table in profile_and_user_views( self._curr_profile): view_geom_columns = table_column_names( sp_table, True ) for geom_col in view_geom_columns: view_layer_name = '{}.{}'.format( sp_table, geom_col ) self._add_geometry_column_to_combo( sp_table, geom_col, view_layer_name, geom_col )
def _populate_layers(self): self.stdm_layers_combo.clear() if self._curr_profile is None: return self.spatial_units = self._curr_profile.social_tenure.spatial_units # Get entities containing geometry # columns based on the config info config_entities = self._curr_profile.entities self.geom_entities = [ ge for ge in config_entities.values() if ge.TYPE_INFO == 'ENTITY' and ge.has_geometry_column() ] self._profile_spatial_layers = [] self.sp_tables = spatial_tables() for e in self.geom_entities: table_name = e.name if table_name in self.sp_tables: for i, gc in enumerate(e.geometry_columns()): column_name = gc.name display_name = gc.layer_display() if i > 0: display_name = '{}.{}'.format(display_name, gc.name) self._add_geometry_column_to_combo(table_name, column_name, display_name, gc) # Add geometry entity to the collection self._profile_spatial_layers.append(table_name) # Append the corresponding(profile) view to the list of entity names str_views = list(self._curr_profile.social_tenure.views.keys()) for str_view in str_views: if str_view in self.sp_tables: self.str_view_geom_columns = table_column_names(str_view, True) if len(self.str_view_geom_columns) > 0: # Pick the first column for i, geom_col in enumerate(self.str_view_geom_columns): view_layer_name = str_view if i > 0: view_layer_name = '{}.{}'.format( view_layer_name, geom_col) self._add_geometry_column_to_combo( str_view, geom_col, view_layer_name, self._curr_profile.social_tenure) # Append view to the list of spatial layers self._profile_spatial_layers.append(str_view) # add old config views and custom views. for sp_table in self.sp_tables: if sp_table in pg_views() and sp_table not in str_views and \ sp_table in profile_and_user_views( self._curr_profile): view_geom_columns = table_column_names(sp_table, True) for geom_col in view_geom_columns: view_layer_name = '{}.{}'.format(sp_table, geom_col) self._add_geometry_column_to_combo(sp_table, geom_col, view_layer_name, geom_col)