Skip to content

Commit

Permalink
Remove ad-hoc python provider connection parameters and replace
Browse files Browse the repository at this point in the history
with proper provider connection parameters
  • Loading branch information
nyalldawson committed Mar 10, 2020
1 parent 6997c18 commit f587430
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 61 deletions.
12 changes: 5 additions & 7 deletions python/plugins/processing/algs/gdal/ogr2ogrtopostgislist.py
Expand Up @@ -28,7 +28,8 @@
QgsProcessingParameterCrs,
QgsProcessingParameterField,
QgsProcessingParameterExtent,
QgsProcessingParameterBoolean)
QgsProcessingParameterBoolean,
QgsProcessingParameterProviderConnection)

from processing.algs.gdal.GdalAlgorithm import GdalAlgorithm
from processing.algs.gdal.GdalUtils import GdalUtils
Expand Down Expand Up @@ -80,12 +81,9 @@ def __init__(self):

def initAlgorithm(self, config=None):

db_param = QgsProcessingParameterString(
db_param = QgsProcessingParameterProviderConnection(
self.DATABASE,
self.tr('Database (connection name)'))
db_param.setMetadata({
'widget_wrapper': {
'class': 'processing.gui.wrappers_postgis.ConnectionWidgetWrapper'}})
self.tr('Database (connection name)'), 'postgres')
self.addParameter(db_param)
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT,
self.tr('Input layer'),
Expand Down Expand Up @@ -202,7 +200,7 @@ def groupId(self):
return 'vectormiscellaneous'

def getConsoleCommands(self, parameters, context, feedback, executing=True):
connection = self.parameterAsString(parameters, self.DATABASE, context)
connection = self.parameterAsConnectionName(parameters, self.DATABASE, context)
uri = uri_from_name(connection)
if executing:
# to get credentials input when needed
Expand Down
11 changes: 5 additions & 6 deletions python/plugins/processing/algs/qgis/ImportIntoPostGIS.py
Expand Up @@ -30,6 +30,7 @@
QgsProcessingParameterString,
QgsProcessingParameterField,
QgsProcessingParameterBoolean,
QgsProcessingParameterProviderConnection,
QgsWkbTypes)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
Expand Down Expand Up @@ -64,12 +65,10 @@ def initAlgorithm(self, config=None):
self.tr('Layer to import'),
types=[QgsProcessing.TypeVector]))

db_param = QgsProcessingParameterString(
db_param = QgsProcessingParameterProviderConnection(
self.DATABASE,
self.tr('Database (connection name)'))
db_param.setMetadata({
'widget_wrapper': {
'class': 'processing.gui.wrappers_postgis.ConnectionWidgetWrapper'}})
self.tr('Database (connection name)'), 'postgres'
)
self.addParameter(db_param)

