Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for generic version label parameter in plugins xml #495

Merged
merged 2 commits into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
matrix:
python-version:
- '3.7'
- '3.12'
steps:

- name: Set up Python ${{ matrix.python-version }}
Expand Down
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'
Comment on lines +75 to +78
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A cleaner way and then instead of using re.search(r'QGIS Version \d+\|Visit .+ version (\d+\.\d+)', content), would be to switch to the JSON equivalent file :

https://version.qgis.org/version.json

It's way less prone to error of re.match, would you like to the do the change @Xpirix ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point @Gustry , I was not aware of this file before. Thank you.

The version.json just requires a small fix (qgis/QGIS-Website#513) so we can directly use the version attribute as it currently shows <no_value>.

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
Loading