def load_mappy_info_text(): file = QFile(":/plugins/qgismappy/INFO.html") file.open(QFile.ReadOnly | QFile.Text) stream = QTextStream(file) text = stream.readAll() # print(f"text {text}") return text
def is_source_service_valid(self): res = False msg = {'text': '', 'level': Qgis.Warning} url = self.txt_service_endpoint.text().strip() if url: with OverrideCursor(Qt.WaitCursor): self.qgis_utils.status_bar_message_emitted.emit("Checking source service availability (this might take a while)...", 0) QCoreApplication.processEvents() if self.qgis_utils.is_connected(TEST_SERVER): nam = QNetworkAccessManager() request = QNetworkRequest(QUrl(url)) reply = nam.get(request) loop = QEventLoop() reply.finished.connect(loop.quit) loop.exec_() allData = reply.readAll() response = QTextStream(allData, QIODevice.ReadOnly) status = reply.attribute(QNetworkRequest.HttpStatusCodeAttribute) if status == 200: try: data = json.loads(response.readAll()) if 'id' in data and data['id'] == SOURCE_SERVICE_EXPECTED_ID: res = True msg['text'] = QCoreApplication.translate("SettingsDialog", "The tested service is valid to upload files!") msg['level'] = Qgis.Info else: res = False msg['text'] = QCoreApplication.translate("SettingsDialog", "The tested upload service is not compatible: no valid 'id' found in response.") except json.decoder.JSONDecodeError as e: res = False msg['text'] = QCoreApplication.translate("SettingsDialog", "Response from the tested service is not compatible: not valid JSON found.") else: res = False msg['text'] = QCoreApplication.translate("SettingsDialog", "There was a problem connecting to the server. The server might be down or the service cannot be reached at the given URL.") else: res = False msg['text'] = QCoreApplication.translate("SettingsDialog", "There was a problem connecting to Internet.") self.qgis_utils.clear_status_bar_emitted.emit() else: res = False msg['text'] = QCoreApplication.translate("SettingsDialog", "Not valid service URL to test!") return (res, msg)
def table_view_dependencies(table_name, column_name=None): """ Find database views that are dependent on the given table and optionally the column. :param table_name: Table name :type table_name: str :param column_name: Name of the column whose dependent views are to be extracted. :type column_name: str :return: A list of views which are dependent on the given table name and column respectively. :rtype: list(str) """ views = [] # Load the SQL file depending on whether its table or table/column if column_name is None: script_path = PLUGIN_DIR + '/scripts/table_related_views.sql' else: script_path = PLUGIN_DIR + '/scripts/table_column_related_views.sql' script_file = QFile(script_path) if not script_file.exists(): raise IOError('SQL file for retrieving view dependencies could ' 'not be found.') else: if not script_file.open(QIODevice.ReadOnly): raise IOError('Failed to read the SQL file for retrieving view ' 'dependencies.') reader = QTextStream(script_file) sql = reader.readAll() if sql: t = text(sql) if column_name is None: result = _execute(t, table_name=table_name) else: result = _execute(t, table_name=table_name, column_name=column_name) # Get view names for r in result: view_name = r['view_name'] views.append(view_name) return views
def __init__(self, qgis_utils): QDialog.__init__(self) self.setupUi(self) self.qgis_utils = qgis_utils self.check_local_help() self.tb_changelog.setOpenExternalLinks(True) if QGIS_LANG == 'en': file = QFile(":/Asistente-LADM_COL/resources/html/Changelog_en.html") else: file = QFile(":/Asistente-LADM_COL/resources/html/Changelog.html") if not file.open(QIODevice.ReadOnly | QIODevice.Text): raise Exception(file.errorString()) stream = QTextStream(file) stream.setCodec("UTF-8") self.tb_changelog.setHtml(stream.readAll())
def load_aoi_from_file(self): path, _ = QFileDialog.getOpenFileName(self, "Open GeoJSON AOI file", QDir.homePath(), "JSON (*.json);;All Files (*)") file = QFile(path) if not file.open(QFile.ReadOnly | QFile.Text): return inf = QTextStream(file) json_txt = inf.readAll() try: json_obj = json.loads(json_txt) except ValueError: # noinspection PyUnresolvedReferences self._show_message('GeoJSON from file invalid', level=Qgis.Warning, duration=10) return json_geom = geometry_from_json(json_obj) if not json_geom: # noinspection PyUnresolvedReferences self._show_message('GeoJSON geometry from file invalid', level=Qgis.Warning, duration=10) return geom: QgsGeometry = qgsgeometry_from_geojson(json_geom) self._aoi_box.setToGeometry(geom, QgsCoordinateReferenceSystem("EPSG:4326")) self.leAOI.setText(json.dumps(json_geom)) self.zoom_to_aoi()
def upload_files(self, layer, field_index, features): """ Upload given features' source files to remote server and return a dict formatted as changeAttributeValues expects to update 'datos' attribute to a remote location. """ if not QSettings().value( 'Asistente-LADM_COL/sources/document_repository', False, bool): self.message_with_duration_emitted.emit( QCoreApplication.translate( "SourceHandler", "The source files were not uploaded to the document repository because you have that option unchecked. You can still upload the source files later using the 'Upload Pending Source Files' menu." ), Qgis.Info, 10) return dict() # Test if we have Internet connection and a valid service dlg = self.qgis_utils.get_settings_dialog() res, msg = dlg.is_source_service_valid() if not res: msg['text'] = QCoreApplication.translate( "SourceHandler", "No file could be uploaded to the document repository. You can do it later from the 'Upload Pending Source Files' menu. Reason: {}" ).format(msg['text']) self.message_with_duration_emitted.emit( msg['text'], Qgis.Info, 20) # The data is still saved, so always show Info msg return dict() file_features = [ feature for feature in features if not feature[field_index] == NULL and os.path.isfile(feature[field_index]) ] total = len(features) not_found = total - len(file_features) upload_dialog = UploadProgressDialog(len(file_features), not_found) upload_dialog.show() count = 0 upload_errors = 0 new_values = dict() for feature in file_features: data_url = feature[field_index] file_name = os.path.basename(data_url) nam = QNetworkAccessManager() #reply.downloadProgress.connect(upload_dialog.update_current_progress) multiPart = QHttpMultiPart(QHttpMultiPart.FormDataType) textPart = QHttpPart() textPart.setHeader(QNetworkRequest.ContentDispositionHeader, QVariant("form-data; name=\"driver\"")) textPart.setBody(QByteArray().append('Local')) filePart = QHttpPart() filePart.setHeader( QNetworkRequest.ContentDispositionHeader, QVariant("form-data; name=\"file\"; filename=\"{}\"".format( file_name))) file = QFile(data_url) file.open(QIODevice.ReadOnly) filePart.setBodyDevice(file) file.setParent( multiPart ) # we cannot delete the file now, so delete it with the multiPart multiPart.append(filePart) multiPart.append(textPart) service_url = '/'.join([ QSettings().value( 'Asistente-LADM_COL/sources/service_endpoint', DEFAULT_ENDPOINT_SOURCE_SERVICE), SOURCE_SERVICE_UPLOAD_SUFFIX ]) request = QNetworkRequest(QUrl(service_url)) reply = nam.post(request, multiPart) #reply.uploadProgress.connect(upload_dialog.update_current_progress) reply.error.connect(self.error_returned) multiPart.setParent(reply) # We'll block execution until we get response from the server loop = QEventLoop() reply.finished.connect(loop.quit) loop.exec_() response = reply.readAll() data = QTextStream(response, QIODevice.ReadOnly) content = data.readAll() if content is None: self.log.logMessage( "There was an error uploading file '{}'".format(data_url), PLUGIN_NAME, Qgis.Critical) upload_errors += 1 continue try: response = json.loads(content) except json.decoder.JSONDecodeError: self.log.logMessage( "Couldn't parse JSON response from server for file '{}'!!!" .format(data_url), PLUGIN_NAME, Qgis.Critical) upload_errors += 1 continue if 'error' in response: self.log.logMessage( "STATUS: {}. ERROR: {} MESSAGE: {} FILE: {}".format( response['status'], response['error'], response['message'], data_url), PLUGIN_NAME, Qgis.Critical) upload_errors += 1 continue reply.deleteLater() if 'url' not in response: self.log.logMessage( "'url' attribute not found in JSON response for file '{}'!" .format(data_url), PLUGIN_NAME, Qgis.Critical) upload_errors += 1 continue url = self.get_file_url(response['url']) new_values[feature.id()] = {field_index: url} count += 1 upload_dialog.update_total_progress(count) if not_found > 0: self.message_with_duration_emitted.emit( QCoreApplication.translate( "SourceHandler", "{} out of {} records {} not uploaded to the document repository because {} file path is NULL or it couldn't be found in the local disk!" ).format( not_found, total, QCoreApplication.translate("SourceHandler", "was") if not_found == 1 else QCoreApplication.translate( "SourceHandler", "were"), QCoreApplication.translate("SourceHandler", "its") if not_found == 1 else QCoreApplication.translate( "SourceHandler", "their")), Qgis.Info, 0) if len(new_values): self.message_with_duration_emitted.emit( QCoreApplication.translate( "SourceHandler", "{} out of {} files {} uploaded to the document repository and {} remote location stored in the database!" ).format( len(new_values), total, QCoreApplication.translate("SourceHandler", "was") if len(new_values) == 1 else QCoreApplication.translate( "SourceHandler", "were"), QCoreApplication.translate("SourceHandler", "its") if len(new_values) == 1 else QCoreApplication.translate( "SourceHandler", "their")), Qgis.Info, 0) if upload_errors: self.message_with_duration_emitted.emit( QCoreApplication.translate( "SourceHandler", "{} out of {} files could not be uploaded to the document repository because of upload errors! See log for details." ).format(upload_errors, total), Qgis.Info, 0) return new_values
def foreign_key_parent_tables(table_name, search_parent=True, filter_exp=None): """ Function that searches for foreign key references in the specified table. :param table_name: Name of the database table. :type table_name: str :param search_parent: Select True if table_name is the child and parent tables are to be retrieved, else child tables will be returned. :type search_parent: bool :param filter_exp: A regex expression to filter related table names. :type filter_exp: QRegExp :return: A list of tuples containing the local column name, foreign table name, corresponding foreign column name and constraint name. :rtype: list """ # Check if the view for listing foreign key references exists fk_ref_view = pg_table_exists("foreign_key_references") # Create if it does not exist if not fk_ref_view: script_path = PLUGIN_DIR + "/scripts/foreign_key_references.sql" script_file = QFile(script_path) if script_file.exists(): if not script_file.open(QIODevice.ReadOnly): return None reader = QTextStream(script_file) sql = reader.readAll() if sql: t = text(sql) _execute(t) else: return None if search_parent: ref_table = "foreign_table_name" search_table = "table_name" else: ref_table = "table_name" search_table = "foreign_table_name" # Fetch foreign key references sql = "SELECT column_name,{0},foreign_column_name, constraint_name FROM " \ "foreign_key_references where {1} =:tb_name".format(ref_table, search_table) t = text(sql) result = _execute(t, tb_name=table_name) fk_refs = [] for r in result: rel_table = r[ref_table] fk_ref = r["column_name"], rel_table, \ r["foreign_column_name"], r["constraint_name"] if not filter_exp is None: if filter_exp.indexIn(rel_table) >= 0: fk_refs.append(fk_ref) continue fk_refs.append(fk_ref) return fk_refs