schema_param = QgsProcessingParameterString(
Expand Down Expand Up @@ -123,7 +122,7 @@ def tags(self):
return self.tr('import,postgis,table,layer,into,copy').split(',')

def processAlgorithm(self, parameters, context, feedback):
connection = self.parameterAsString(parameters, self.DATABASE, context)
connection = self.parameterAsConnectionName(parameters, self.DATABASE, context)
db = postgis.GeoDB.from_name(connection)

schema = self.parameterAsString(parameters, self.SCHEMA, context)
Expand Down
10 changes: 4 additions & 6 deletions python/plugins/processing/algs/qgis/PostGISExecuteAndLoadSQL.py
Expand Up @@ -33,6 +33,7 @@
QgsProcessingException,
QgsProcessingOutputVectorLayer,
QgsProcessingContext,
QgsProcessingParameterProviderConnection,
QgsProcessingFeedback)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools import postgis
Expand All @@ -56,12 +57,9 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
db_param = QgsProcessingParameterString(
db_param = QgsProcessingParameterProviderConnection(
self.DATABASE,
self.tr('Database (connection name)'))
db_param.setMetadata({
'widget_wrapper': {
'class': 'processing.gui.wrappers_postgis.ConnectionWidgetWrapper'}})
self.tr('Database (connection name)'), 'postgres')
self.addParameter(db_param)
self.addParameter(QgsProcessingParameterString(
self.SQL,
Expand Down Expand Up @@ -94,7 +92,7 @@ def tags(self):
return self.tr('postgis,table,database').split(',')

def processAlgorithm(self, parameters, context, feedback):
connection = self.parameterAsString(parameters, self.DATABASE, context)
connection = self.parameterAsConnectionName(parameters, self.DATABASE, context)
id_field = self.parameterAsString(parameters, self.ID_FIELD, context)
geom_field = self.parameterAsString(
parameters, self.GEOMETRY_FIELD, context)
Expand Down
15 changes: 8 additions & 7 deletions python/plugins/processing/algs/qgis/PostGISExecuteSQL.py
Expand Up @@ -21,7 +21,11 @@
__date__ = 'October 2012'
__copyright__ = '(C) 2012, Victor Olaya, Carterix Geomatics'

from qgis.core import (QgsProcessingException, QgsProcessingParameterString)
from qgis.core import (
QgsProcessingException,
QgsProcessingParameterString,
QgsProcessingParameterProviderConnection
)
from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.tools import postgis

Expand All @@ -41,12 +45,9 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
db_param = QgsProcessingParameterString(
db_param = QgsProcessingParameterProviderConnection(
self.DATABASE,
self.tr('Database (connection name)'))
db_param.setMetadata({
'widget_wrapper': {
'class': 'processing.gui.wrappers_postgis.ConnectionWidgetWrapper'}})
self.tr('Database (connection name)'), 'postgres')
self.addParameter(db_param)
self.addParameter(QgsProcessingParameterString(self.SQL, self.tr('SQL query'), multiLine=True))

Expand All @@ -63,7 +64,7 @@ def tags(self):
return self.tr('postgis,database').split(',')

def processAlgorithm(self, parameters, context, feedback):
connection = self.parameterAsString(parameters, self.DATABASE, context)
connection = self.parameterAsConnectionName(parameters, self.DATABASE, context)
db = postgis.GeoDB.from_name(connection)

sql = self.parameterAsString(parameters, self.SQL, context).replace('\n', ' ')
Expand Down
36 changes: 1 addition & 35 deletions python/plugins/processing/gui/wrappers_postgis.py
Expand Up @@ -18,8 +18,7 @@
"""


from qgis.core import (QgsSettings,
QgsProcessingParameterNumber,
from qgis.core import (QgsProcessingParameterNumber,
QgsProcessingParameterFile,
QgsProcessingParameterField,
QgsProcessingParameterExpression,
Expand All @@ -35,39 +34,6 @@
from processing.tools.postgis import GeoDB


class ConnectionWidgetWrapper(WidgetWrapper):
"""
WidgetWrapper for ParameterString that create and manage a combobox widget
with existing postgis connections.
"""

def createWidget(self):
self._combo = QComboBox()
for group in self.items():
self._combo.addItem(*group)
self._combo.currentIndexChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
return self._combo

def items(self):
settings = QgsSettings()
settings.beginGroup('/PostgreSQL/connections/')
items = [(group, group) for group in settings.childGroups()]

if self.dialogType == DIALOG_MODELER:
strings = self.dialog.getAvailableValuesOfType(
[QgsProcessingParameterString, QgsProcessingParameterNumber, QgsProcessingParameterFile,
QgsProcessingParameterField, QgsProcessingParameterExpression], QgsProcessingOutputString)
items = items + [(self.dialog.resolveValueDescription(s), s) for s in strings]

return items

def setValue(self, value):
self.setComboValue(value, self._combo)

def value(self):
return self.comboValue(combobox=self._combo)


class SchemaWidgetWrapper(WidgetWrapper):
"""
WidgetWrapper for ParameterString that create and manage a combobox widget
Expand Down

0 comments on commit f587430

Please sign in to comment.