|
5 | 5 | from urllib.error import URLError |
6 | 6 | import requests |
7 | 7 |
|
8 | | -from qgis.core import QgsApplication, QgsAuthMethodConfig, QgsBlockingNetworkRequest, QgsNetworkAccessManager |
| 8 | +from qgis.core import ( |
| 9 | + QgsApplication, |
| 10 | + QgsAuthMethodConfig, |
| 11 | + QgsBlockingNetworkRequest, |
| 12 | + QgsNetworkAccessManager, |
| 13 | + QgsExpressionContextUtils, |
| 14 | +) |
9 | 15 | from qgis.PyQt.QtCore import QSettings, QUrl |
10 | 16 | from qgis.PyQt.QtNetwork import QNetworkRequest |
11 | 17 |
|
12 | | -from .mergin.client import LoginType, MerginClient, AuthTokenExpiredError |
| 18 | +from .mergin.client import LoginType, MerginClient, AuthTokenExpiredError, ServerType |
13 | 19 | from .mergin.common import ClientError, LoginError |
14 | 20 |
|
15 | 21 | from .utils import MERGIN_URL, get_qgis_proxy_config, get_plugin_version |
@@ -325,7 +331,7 @@ def test_server_connection( |
325 | 331 | password: typing.Optional[str] = None, |
326 | 332 | use_sso: bool = False, |
327 | 333 | sso_email: typing.Optional[str] = None, |
328 | | -): |
| 334 | +) -> typing.Tuple[bool, str]: |
329 | 335 | """ |
330 | 336 | Test connection to Mergin Maps server. This includes check for valid server URL |
331 | 337 | and user credentials correctness. |
@@ -423,3 +429,47 @@ def sso_ask_for_email(url: str) -> typing.Tuple[bool, typing.Optional[str]]: |
423 | 429 | return True, None |
424 | 430 |
|
425 | 431 | return False, None |
| 432 | + |
| 433 | + |
| 434 | +def set_qgsexpressionscontext(url: str, mc: typing.Optional[MerginClient] = None): |
| 435 | + QgsExpressionContextUtils.setGlobalVariable("mergin_url", url) |
| 436 | + QgsExpressionContextUtils.setGlobalVariable("mm_url", url) |
| 437 | + if mc: |
| 438 | + # username can be username or email, so we fetch it from api |
| 439 | + user_info = mc.user_info() |
| 440 | + username = user_info["username"] |
| 441 | + user_email = user_info["email"] |
| 442 | + user_full_name = user_info["name"] |
| 443 | + settings = QSettings() |
| 444 | + settings.setValue("Mergin/username", username) |
| 445 | + settings.setValue("Mergin/user_email", user_email) |
| 446 | + settings.setValue("Mergin/full_name", user_full_name) |
| 447 | + QgsExpressionContextUtils.setGlobalVariable("mergin_username", username) |
| 448 | + QgsExpressionContextUtils.setGlobalVariable("mergin_user_email", user_email) |
| 449 | + QgsExpressionContextUtils.setGlobalVariable("mergin_full_name", user_full_name) |
| 450 | + QgsExpressionContextUtils.setGlobalVariable("mm_username", username) |
| 451 | + QgsExpressionContextUtils.setGlobalVariable("mm_user_email", user_email) |
| 452 | + QgsExpressionContextUtils.setGlobalVariable("mm_full_name", user_full_name) |
| 453 | + else: |
| 454 | + QgsExpressionContextUtils.removeGlobalVariable("mergin_username") |
| 455 | + QgsExpressionContextUtils.removeGlobalVariable("mergin_user_email") |
| 456 | + QgsExpressionContextUtils.removeGlobalVariable("mergin_full_name") |
| 457 | + QgsExpressionContextUtils.removeGlobalVariable("mm_username") |
| 458 | + QgsExpressionContextUtils.removeGlobalVariable("mm_user_email") |
| 459 | + QgsExpressionContextUtils.removeGlobalVariable("mm_full_name") |
| 460 | + |
| 461 | + |
| 462 | +def mergin_server_deprecated_version(url: str) -> bool: |
| 463 | + mc = MerginClient( |
| 464 | + url=url, |
| 465 | + auth_token=None, |
| 466 | + login=None, |
| 467 | + password=None, |
| 468 | + plugin_version=get_plugin_version(), |
| 469 | + proxy_config=get_qgis_proxy_config(url), |
| 470 | + ) |
| 471 | + |
| 472 | + if mc.server_type() == ServerType.OLD: |
| 473 | + return True |
| 474 | + |
| 475 | + return False |
0 commit comments