From 3a99811170a57ed48184d5e2b32738e996f1b946 Mon Sep 17 00:00:00 2001 From: Lova ANDRIARIMALALA <43842786+Xpirix@users.noreply.github.com> Date: Mon, 27 Nov 2023 08:55:11 +0300 Subject: [PATCH] Making license file not required --- qgis-app/plugins/forms.py | 2 +- qgis-app/plugins/tests/test_validator.py | 41 ++++++++++++------------ qgis-app/plugins/validator.py | 7 ++-- qgis-app/plugins/views.py | 14 ++++++-- 4 files changed, 36 insertions(+), 28 deletions(-) diff --git a/qgis-app/plugins/forms.py b/qgis-app/plugins/forms.py index 8dabcf5f..600986de 100644 --- a/qgis-app/plugins/forms.py +++ b/qgis-app/plugins/forms.py @@ -180,7 +180,7 @@ def clean_package(self): """ package = self.cleaned_data.get("package") try: - self.cleaned_data.update(validator(package, plugin_is_new=True)) + self.cleaned_data.update(validator(package)) except ValidationError as e: msg = _( "There were errors reading plugin package (please check also your plugin's metadata)." diff --git a/qgis-app/plugins/tests/test_validator.py b/qgis-app/plugins/tests/test_validator.py index 4bf084d9..62414a9a 100644 --- a/qgis-app/plugins/tests/test_validator.py +++ b/qgis-app/plugins/tests/test_validator.py @@ -200,31 +200,32 @@ class TestLicenseValidator(TestCase): def setUp(self) -> None: plugin_without_license = os.path.join(TESTFILE_DIR, "plugin_without_license.zip_") - self.invalid_plugin = open(plugin_without_license, "rb") + self.plugin_package = open(plugin_without_license, "rb") def tearDown(self): - self.invalid_plugin.close() - - def test_new_plugin_without_license(self): - self.assertRaises( - ValidationError, - validator, - InMemoryUploadedFile( - self.invalid_plugin, - field_name="tempfile", - name="testfile.zip", - content_type="application/zip", - size=39889, - charset="utf8", - ), - plugin_is_new=True - ) - - def test_update_plugin_without_license(self): + self.plugin_package.close() + + # License file is just recommended for now + # def test_new_plugin_without_license(self): + # self.assertRaises( + # ValidationError, + # validator, + # InMemoryUploadedFile( + # self.plugin_package, + # field_name="tempfile", + # name="testfile.zip", + # content_type="application/zip", + # size=39889, + # charset="utf8", + # ), + # plugin_is_new=True + # ) + + def test_plugin_without_license(self): self.assertTrue( validator( InMemoryUploadedFile( - self.invalid_plugin, + self.plugin_package, field_name="tempfile", name="testfile.zip", content_type="application/zip", diff --git a/qgis-app/plugins/validator.py b/qgis-app/plugins/validator.py index 98f5ca54..5462ea5f 100644 --- a/qgis-app/plugins/validator.py +++ b/qgis-app/plugins/validator.py @@ -146,7 +146,7 @@ def _check_url_link(url: str, forbidden_url: str, metadata_attr: str) -> None: raise error_check_if_exist -def validator(package, plugin_is_new=False): +def validator(package): """ Analyzes a zipped file, returns metadata if success, False otherwise. If the new icon metadata is found, an inmemory file object is also returned @@ -331,10 +331,7 @@ def validator(package, plugin_is_new=False): # according to https://github.com/qgis/QGIS-Django/issues/38#issuecomment-1824010198 licensename = package_name + "/LICENSE" if licensename not in namelist: - if plugin_is_new: - raise ValidationError(_("Cannot find LICENSE in the plugin package. This file is required for a new plugin, please consider adding it to the plugin package.")) - else: - metadata.append(("license_recommended", "Yes")) + metadata.append(("license_recommended", "Yes")) zip.close() del zip diff --git a/qgis-app/plugins/views.py b/qgis-app/plugins/views.py index bcf8085a..6aa8b68d 100644 --- a/qgis-app/plugins/views.py +++ b/qgis-app/plugins/views.py @@ -468,6 +468,16 @@ def plugin_upload(request): fail_silently=True, ) + if form.cleaned_data.get("license_recommended"): + messages.warning( + request, + _( + "Cannot find LICENSE in the plugin package. This file is not required but recommended, please consider adding it to the plugin package." + ), + fail_silently=True, + ) + del form.cleaned_data["license_recommended"] + except (IntegrityError, ValidationError, DjangoUnicodeDecodeError) as e: connection.close() messages.error(request, e, fail_silently=True) @@ -961,7 +971,7 @@ def version_create(request, package_name): messages.warning( request, _( - "Cannot find LICENSE in the plugin package. This file is not required for updating the plugin but is recommended, please consider adding it to the plugin package." + "Cannot find LICENSE in the plugin package. This file is not required but recommended, please consider adding it to the plugin package." ), fail_silently=True, ) @@ -1017,7 +1027,7 @@ def version_update(request, package_name, version): messages.warning( request, _( - "Cannot find LICENSE in the plugin package. This file is not required for updating the plugin but is recommended, please consider adding it to the plugin package." + "Cannot find LICENSE in the plugin package. This file is not required but recommended, please consider adding it to the plugin package." ), fail_silently=True, )