Skip to content
Permalink
Browse files
processing: verify postgresql access and request credentials if neces…
…sary (fixes #12137)
  • Loading branch information
jef-n committed Feb 12, 2015
1 parent e96b204 commit f540cd16a2118a16dae5f5a8642255b9d922679b
Showing with 21 additions and 6 deletions.
  1. +21 −6 python/plugins/processing/algs/gdal/OgrAlgorithm.py
@@ -27,6 +27,7 @@

import re
import os
import psycopg2

from qgis.core import QgsDataSourceURI, QgsCredentials

@@ -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):

2 comments on commit f540cd1

@gioman

This comment has been minimized.

Copy link
Contributor

@gioman gioman replied Feb 17, 2015

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

This comment has been minimized.

Copy link
Member Author

@jef-n jef-n replied Feb 17, 2015

should be fix in e2dd504

Please sign in to comment.