From e2d55cd8d44281500a45a4afdfe57e5dd37ba209 Mon Sep 17 00:00:00 2001 From: Even Rouault Date: Fri, 8 Nov 2024 00:11:05 +0100 Subject: [PATCH] Add validation for new plugin metadata keywords --- .github/workflows/test.yaml | 1 + dockerize/docker/REQUIREMENTS.txt | 2 ++ qgis-app/REQUIREMENTS_plugins.txt | 4 +++- qgis-app/plugins/validator.py | 34 +++++++++++++++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 24befe05..66328edd 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -62,6 +62,7 @@ jobs: run: | docker compose exec -T devweb bash -c ' set -e # Exit immediately if any command fails + pip install pycountry~=24.6 && python manage.py makemigrations && python manage.py migrate && python manage.py test diff --git a/dockerize/docker/REQUIREMENTS.txt b/dockerize/docker/REQUIREMENTS.txt index e3827683..126ea170 100644 --- a/dockerize/docker/REQUIREMENTS.txt +++ b/dockerize/docker/REQUIREMENTS.txt @@ -62,3 +62,5 @@ uwsgi~=2.0 freezegun~=1.4 sentry-sdk~=2.2 + +pycountry~=24.6 diff --git a/qgis-app/REQUIREMENTS_plugins.txt b/qgis-app/REQUIREMENTS_plugins.txt index be3c802d..126ea170 100644 --- a/qgis-app/REQUIREMENTS_plugins.txt +++ b/qgis-app/REQUIREMENTS_plugins.txt @@ -61,4 +61,6 @@ django-matomo==0.1.6 uwsgi~=2.0 freezegun~=1.4 -sentry-sdk~=2.2 \ No newline at end of file +sentry-sdk~=2.2 + +pycountry~=24.6 diff --git a/qgis-app/plugins/validator.py b/qgis-app/plugins/validator.py index a6a9f2b2..ccee39e2 100644 --- a/qgis-app/plugins/validator.py +++ b/qgis-app/plugins/validator.py @@ -4,8 +4,10 @@ """ import codecs import configparser +import locale import mimetypes import os +import pycountry import re import zipfile from io import StringIO @@ -46,6 +48,10 @@ "experimental", "external_deps", "server", + "payment", + "authentication", + "countries", + "languages", ), ) PLUGIN_BOOLEAN_METADATA = getattr( @@ -368,6 +374,34 @@ def validator(package): if not re.match(r"^[^/]+$", dict(metadata)["author"]): raise ValidationError(_("Author name cannot contain slashes.")) + # Check payment + if "payment" in dict(metadata): + value = dict(metadata)["payment"].strip().lower() + if value not in ("true", "1", "false", "0", "partial"): + raise ValidationError(_("payment should be one of True, False or Partial")) + + # Check authentication + if "authentication" in dict(metadata): + value = dict(metadata)["authentication"].strip().lower() + if value not in ("true", "1", "false", "0", "partial"): + raise ValidationError(_("authentication should be one of True, False or Partial")) + + # Check countries + if "countries" in dict(metadata): + values = dict(metadata)["countries"].strip().lower().split(',') + known_countries = [x.alpha_2.lower() for x in pycountry.countries] + for value in values: + if value not in known_countries: + raise ValidationError(_("Country %s is unknown. Accepted values are %s") % (value, ",".join(known_countries))) + + # Check languages + if "languages" in dict(metadata): + values = dict(metadata)["languages"].strip().split(',') + known_locales = [x[0] for x in locale.locale_alias.items()] + for value in values: + if value not in known_locales: + raise ValidationError(_("Language %s is unknown. Accepted values are %s") % (value, ",".join(known_locales))) + # strip and check checked_metadata = [] for k, v in metadata: