From 299843322bc7599bc4c43d468411ea882261b843 Mon Sep 17 00:00:00 2001 From: "Michael A. Salgado" Date: Tue, 1 Sep 2015 11:16:46 -0500 Subject: [PATCH] Fix add cartodb_id field to updated layers --- CartoDBPlugin.py | 13 ++++++++----- dialogs/Upload.py | 21 +++++++++++---------- i18n/en.ts | 8 ++++++-- i18n/es.ts | 8 ++++++-- layers/CartoDBLayer.py | 23 +++++++++++------------ pylintrc | 3 +++ ui/Upload.ui | 2 +- 7 files changed, 46 insertions(+), 32 deletions(-) create mode 100644 pylintrc diff --git a/CartoDBPlugin.py b/CartoDBPlugin.py index 48cf69c..2165654 100644 --- a/CartoDBPlugin.py +++ b/CartoDBPlugin.py @@ -22,12 +22,13 @@ # Import the PyQt and QGIS libraries from PyQt4.QtCore import * from PyQt4.QtGui import * -from qgis.core import * + +from qgis.core import QgsMessageLog, QgsPluginLayerRegistry, QgsMapLayerRegistry from osgeo import gdal from osgeo import ogr + import resources -from cartodb import CartoDBAPIKey, CartoDBException from QgisCartoDB.dialogs import CartoDBPluginUpload, CartoDBPluginDialog, CartoDBNewSQLDialog, CartoDBPluginCreateViz from QgisCartoDB.dialogs.ConnectionManager import CartoDBConnectionsManager from QgisCartoDB.layers import CartoDBLayer, CartoDBPluginLayer, CartoDBPluginLayerType, CartoDBLayerWorker @@ -37,9 +38,11 @@ import os.path import shutil -from urllib import urlopen - +""" +Class CartoDBPlugin +Main plugin class. +""" class CartoDBPlugin(QObject): # initialize plugin directory PLUGIN_DIR = os.path.dirname(os.path.abspath(__file__)) @@ -236,7 +239,7 @@ def upload(self): dlg = CartoDBPluginUpload(self.iface, self.toolbar) def addLayer(fileName, tableName): newLayer = CartoDBLayer(self.iface, tableName, dlg.currentUser, dlg.currentApiKey, - dlg.currentUser, None, spatiaLite=fileName, multiuser=dlg.currentMultiuser) + dlg.currentUser, None, spatiaLite=fileName, multiuser=dlg.currentMultiuser) QgsMapLayerRegistry.instance().addMapLayer(newLayer) self.layers.append(newLayer) diff --git a/dialogs/Upload.py b/dialogs/Upload.py index 175b000..53ee4d7 100644 --- a/dialogs/Upload.py +++ b/dialogs/Upload.py @@ -18,10 +18,10 @@ * * ***************************************************************************/ """ -from PyQt4.QtCore import Qt, QSettings, QFile, QFileInfo, QTimer, pyqtSignal, pyqtSlot, qDebug +from PyQt4.QtCore import Qt, QSettings, QFile, QFileInfo, QTimer, QVariant, pyqtSignal, pyqtSlot, qDebug from PyQt4.QtGui import QApplication, QDialog, QPixmap, QListWidgetItem, QLabel, QSizePolicy -from qgis.core import QgsMapLayerRegistry, QgsMapLayer, QgsVectorFileWriter, QgsField +from qgis.core import QgsMapLayerRegistry, QgsMapLayer, QgsVectorFileWriter, QgsField, QgsVectorLayer from qgis.gui import QgsMessageBar from QgisCartoDB.cartodb import CartoDBApi @@ -76,11 +76,11 @@ def upload(self): for layerItem in self.ui.layersList.selectedItems(): widget = self.ui.layersList.itemWidget(layerItem) qDebug('Layer: ' + str(widget.layer.storageType())) - layer = self.checkCartoDBId(widget.layer) - zipPath = self.zipLayer(layer, self.ui.convertCH.isChecked()) - self.uploadZip(zipPath, widget, self.ui.convertCH.isChecked()) + layer = self.checkCartoDBId(widget.layer, self.ui.convertCH.isChecked()) + zipPath = self.zipLayer(layer) + self.uploadZip(zipPath, widget, layer, self.ui.convertCH.isChecked()) - def uploadZip(self, zipPath, widget, convert = False): + def uploadZip(self, zipPath, widget, convertLayer=None, convert=False): def completeUpload(data): timer = QTimer(self) @@ -101,10 +101,11 @@ def statusComplete(d): level=QgsMessageBar.INFO, duration=5) if convert: - self.convert2CartoDB(widget.layer, d['table_name']) + self.convert2CartoDB(convertLayer if convertLayer is not None else widget.layer, d['table_name']) elif d['state'] == 'failure': timer.stop() - self.ui.statusLB.setText(QApplication.translate('CartoDBPlugin', '{} failed, {}').format(widget.layer.name(), d['get_error_text']['title'])) + self.ui.statusLB.setText(QApplication.translate('CartoDBPlugin', '{} failed, {}').format( + widget.layer.name(), d['get_error_text']['title'])) widget.setStatus(d['state']) self.ui.bar.clearWidgets() self.ui.bar.pushMessage(QApplication.translate('CartoDBPlugin', 'Error uploading {}').format(widget.layer.name()), @@ -179,7 +180,7 @@ def checkCartoDBId(self, layer, convert = False): tf = tempfile.NamedTemporaryFile() error = QgsVectorFileWriter.writeAsVectorFormat(layer, tf.name, 'utf-8', None, 'ESRI Shapefile') if error == QgsVectorFileWriter.NoError: - ly = QgsVectorLayer(tf.name + '.shp', layer.name() + '-tmp', 'ogr') + ly = QgsVectorLayer(tf.name + '.shp', layer.name(), 'ogr') res = ly.dataProvider().addAttributes([QgsField('cartodb_id', QVariant.Int)]) ly.updateFields() features = ly.getFeatures() @@ -218,7 +219,7 @@ def zipLayer(self, layer): if layer.storageType() == 'ESRI Shapefile': for suffix in ['.shp', '.dbf', '.prj', '.shx']: if os.path.exists(os.path.join(dirName, fileName + suffix)): - zipFile.write(os.path.join(dirName, fileName + suffix), fileName + suffix, zipfile.ZIP_DEFLATED) + zipFile.write(os.path.join(dirName, fileName + suffix), layer.name() + suffix, zipfile.ZIP_DEFLATED) elif layer.storageType() == 'GeoJSON': zipFile.write(filePath, layer.name() + '.geojson', zipfile.ZIP_DEFLATED) elif layer.storageType() == 'GPX': diff --git a/i18n/en.ts b/i18n/en.ts index 01fcfb0..3081cff 100644 --- a/i18n/en.ts +++ b/i18n/en.ts @@ -214,8 +214,8 @@ Replace if exists - Add CartoDB layer to project - Add CartoDB layer to project + Add CartoDB layer to project (this could truncate the column's names) + Add CartoDB layer to project (this could truncate the column's names) Cancel @@ -225,6 +225,10 @@ Upload Upload + + Ready + Listo + CreateViz diff --git a/i18n/es.ts b/i18n/es.ts index 2d6115c..31b2281 100644 --- a/i18n/es.ts +++ b/i18n/es.ts @@ -214,8 +214,8 @@ Reemplazar si existe en CartoDB - Add CartoDB layer to project - Agregar capa CartoDB al proyecto + Add CartoDB layer to project (this could truncate the column's names) + Agregar capa CartoDB al proyecto (esto podrĂ­a truncar los nombres de las columnas) Cancel @@ -225,6 +225,10 @@ Upload Subir + + Ready + Listo + CreateViz diff --git a/layers/CartoDBLayer.py b/layers/CartoDBLayer.py index 5a64bb3..96a739c 100644 --- a/layers/CartoDBLayer.py +++ b/layers/CartoDBLayer.py @@ -21,13 +21,10 @@ from PyQt4.QtCore import * from PyQt4.QtGui import * -from qgis.core import * -from osgeo import gdal +from qgis.core import QgsFeatureRequest, QgsVectorLayer, QgsMessageLog, QgsDataSourceURI from osgeo import ogr -import os.path - from urllib import urlopen from PyQt4.QtCore import pyqtSlot @@ -56,8 +53,8 @@ def __init__(self, iface, tableName, user, apiKey, owner=None, sql=None, geoJSON # SQLite available? driverName = "SQLite" sqLiteDrv = ogr.GetDriverByName(driverName) - self.databasePath = QgisCartoDB.CartoDBPlugin.PLUGIN_DIR + '/db/database.sqlite' - self.datasource = sqLiteDrv.Open(self.databasePath, True) + self.database_path = QgisCartoDB.CartoDBPlugin.PLUGIN_DIR + '/db/database.sqlite' + self.datasource = sqLiteDrv.Open(self.database_path, True) self.layerName = tableName self.cartoTable = tableName @@ -127,7 +124,7 @@ def _loadData(self, sql, geoJSON=None, spatiaLite=None): if newLyr is not None: QgsMessageLog.logMessage('New Layer created', 'CartoDB Plugin', QgsMessageLog.INFO) uri = QgsDataSourceURI() - uri.setDatabase(self.databasePath) + uri.setDatabase(self.database_path) uri.setDataSource('', self.layerName, 'geometry') QgsMessageLog.logMessage('New Connection: ' + uri.uri(), 'CartoDB Plugin', QgsMessageLog.INFO) path = uri.uri() @@ -190,14 +187,16 @@ def _uneditableFields(self): if pgField['data_type'] == 'timestamp with time zone': self.setEditorWidgetV2(self.fieldNameIndex(pgField['column_name']), 'DateTime') self.setEditorWidgetV2Config(self.fieldNameIndex(pgField['column_name']), { - u'display_format': u'yyyy-MM-dd hh:mm:ss', - u'field_format': u'yyyy-MM-dd hh:mm:ss', - u'calendar_popup': True}) + u'display_format': u'yyyy-MM-dd hh:mm:ss', + u'field_format': u'yyyy-MM-dd hh:mm:ss', + u'calendar_popup': True + }) elif pgField['data_type'] == 'boolean': self.setEditorWidgetV2(self.fieldNameIndex(pgField['column_name']), 'CheckBox') self.setEditorWidgetV2Config(self.fieldNameIndex(pgField['column_name']), { - u'CheckedState': u'true', - u'UncheckedState': u'false'}) + u'CheckedState': u'true', + u'UncheckedState': u'false' + }) except CartoDBException as e: QgsMessageLog.logMessage(errorMsg + ' - ' + str(e), 'CartoDB Plugin', QgsMessageLog.CRITICAL) diff --git a/pylintrc b/pylintrc new file mode 100644 index 0000000..b416d32 --- /dev/null +++ b/pylintrc @@ -0,0 +1,3 @@ +[FORMAT] +# Maximum number of characters on a single line. +max-line-length = 150 diff --git a/ui/Upload.ui b/ui/Upload.ui index cf55731..be47884 100644 --- a/ui/Upload.ui +++ b/ui/Upload.ui @@ -153,7 +153,7 @@ true - Add CartoDB layer to project + Add CartoDB layer to project (this could truncate the column's names) true