Skip to content

Commit

Permalink
processing: verify postgresql access and request credentials if neces…
Browse files Browse the repository at this point in the history
…sary (fixes #12137)
  • Loading branch information
jef-n committed Feb 12, 2015
1 parent e96b204 commit f540cd1
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions python/plugins/processing/algs/gdal/OgrAlgorithm.py
Expand Up @@ -27,6 +27,7 @@

import re
import os
import psycopg2

from qgis.core import QgsDataSourceURI, QgsCredentials

Expand Down Expand Up @@ -54,14 +55,28 @@ def ogrConnectionString(self, uri):
# key='gid' estimatedmetadata=true srid=4326 type=MULTIPOLYGON
# table="t4" (geom) sql=
dsUri = QgsDataSourceURI(layer.dataProvider().dataSourceUri())
connInfo = dsUri.connectionInfo()
(success, user, passwd ) = QgsCredentials.instance().get(connInfo, None, None)
if success:
QgsCredentials.instance().put(connInfo, user, passwd)
ogrstr = ("PG:dbname='%s' host='%s' port='%s' user='%s' password='%s'"
% (dsUri.database(), dsUri.host(), dsUri.port(), user, passwd))
conninfo = dsUri.connectionInfo()

conn = None
while not conn:
try:
conn = psycopg2.connect(dsUri.connectionInfo())
except psycopg2.OperationalError, e:
(ok, user, passwd ) = QgsCredentials.instance().get(conninfo, dsUri.username(), dsUri.password())
if not ok:
break

dsUri.setUsername( user )
dsUri.setPassword( passwd )

if not conn:
raise RuntimeError('Could not connect to PostgreSQL database - check connection info')

QgsCredentials.instance().put(conninfo, user, passwd)
ogrstr = "PG:%s" % dsUri.connectionInfo()
else:
ogrstr = unicode(layer.source()).split("|")[0]

return '"' + ogrstr + '"'

def ogrLayerName(self, uri):
Expand Down

2 comments on commit f540cd1

@gioman
Copy link
Contributor

@gioman gioman commented on f540cd1 Feb 17, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I think that this commit is causing this error now when using postgis inputs in processing OGR tools

Uncaught error while executing algorithm
Traceback (most recent call last):
Traceback (most recent call last):
File "/usr/share/qgis/python/plugins/processing/core/GeoAlgorithm.py", line 232, in execute
self.processAlgorithm(progress)
File "/usr/share/qgis/python/plugins/processing/algs/gdal/ogrinfo.py", line 55, in processAlgorithm
conn = self.ogrConnectionString(layer)
File "/usr/share/qgis/python/plugins/processing/algs/gdal/OgrAlgorithm.py", line 75, in ogrConnectionString
QgsCredentials.instance().put(conninfo, user, passwd)
UnboundLocalError: local variable 'user' referenced before assignment

@jef-n
Copy link
Member Author

@jef-n jef-n commented on f540cd1 Feb 17, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be fix in e2dd504

Please sign in to comment.