From c4eab6bddd71971eeb30e454a0ca57b4fc92da2f Mon Sep 17 00:00:00 2001 From: Lova Andriarimalala <43842786+Xpirix@users.noreply.github.com> Date: Wed, 18 Dec 2024 09:24:35 +0300 Subject: [PATCH] Add support for generic version label parameter in plugins xml --- qgis-app/plugins/utils.py | 35 ++++++++++++++++++++++++++++++++++- qgis-app/plugins/views.py | 7 ++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/qgis-app/plugins/utils.py b/qgis-app/plugins/utils.py index 521a5797..546f653e 100644 --- a/qgis-app/plugins/utils.py +++ b/qgis-app/plugins/utils.py @@ -55,4 +55,37 @@ def parse_remote_addr(request: HttpRequest) -> str: x_forwarded_for = request.headers.get("X-Forwarded-For", "") if x_forwarded_for: return x_forwarded_for.split(",")[0] - return request.META.get("REMOTE_ADDR", "") \ No newline at end of file + return request.META.get("REMOTE_ADDR", "") + +def get_version_from_label(param): + """ + Fetches the QGIS version based on the given parameter. + + Args: + param (str): The parameter to determine which version to fetch. + Accepts 'ltr', 'stable', or 'latest'. + + Returns: + str: The major and minor version of QGIS. + + Raises: + ValueError: If the parameter value is invalid. + Exception: If the request to the QGIS version service fails or the version is not found. + """ + if param.lower() in ['ltr', 'stable']: + url = 'https://version.qgis.org/version-ltr.txt' + elif param.lower() == 'latest': + url = 'https://version.qgis.org/version.txt' + else: + raise ValueError('Invalid parameter value') + + response = requests.get(url) + if response.status_code != 200: + raise Exception('Request failed') + + content = response.text + match = re.search(r'QGIS Version \d+\|Visit .+ version (\d+\.\d+)', content) + if match: + return match.group(1) + else: + raise Exception('Version not found in response') \ No newline at end of file diff --git a/qgis-app/plugins/views.py b/qgis-app/plugins/views.py index 37980558..69eb394d 100644 --- a/qgis-app/plugins/views.py +++ b/qgis-app/plugins/views.py @@ -36,7 +36,7 @@ from plugins.models import Plugin, PluginOutstandingToken, PluginVersion, PluginVersionDownload, vjust from plugins.validator import PLUGIN_REQUIRED_METADATA from django.contrib.gis.geoip2 import GeoIP2 -from plugins.utils import parse_remote_addr +from plugins.utils import parse_remote_addr, get_version_from_label from rest_framework_simplejwt.token_blacklist.models import OutstandingToken from rest_framework_simplejwt.tokens import RefreshToken, api_settings @@ -1630,6 +1630,11 @@ def xml_plugins(request, qg_version=None, stable_only=None, package_name=None): """ request_version = request.GET.get("qgis", "1.8.0") + if request_version in ["latest", "ltr", "stable"]: + numbered_version = get_version_from_label(request_version) + # Redirect to this view using the numbered version because + # the xml file is cached with that. + return HttpResponseRedirect(reverse("xml_plugins") + f"?qgis={numbered_version}") version_level = len(str(request_version).split('.')) - 1 qg_version = ( qg_version