From d9f5d2ae6ef5fa7686408d9c0471d9e90d0f961f Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 12 Jun 2026 08:26:44 +0000 Subject: [PATCH 1/8] =?UTF-8?q?Oprava=20p=C3=A1d=C5=AF:=20chyb=C4=9Bj?= =?UTF-8?q?=C3=ADc=C3=AD=20locale=20p=C5=99i=20startu=20a=20=C5=BEivotn?= =?UTF-8?q?=C3=AD=20cyklus=20=C3=BAlohy=20aktualizace=20hesl=C3=A1=C5=99?= =?UTF-8?q?=C5=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - amcr_viewer.py: QSettings().value('locale/userLocale') může být None na čisté instalaci QGIS – ošetřen fallback na 'en', jinak plugin spadne na TypeError hned při načtení. - amcr_viewer.py: odstraněn nepoužívaný hvězdičkový import resources. - amcr_dialog.py: úloha aktualizace heslářů se drží v modulové referenci, aby GC neuklidil Python wrapper QgsTask před dokončením (známá příčina pádů QGIS). - amcr_dialog.py: tlačítko aktualizace se po spuštění zakáže – nelze už spustit několik stahování paralelně přepisujících heslar.csv. - amcr_dialog.py: závěrečné QMessageBoxy se parentují na hlavní okno QGIS místo dialogu, který může být v době dokončení úlohy už zavřený (smazaný C++ objekt → pád). --- amcr_viewer/amcr_dialog.py | 45 +++++++++++++++++++++++++++++--------- amcr_viewer/amcr_viewer.py | 6 ++--- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/amcr_viewer/amcr_dialog.py b/amcr_viewer/amcr_dialog.py index a91cc0a..2c3a154 100644 --- a/amcr_viewer/amcr_dialog.py +++ b/amcr_viewer/amcr_dialog.py @@ -7,6 +7,7 @@ from qgis.PyQt.QtWidgets import (QDialog, QVBoxLayout, from qgis.PyQt.QtCore import Qt, QSettings from qgis.core import (QgsTask, QgsApplication, QgsMessageLog, Qgis, QgsAuthMethodConfig) +from qgis.utils import iface from .amcr_codelists import (OBDOBI, TYP_AKCE, KRAJE, AREAL, ORGANIZACE, OKRESY, KATASTRY, VEDOUCI, PIAN_PRESNOST, TYP_LOKALITY, DRUH_LOKALITY, JISTOTA, @@ -14,6 +15,12 @@ from .amcr_codelists import (OBDOBI, TYP_AKCE, KRAJE, AREAL, ORGANIZACE, download_heslare, refresh_globals) +# Keep Python references to running tasks. QgsTaskManager only holds the +# C++ object; without a Python-side reference the wrapper can be garbage +# collected before the task finishes, which crashes QGIS. +_ACTIVE_TASKS = [] + + class UpdateCodelistsTask(QgsTask): def __init__(self, description): super().__init__(description, QgsTask.CanCancel) @@ -370,21 +377,38 @@ class AmcrFilterDialog(QDialog): return row_widget def action_update_heslare(self): - # Create the task instance + # Create the task instance and keep a reference so the Python + # wrapper survives until the task finishes task = UpdateCodelistsTask("Aktualizace heslářů AMČR") + _ACTIVE_TASKS.append(task) - # Re-enable the button regardless of the outcome - task.taskCompleted.connect(lambda: self.btn_update.setEnabled(True)) - task.taskTerminated.connect(lambda: self.btn_update.setEnabled(True)) + # Prevent parallel downloads overwriting heslar.csv + self.btn_update.setEnabled(False) - task.taskCompleted.connect(lambda: QMessageBox.information( - self, - "Hotovo", - "Hesláře byly úspěšně aktualizovány." - )) + # Message boxes are parented to the main window, not to this dialog – + # the dialog may already be closed (and its C++ object deleted) + # by the time the minute-long task finishes. + parent_win = iface.mainWindow() if iface else None + + def _cleanup(): + if task in _ACTIVE_TASKS: + _ACTIVE_TASKS.remove(task) + try: + self.btn_update.setEnabled(True) + except RuntimeError: + pass # dialog already closed + + def on_completed(): + _cleanup() + QMessageBox.information( + parent_win, + "Hotovo", + "Hesláře byly úspěšně aktualizovány." + ) # Show the exact error if the task fails def on_error(): + _cleanup() if task.exception: # This will show exactly what went wrong (e.g. PermissionError) msg = ( @@ -393,8 +417,9 @@ class AmcrFilterDialog(QDialog): ) else: msg = "Aktualizace byla zrušena uživatelem." - QMessageBox.warning(self, "Chyba / Zrušeno", msg) + QMessageBox.warning(parent_win, "Chyba / Zrušeno", msg) + task.taskCompleted.connect(on_completed) task.taskTerminated.connect(on_error) QgsApplication.taskManager().addTask(task) diff --git a/amcr_viewer/amcr_viewer.py b/amcr_viewer/amcr_viewer.py index ac1e44c..e28de5b 100644 --- a/amcr_viewer/amcr_viewer.py +++ b/amcr_viewer/amcr_viewer.py @@ -6,7 +6,6 @@ from qgis.core import Qgis from .amcr_tools import load_amcr_data, login_to_api from .amcr_dialog import AmcrFilterDialog, LoginDialog -from .resources import * import os.path @@ -24,8 +23,9 @@ class AmcrViewer: self.iface = iface self.plugin_dir = os.path.dirname(__file__) - # Determine the user's locale to load appropriate translation files - locale = QSettings().value('locale/userLocale')[0:2] + # Determine the user's locale to load appropriate translation files. + # The setting may be missing (None) on a fresh QGIS install. + locale = str(QSettings().value('locale/userLocale') or 'en')[0:2] locale_path = os.path.join( self.plugin_dir, 'i18n', From 444d1c4826dc68c40c02808220508195a5264640 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 12 Jun 2026 08:27:25 +0000 Subject: [PATCH 2/8] =?UTF-8?q?Oprava=20geometri=C3=AD=20a=20filtrov=C3=A1?= =?UTF-8?q?n=C3=AD=20PIAN?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fallback geometrie z geom_wkt (WGS-84) se nyní transformuje do S-JTSK; dříve se souřadnice ve stupních vkládaly přímo do vrstvy EPSG:5514 a prvek se vykreslil úplně mimo. - Nevalidní geometrie (např. samoprůniky polygonů) se před zahozením zkusí opravit přes makeValid(). - Filtr přesnosti PIAN normalizuje hodnotu pian_presnost (API ji může vracet jako jednoprvkový seznam) – dříve mohl tiše zahazovat záznamy. Stejná normalizace pro pian_typ kvůli překladu kódu. - Přístupy ke komponenta_areal/komponenta_obdobi a az_chranene_udaje ošetřeny proti hodnotě None (klíč existuje, ale je null). --- amcr_viewer/amcr_tools.py | 56 ++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/amcr_viewer/amcr_tools.py b/amcr_viewer/amcr_tools.py index 724e699..947431a 100644 --- a/amcr_viewer/amcr_tools.py +++ b/amcr_viewer/amcr_tools.py @@ -179,11 +179,12 @@ def tr_code(code): def komp_projde_filtrem(komp, filter_areal, filter_datace, filters): - areal_id = komp.get('komponenta_areal', {}).get('id', "") + # 'or {}' – the key may be present with a None value + areal_id = (komp.get('komponenta_areal') or {}).get('id', "") if filter_areal and areal_id not in filters.get('f_areal', []): return False - obdobi_id = komp.get('komponenta_obdobi', {}).get('id', "") + obdobi_id = (komp.get('komponenta_obdobi') or {}).get('id', "") if filter_datace and obdobi_id not in filters.get('f_obdobi', []): return False @@ -361,8 +362,8 @@ def load_amcr_data(canvas, bb, filters=None, actions_with_geom += 1 - # Extract protected fields - az_chranene = doc.get('az_chranene_udaje', {}) + # Extract protected fields ('or {}' – key may hold None) + az_chranene = doc.get('az_chranene_udaje') or {} chranene = ( doc.get('akce_chranene_udaje') or doc.get('lokalita_chranene_udaje') @@ -518,13 +519,13 @@ def load_amcr_data(canvas, bb, filters=None, 'ident_cely', "" ), - 'komponenta_areal': komp.get( - 'komponenta_areal', - {} + 'komponenta_areal': ( + komp.get('komponenta_areal') + or {} ).get('value', ""), - 'komponenta_obdobi': komp.get( - 'komponenta_obdobi', - {} + 'komponenta_obdobi': ( + komp.get('komponenta_obdobi') + or {} ).get('value', ""), } pian_lookup[dj_pian_value].append(komp_meta) @@ -713,6 +714,14 @@ def load_amcr_data(canvas, bb, filters=None, # Lists to hold features before batch-adding to layers feats_p, feats_l, feats_pt = [], [], [] + # Transform for PIANs that only provide WGS-84 geometry (geom_wkt) – + # the target layers are in S-JTSK (EPSG:5514) + xform_wgs_to_sjtsk = QgsCoordinateTransform( + QgsCoordinateReferenceSystem("EPSG:4326"), + QgsCoordinateReferenceSystem("EPSG:5514"), + QgsProject.instance() + ) + # --- FEATURE POPULATION --- for doc in docs_pian: try: @@ -733,25 +742,46 @@ def load_amcr_data(canvas, bb, filters=None, ) wkt = None + wkt_is_wgs = False if jdata.get('geom_sjtsk_wkt'): wkt = jdata.get('geom_sjtsk_wkt', {}).get('value') elif jdata.get('geom_wkt'): + # Fallback geometry is in WGS-84 and must be + # transformed to S-JTSK before use wkt = jdata.get('geom_wkt', {}).get('value') + wkt_is_wgs = True - pian_presnost = tr_code(str(doc.get('pian_presnost', ''))) - pian_typ = tr_code(str(doc.get('pian_typ', ''))) + # The API may return the value as a single-item list – + # normalize before comparing against filter codes + raw_presnost = doc.get('pian_presnost', '') + if isinstance(raw_presnost, list): + raw_presnost = raw_presnost[0] if raw_presnost else '' + raw_typ = doc.get('pian_typ', '') + if isinstance(raw_typ, list): + raw_typ = raw_typ[0] if raw_typ else '' + + pian_presnost = tr_code(str(raw_presnost)) + pian_typ = tr_code(str(raw_typ)) # Final precision filter check if ( filters and filters.get('f_pian_presnost') - and doc.get('pian_presnost') + and str(raw_presnost) not in filters.get('f_pian_presnost') ): continue if wkt: geom = QgsGeometry.fromWkt(wkt) + if geom.isNull(): + continue + if wkt_is_wgs: + geom.transform(xform_wgs_to_sjtsk) + if not geom.isGeosValid(): + # Try to repair (e.g. self-intersections) + # instead of silently dropping the feature + geom = geom.makeValid() if geom.isGeosValid(): t = geom.type() target_list = None From 46c09c4a09309be3c0e3afb611e6869032619a62 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 12 Jun 2026 08:28:40 +0000 Subject: [PATCH 3/8] =?UTF-8?q?Robustn=C4=9Bj=C5=A1=C3=AD=20s=C3=AD=C5=A5o?= =?UTF-8?q?v=C3=A1=20vrstva=20a=20stahov=C3=A1n=C3=AD=20dat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - login_to_api: ošetřena výjimka při parsování ne-JSON odpovědi (např. HTML chybová stránka za proxy) – dříve propadla až do QGIS. - _api_get přejmenováno na _api_get_json: tělo odpovědi se parsuje jen jednou (kontrola expirované session ho znovu využije), místo dvojího json() na každé dávce. - Stránkování porovnává počet stažených (ne unikátních) záznamů proti numFound – stránky plné duplicit už nevyvolávají zbytečné requesty. - Přidán re-entrancy guard: během běžícího stahování (processEvents pumpuje event loop) nelze spustit druhé stahování. - print() nahrazeno QgsMessageLog (záložka AMČR v panelu Zprávy). --- amcr_viewer/amcr_tools.py | 108 ++++++++++++++++++++++++++++---------- 1 file changed, 79 insertions(+), 29 deletions(-) diff --git a/amcr_viewer/amcr_tools.py b/amcr_viewer/amcr_tools.py index 947431a..cc484d2 100644 --- a/amcr_viewer/amcr_tools.py +++ b/amcr_viewer/amcr_tools.py @@ -17,6 +17,11 @@ TRANSLATIONS = {} # None = not logged in (anonymous access) AMCR_SESSION: requests.Session | None = None +# Re-entrancy guard: the download runs in the main thread and pumps the +# event loop via processEvents(), so the user could otherwise start +# a second download while the first one is still running +_LOADING = False + def _log(msg: str, level=Qgis.MessageLevel.Info): """ @@ -82,6 +87,12 @@ def login_to_api(username: str, password: str): except requests.exceptions.RequestException as e: _log(f"CHYBA sítě: {e}", Qgis.MessageLevel.Critical) return None + except ValueError: + # Server returned non-JSON (e.g. an HTML error page behind a proxy) + _log("CHYBA: server nevrátil platný JSON: " + f"{response.text[:300]}", + Qgis.MessageLevel.Critical) + return None def _get_session() -> requests.Session | None: @@ -104,33 +115,40 @@ def _get_session() -> requests.Session | None: return AMCR_SESSION -def _api_get(url, params, timeout=30) -> requests.Response: +def _api_get_json(url, params, timeout=30) -> dict: """ - Performs a GET request. If the API signals an expired login, - re-authenticates once and retries. + Performs a GET request and returns the parsed JSON body. + If the API signals an expired login, re-authenticates once and retries. + The body is parsed exactly once (the auth check reuses it). + Raises ValueError if the server does not return valid JSON. """ global AMCR_SESSION - def _is_auth_error(resp: requests.Response) -> bool: + def _is_auth_error(resp: requests.Response, body) -> bool: """The API returns auth errors with status 200 – the body must be checked.""" if resp.status_code == 401: return True - try: - body = resp.json() - err = str(body.get("error", "")).lower() - return ( - "unauthorized" in err - or "not logged" in err - or "session" in err - ) - except Exception: + if not isinstance(body, dict): return False + err = str(body.get("error", "")).lower() + return ( + "unauthorized" in err + or "not logged" in err + or "session" in err + ) + + def _parse(resp): + try: + return resp.json() + except ValueError: + return None session = _get_session() resp = (session or requests).get(url, params=params, timeout=timeout) + body = _parse(resp) - if _is_auth_error(resp): + if _is_auth_error(resp, body): _log("Session vypršela během stahování – obnovuji přihlášení...", Qgis.MessageLevel.Warning) AMCR_SESSION = None # Invalidate the old session @@ -140,6 +158,7 @@ def _api_get(url, params, timeout=30) -> requests.Response: AMCR_SESSION = login_to_api(username, password) if AMCR_SESSION: resp = AMCR_SESSION.get(url, params=params, timeout=timeout) + body = _parse(resp) else: _log("Opakované přihlášení selhalo.", Qgis.MessageLevel.Critical) @@ -147,7 +166,11 @@ def _api_get(url, params, timeout=30) -> requests.Response: _log("Přihlašovací údaje nejsou uloženy – pokračuji anonymně.", Qgis.MessageLevel.Warning) - return resp + if body is None: + raise ValueError( + f"API nevrátilo platný JSON (HTTP {resp.status_code})" + ) + return body def load_translations(): @@ -165,7 +188,10 @@ def load_translations(): if r.status_code == 200: TRANSLATIONS = r.json() except Exception as e: - print(f"Error downloading vocabulary: {e}") + QgsMessageLog.logMessage( + f"Error downloading vocabulary: {e}", + "AMČR", Qgis.MessageLevel.Warning + ) def tr_code(code): @@ -199,6 +225,16 @@ def load_amcr_data(canvas, bb, filters=None, 2. Fetches metadata and geometries from API 3. Creates QGIS memory layers and populates them with features """ + global _LOADING + if _LOADING: + iface.messageBar().pushMessage( + "AMCR", + "Stahování již probíhá, počkejte na jeho dokončení.", + level=Qgis.MessageLevel.Warning + ) + return + _LOADING = True + load_translations() # --- 1. COORDINATE TRANSFORMATION --- @@ -258,6 +294,7 @@ def load_amcr_data(canvas, bb, filters=None, MAX_LIMIT = 20000 # Safety limit to prevent QGIS from freezing seen_ids = set() + fetched_total = 0 # All downloaded records incl. duplicates target_pian_ids_count = 0 # Check if we should skip negative results based on filter @@ -280,8 +317,7 @@ def load_amcr_data(canvas, bb, filters=None, del base_params['page'] try: - resp_docs = _api_get(url, params=base_params, timeout=30) - resp_json = resp_docs.json() + resp_json = _api_get_json(url, params=base_params, timeout=30) data = resp_json.get('response', {}) batch_docs = data.get('docs', []) num_found = data.get('numFound', 0) @@ -289,6 +325,8 @@ def load_amcr_data(canvas, bb, filters=None, if not batch_docs: break + fetched_total += len(batch_docs) + # Filter out duplicates and append to main list new_docs = [] for d in batch_docs: @@ -298,12 +336,16 @@ def load_amcr_data(canvas, bb, filters=None, new_docs.append(d) docs.extend(new_docs) - print( + QgsMessageLog.logMessage( f"Strana {current_page} stažena. " - f"Celkem záznamů: {len(docs)} / {num_found}" + f"Celkem záznamů: {len(docs)} / {num_found}", + "AMČR", Qgis.MessageLevel.Info ) - if len(docs) >= num_found: + # Compare downloaded (not unique) records against numFound – + # pages full of duplicates would otherwise trigger + # needless extra requests + if fetched_total >= num_found: break if len(docs) >= MAX_LIMIT: iface.messageBar().pushMessage( @@ -317,7 +359,10 @@ def load_amcr_data(canvas, bb, filters=None, QApplication.processEvents() # Keep UI responsive except Exception as e: - print(f"Chyba při stránkování na straně {current_page}: {e}") + QgsMessageLog.logMessage( + f"Chyba při stránkování na straně {current_page}: {e}", + "AMČR", Qgis.MessageLevel.Warning + ) break if not docs: @@ -593,11 +638,13 @@ def load_amcr_data(canvas, bb, filters=None, } try: QApplication.processEvents() - r_pian = _api_get(url, params=params_pian, timeout=15) - batch_docs = r_pian.json().get('response', {}).get('docs', []) - docs_pian.extend(batch_docs) + r_json = _api_get_json(url, params=params_pian, timeout=15) + docs_pian.extend(r_json.get('response', {}).get('docs', [])) except Exception as e: - print(f"Chyba PIAN: {e}") + QgsMessageLog.logMessage( + f"Chyba PIAN: {e}", + "AMČR", Qgis.MessageLevel.Warning + ) # ========================================== # D) LAYER CREATION (QGIS Memory Layers) @@ -851,8 +898,10 @@ def load_amcr_data(canvas, bb, filters=None, target_list.append(feat) except Exception as ex: - print(f"Chyba při tvorbě feature: {ex}") - pass + QgsMessageLog.logMessage( + f"Chyba při tvorbě feature: {ex}", + "AMČR", Qgis.MessageLevel.Warning + ) # --- ADDING TO QGIS INTERFACE --- proj = QgsProject.instance() @@ -892,5 +941,6 @@ def load_amcr_data(canvas, bb, filters=None, level=Qgis.MessageLevel.Critical ) finally: - # Always restore cursor, even after failure + # Always restore cursor and release the guard, even after failure + _LOADING = False QApplication.restoreOverrideCursor() From 27e5fe02ac2477577e7e5d9b243ee3781771b20f Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 12 Jun 2026 08:28:56 +0000 Subject: [PATCH 4/8] =?UTF-8?q?Hesl=C3=A1=C5=99=20pristupnost:=20p=C5=99es?= =?UTF-8?q?ko=C4=8Den=C3=AD=20z=C3=A1znam=C5=AF=20bez=20platn=C3=A9ho=20k?= =?UTF-8?q?=C3=B3du?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pokud žádný titulek záznamu neprojde filtrem na jednopísmenný kód, next() vrátil None a do heslar.csv se zapsal prázdný/None kód, který se mohl dostat do API filtru jako hodnota 'None:or'. Takové záznamy se nyní přeskakují. --- amcr_viewer/amcr_codelists.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/amcr_viewer/amcr_codelists.py b/amcr_viewer/amcr_codelists.py index 93c0b34..4eb6965 100644 --- a/amcr_viewer/amcr_codelists.py +++ b/amcr_viewer/amcr_codelists.py @@ -158,6 +158,11 @@ def fetch_set(internal_name, api_set, task=None): ), None ) + # Skip records without a valid one-letter code – + # a None code would end up in the CSV and later + # in the API filter as the string "None" + if not kod: + continue dataset.append({ 'Název': nazev, From 64ec1ea7fd8ffb65e22f63cc9eebf0f81867ce0b Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 12 Jun 2026 08:29:27 +0000 Subject: [PATCH 5/8] Metadata, CI a dokumentace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - metadata.txt: icon ukazoval na neexistující download.png – plugin se ve správci zásuvných modulů zobrazoval bez ikony; nastaveno na akce.png. - Workflow: actions/checkout v2→v4 (v2 je deprecated, Node 12) a softprops/action-gh-release v1→v2. - README: poznámka, že knihovna requests nemusí být na Linuxu součástí systémové instalace QGIS. --- .github/workflows/release_plugin.yml | 4 ++-- README.md | 2 ++ amcr_viewer/metadata.txt | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.github/workflows/release_plugin.yml b/.github/workflows/release_plugin.yml index 10184c3..bf4d8fa 100644 --- a/.github/workflows/release_plugin.yml +++ b/.github/workflows/release_plugin.yml @@ -11,7 +11,7 @@ jobs: steps: # 1. Stáhne kód z repozitáře - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v4 # 2. Vytvoří ZIP (předpokládá, že kód je ve složce 'amcr_viewer') - name: Zip Plugin @@ -22,7 +22,7 @@ jobs: # 3. Nahraje ZIP k Releasu - name: Upload Release Asset - uses: softprops/action-gh-release@v1 + uses: softprops/action-gh-release@v2 if: startsWith(github.ref, 'refs/tags/') with: files: amcr_viewer.zip diff --git a/README.md b/README.md index f5477c3..a037274 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,8 @@ Layers are only created if the query returns features of the corresponding geome The plugin is developed in **Python 3** using the **PyQt6** framework for the GUI and the **Requests** library for HTTP communication. +> **Note:** The `requests` library is bundled with the QGIS installers for Windows and macOS. On Linux (distribution packages), it may need to be installed separately (e.g. `python3-requests`). + ### 4.1 File Structure * `amcr_viewer.py`: Entry point; handles GUI integration, toolbar/menu setup, and login flow. diff --git a/amcr_viewer/metadata.txt b/amcr_viewer/metadata.txt index 8343be2..124778e 100644 --- a/amcr_viewer/metadata.txt +++ b/amcr_viewer/metadata.txt @@ -47,7 +47,7 @@ tags=python,AMCR,AIS CR,archaeology,PIAN,AMČR,archeologie homepage=https://amcr-help.aiscr.cz/digiarchiv/qgis-viewer.html category=Vector -icon=download.png +icon=akce.png # experimental flag experimental=False From 93ed0ca810767f5eead7866b93064a85da589f8b Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 12 Jun 2026 10:32:37 +0000 Subject: [PATCH 6/8] =?UTF-8?q?Z=C5=99eteln=C3=A9=20hl=C3=A1=C5=A1en=C3=AD?= =?UTF-8?q?=20s=C3=AD=C5=A5ov=C3=BDch=20chyb=20b=C4=9Bhem=20stahov=C3=A1n?= =?UTF-8?q?=C3=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Při výpadku sítě uprostřed stahování se dosud chyba jen zapsala do logu: stránkování se tiše ukončilo s částečnými daty a smyčka PIAN zkoušela marně všechny zbývající dávky, takže uživatel skončil u hlášky 'Žádná data k zobrazení' bez vysvětlení. - Síťové chyby (requests.RequestException) se v obou smyčkách chytají zvlášť a stahování PIAN se po první z nich ihned ukončí. - Pokud selže už stahování metadat, zobrazí se červená lišta 'Stahování selhalo: chyba sítě'. - Pokud se stihla vykreslit část dat, zobrazí se varování, že výsledek je neúplný a je třeba stahování zopakovat. --- amcr_viewer/amcr_tools.py | 44 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/amcr_viewer/amcr_tools.py b/amcr_viewer/amcr_tools.py index cc484d2..e872cbf 100644 --- a/amcr_viewer/amcr_tools.py +++ b/amcr_viewer/amcr_tools.py @@ -308,6 +308,10 @@ def load_amcr_data(canvas, bb, filters=None, filter_areal = "f_areal" in filters if filters else False filter_datace = "f_obdobi" in filters if filters else False + # Set when a network error interrupts the download – the user + # gets an explicit error/warning instead of a silent partial result + network_error = False + # --- API PAGINATION LOOP --- while True: base_params['rows'] = BATCH_DOCS @@ -358,6 +362,14 @@ def load_amcr_data(canvas, bb, filters=None, current_page += 1 QApplication.processEvents() # Keep UI responsive + except requests.exceptions.RequestException as e: + network_error = True + QgsMessageLog.logMessage( + f"Chyba sítě při stránkování na straně " + f"{current_page}: {e}", + "AMČR", Qgis.MessageLevel.Critical + ) + break except Exception as e: QgsMessageLog.logMessage( f"Chyba při stránkování na straně {current_page}: {e}", @@ -365,6 +377,15 @@ def load_amcr_data(canvas, bb, filters=None, ) break + if network_error and not docs: + iface.messageBar().pushMessage( + "AMCR", + "Stahování selhalo: chyba sítě. " + "Zkontrolujte připojení k internetu.", + level=Qgis.MessageLevel.Critical + ) + return + if not docs: iface.messageBar().pushMessage( "AMCR", @@ -640,6 +661,15 @@ def load_amcr_data(canvas, bb, filters=None, QApplication.processEvents() r_json = _api_get_json(url, params=params_pian, timeout=15) docs_pian.extend(r_json.get('response', {}).get('docs', [])) + except requests.exceptions.RequestException as e: + # Network is down – stop immediately instead of + # uselessly retrying every remaining batch + network_error = True + QgsMessageLog.logMessage( + f"Chyba sítě při stahování geometrií PIAN: {e}", + "AMČR", Qgis.MessageLevel.Critical + ) + break except Exception as e: QgsMessageLog.logMessage( f"Chyba PIAN: {e}", @@ -920,7 +950,19 @@ def load_amcr_data(canvas, bb, filters=None, proj.addMapLayer(l) added += len(f) - if added > 0: + if network_error: + iface.messageBar().pushMessage( + "AMCR", + "Stahování bylo přerušeno chybou sítě – " + f"výsledek je neúplný (vykresleno {added} prvků). " + "Zkontrolujte připojení a spusťte stahování znovu.", + level=( + Qgis.MessageLevel.Warning + if added > 0 + else Qgis.MessageLevel.Critical + ) + ) + elif added > 0: iface.messageBar().pushMessage( "AMCR", f"Hotovo. Záznamů: {len(docs)} (s geom: {actions_with_geom}). " From fd11bee274815d4407fd2de19fe0e2bec0bc5bf0 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 12 Jun 2026 10:49:59 +0000 Subject: [PATCH 7/8] =?UTF-8?q?Ov=C4=9B=C5=99en=C3=AD=20p=C5=99ihla=C5=A1o?= =?UTF-8?q?vac=C3=ADch=20=C3=BAdaj=C5=AF=20p=C5=99ed=20ulo=C5=BEen=C3=ADm?= =?UTF-8?q?=20do=20spr=C3=A1vce=20autentizace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Špatné heslo zadané v přihlašovacím dialogu se dosud uložilo do QGIS Authentication Manageru a chyba se ukázala až po zavření dialogu. - Dialog nyní před uložením zkusí přihlášení k API; při neplatných údajích se nic neuloží a uživatel může údaje rovnou opravit. - Pokud je server nedostupný (údaje nelze ověřit), uživatel si může zvolit, zda je uložit neověřené – změna hesla na webu u dříve uložených údajů tím není dotčena. - login_to_api nově rozlišuje důvod selhání (LAST_LOGIN_ERROR: 'auth' × 'network'), aby dialog uměl oba případy odlišit. - Ověřuje se i změna samotného e-mailu (proti uloženému heslu). --- amcr_viewer/amcr_dialog.py | 43 ++++++++++++++++++++++++++++++++++++++ amcr_viewer/amcr_tools.py | 17 +++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/amcr_viewer/amcr_dialog.py b/amcr_viewer/amcr_dialog.py index 2c3a154..f109f65 100644 --- a/amcr_viewer/amcr_dialog.py +++ b/amcr_viewer/amcr_dialog.py @@ -627,6 +627,39 @@ class LoginDialog(QDialog): return True + def _verify_credentials(self, username: str, password: str) -> bool: + """ + Verify the credentials against the API before saving them. + Returns True if they should be stored: either the login succeeded, + or the server was unreachable and the user chose to keep them + unverified. Wrong credentials are never stored. + """ + # Lazy import to avoid an import cycle + # (amcr_tools imports LoginDialog lazily as well) + from . import amcr_tools + + if amcr_tools.login_to_api(username, password): + return True + + if amcr_tools.LAST_LOGIN_ERROR == 'network': + answer = QMessageBox.question( + self, + "Server nedostupný", + "Přihlašovací údaje se nepodařilo ověřit – server AMČR " + "je nedostupný.\nChcete je přesto uložit (neověřené)?", + QMessageBox.StandardButton.Yes + | QMessageBox.StandardButton.No + ) + return answer == QMessageBox.StandardButton.Yes + + QMessageBox.warning( + self, + "Neplatné přihlašovací údaje", + "Přihlášení se nezdařilo – zkontrolujte e-mail a heslo.\n" + "Údaje nebyly uloženy." + ) + return False + # ------------------------------------------------------------------ # Button actions # ------------------------------------------------------------------ @@ -653,6 +686,11 @@ class LoginDialog(QDialog): if ok: if not self._ensure_master_password(): return + # Verify the new username against the stored password + if not self._verify_credentials( + username, cfg.config("password", "") + ): + return cfg.setConfig("username", username) auth_mgr.updateAuthenticationConfig(cfg) self.accept() @@ -662,6 +700,11 @@ class LoginDialog(QDialog): QMessageBox.warning(self, "Chybí údaje", "Vyplňte prosím heslo.") return + # Verify before prompting for the master password – wrong + # credentials must never reach the Authentication Manager + if not self._verify_credentials(username, password): + return + if not self._ensure_master_password(): return diff --git a/amcr_viewer/amcr_tools.py b/amcr_viewer/amcr_tools.py index e872cbf..e9a22ea 100644 --- a/amcr_viewer/amcr_tools.py +++ b/amcr_viewer/amcr_tools.py @@ -17,6 +17,10 @@ TRANSLATIONS = {} # None = not logged in (anonymous access) AMCR_SESSION: requests.Session | None = None +# Reason of the last failed login: 'auth' (wrong credentials), +# 'network' (server unreachable / invalid response) or None +LAST_LOGIN_ERROR: str | None = None + # Re-entrancy guard: the download runs in the main thread and pumps the # event loop via processEvents(), so the user could otherwise start # a second download while the first one is still running @@ -38,6 +42,9 @@ def login_to_api(username: str, password: str): """ login_url = "https://digiarchiv.aiscr.cz/api/user/login" + global LAST_LOGIN_ERROR + LAST_LOGIN_ERROR = None + _log(f"Přihlašuji uživatele: '{username}'") if not username or not password: @@ -45,6 +52,7 @@ def login_to_api(username: str, password: str): "CHYBA: username nebo heslo je prázdné.", Qgis.MessageLevel.Critical ) + LAST_LOGIN_ERROR = 'auth' return None session = requests.Session() @@ -72,6 +80,7 @@ def login_to_api(username: str, password: str): f"CHYBA přihlášení (API): {body['error']}", Qgis.MessageLevel.Critical ) + LAST_LOGIN_ERROR = 'auth' return None _log("Přihlášení proběhlo úspěšně.") @@ -80,18 +89,22 @@ def login_to_api(username: str, password: str): return session except requests.exceptions.HTTPError as e: - _log(f"CHYBA HTTP {e.response.status_code if e.response else '?'}: " - f"{e.response.text[:300] if e.response else 'žádná odpověď'}", + status = e.response.status_code if e.response is not None else None + _log(f"CHYBA HTTP {status if status else '?'}: " + f"{e.response.text[:300] if e.response is not None else 'žádná odpověď'}", Qgis.MessageLevel.Critical) + LAST_LOGIN_ERROR = 'auth' if status in (401, 403) else 'network' return None except requests.exceptions.RequestException as e: _log(f"CHYBA sítě: {e}", Qgis.MessageLevel.Critical) + LAST_LOGIN_ERROR = 'network' return None except ValueError: # Server returned non-JSON (e.g. an HTML error page behind a proxy) _log("CHYBA: server nevrátil platný JSON: " f"{response.text[:300]}", Qgis.MessageLevel.Critical) + LAST_LOGIN_ERROR = 'network' return None From eebd7668a53efff75a0cd39b49978238678a5184 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 12 Jun 2026 11:45:55 +0000 Subject: [PATCH 8/8] =?UTF-8?q?Workflow:=20p=C5=99ipnut=C3=AD=20GitHub=20A?= =?UTF-8?q?ctions=20na=20commit=20hash=20+=20aktualizace=20verz=C3=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reakce na CodeQL upozornění 'Unpinned tag for a non-immutable Action'. Tag (v4/v2) může vlastník akce kdykoli přepsat na jiný commit; připnutí na hash chrání release pipeline před podvržením závislosti. Zároveň povýšeno na nejnovější verze: actions/checkout v6.0.3 a softprops/action-gh-release v3.0.0 (jen přechod na Node 24 runtime, bez změn API; hashe ověřeny přes git ls-remote). --- .github/workflows/release_plugin.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/release_plugin.yml b/.github/workflows/release_plugin.yml index bf4d8fa..c398381 100644 --- a/.github/workflows/release_plugin.yml +++ b/.github/workflows/release_plugin.yml @@ -11,7 +11,7 @@ jobs: steps: # 1. Stáhne kód z repozitáře - name: Checkout code - uses: actions/checkout@v4 + uses: actions/checkout@df4cb1c069e1874edd31b4311f1884172cec0e10 # v6.0.3 # 2. Vytvoří ZIP (předpokládá, že kód je ve složce 'amcr_viewer') - name: Zip Plugin @@ -22,7 +22,7 @@ jobs: # 3. Nahraje ZIP k Releasu - name: Upload Release Asset - uses: softprops/action-gh-release@v2 + uses: softprops/action-gh-release@b4309332981a82ec1c5618f44dd2e27cc8bfbfda # v3.0.0 if: startsWith(github.ref, 'refs/tags/') with: files: amcr_viewer.zip