Skip to content

Commit

Permalink
Add support for generic version label parameter in plugins xml
Browse files Browse the repository at this point in the history
  • Loading branch information
Xpirix committed Dec 18, 2024
1 parent d6df3ed commit c4eab6b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
35 changes: 34 additions & 1 deletion qgis-app/plugins/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
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')
7 changes: 6 additions & 1 deletion qgis-app/plugins/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c4eab6b

Please sign in to comment.