Skip to content

Commit

Permalink
Add new blocking rule about trailing slashes, fix translations
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Dec 4, 2023
1 parent 6415ebe commit 29a9f9a
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* Add a link to PgService documentation on qgis.org website
* Add more CSS files from the server in the HTML Maptip preview dock
* Use a scroll widget for the settings panel
* Fix translations about checks/rules/explanations
* Add new blocker rule when the layer or group has a trailing space
* Fix some casting issue with SIP about the legend

## 4.0.1 - 2023-11-27

Expand Down
43 changes: 30 additions & 13 deletions lizmap/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
project_safeguards_checks,
project_trust_layer_metadata,
simplify_provider_side,
trailing_layer_group_name,
use_estimated_metadata,
)
from lizmap.saas import check_project_ssl_postgis, is_lizmap_cloud
Expand Down Expand Up @@ -174,7 +175,6 @@
)
from lizmap.tooltip import Tooltip
from lizmap.version_checker import VersionChecker
from lizmap.widgets.check_project import Checks, Error, Severities, SourceLayer

if qgis_version() >= 31400:
from qgis.core import QgsProjectServerValidator
Expand Down Expand Up @@ -2864,6 +2864,14 @@ def project_config_file(
self, lwc_version: LwcVersions, with_gui: bool = True, check_server=True, ignore_error=False
) -> Optional[Dict]:
""" Generate the CFG file with all options. """
# Import must be done after QTranslator
from lizmap.widgets.check_project import (
Checks,
Error,
Severities,
SourceLayer,
)

valid, _ = self.check_project_validity()

if with_gui:
Expand Down Expand Up @@ -2977,9 +2985,9 @@ def project_config_file(

self.dlg.check_results.add_error(
Error(
layer.layer_name,
layer.name,
error,
source_type=SourceLayer(layer.layer_name, layer.layer_id),
source_type=SourceLayer(layer.name, layer.layer_id),
),
lizmap_cloud=lizmap_cloud,
severity=severity,
Expand All @@ -3005,9 +3013,9 @@ def project_config_file(
for layer in error:
self.dlg.check_results.add_error(
Error(
layer.layer_name,
layer.name,
Checks.SSLConnection,
source_type=SourceLayer(layer.layer_name, layer.layer_id),
source_type=SourceLayer(layer.name, layer.layer_id),
)
)
self.dlg.enabled_ssl_button(True)
Expand All @@ -3016,20 +3024,24 @@ def project_config_file(
for layer in autogenerated_keys:
self.dlg.check_results.add_error(
Error(
layer.layer_name,
layer.name,
Checks.MissingPk,
source_type=SourceLayer(layer.layer_name, layer.layer_id),
source_type=SourceLayer(layer.name, layer.layer_id),
)
)
for layer in int8:
self.dlg.check_results.add_error(
Error(
layer.layer_name,
layer.name,
Checks.PkInt8,
source_type=SourceLayer(layer.layer_name, layer.layer_id),
source_type=SourceLayer(layer.name, layer.layer_id),
)
)

results = trailing_layer_group_name(self.project.layerTreeRoot(), self.project, [])
for result in results:
self.dlg.check_results.add_error(result)

if lwc_version >= LwcVersions.Lizmap_3_7:
text = duplicated_layer_with_filter(self.project)
if text:
Expand All @@ -3042,9 +3054,9 @@ def project_config_file(
for layer in results:
self.dlg.check_results.add_error(
Error(
layer.layer_name,
layer.name,
Checks.SimplifyGeometry,
source_type=SourceLayer(layer.layer_name, layer.layer_id),
source_type=SourceLayer(layer.name, layer.layer_id),
)
)
self.dlg.enabled_simplify_geom(True)
Expand All @@ -3053,9 +3065,9 @@ def project_config_file(
for layer in results:
self.dlg.check_results.add_error(
Error(
layer.layer_name,
layer.name,
Checks.EstimatedMetadata,
source_type=SourceLayer(layer.layer_name, layer.layer_id),
source_type=SourceLayer(layer.name, layer.layer_id),
)
)
self.dlg.enabled_estimated_md_button(True)
Expand Down Expand Up @@ -3467,6 +3479,9 @@ def check_project_validity(self):
if qgis_version() < 31400:
return None, None

# Import must be done after QTranslator
from lizmap.widgets.check_project import Checks, Error

validator = QgsProjectServerValidator()
valid, results = validator.validate(self.project)
LOGGER.info(f"Project has been detected : {'VALID' if valid else 'NOT valid'} according to OGC validation.")
Expand Down Expand Up @@ -3585,6 +3600,8 @@ def save_cfg_file(
Check the user defined data from GUI and save them to both global and project config files.
"""
from lizmap.widgets.check_project import Checks, Severities

server_metadata = self.dlg.server_combo.currentData(ServerComboData.JsonMetadata.value)
self.dlg.check_results.truncate()
beginner_mode = QgsSettings().value(Settings.key(Settings.BeginnerMode), True, bool)
Expand Down
32 changes: 30 additions & 2 deletions lizmap/project_checker_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from qgis.core import (
QgsDataSourceUri,
QgsLayerTree,
QgsLayerTreeNode,
QgsMapLayer,
QgsProject,
QgsProviderRegistry,
Expand All @@ -19,8 +20,13 @@

from lizmap.definitions.lizmap_cloud import CLOUD_DOMAIN
from lizmap.qgis_plugin_tools.tools.i18n import tr
from lizmap.tools import cast_to_group, is_vector_pg, update_uri
from lizmap.widgets.check_project import Checks, SourceLayer
from lizmap.tools import cast_to_group, cast_to_layer, is_vector_pg, update_uri
from lizmap.widgets.check_project import (
Checks,
Error,
SourceGroup,
SourceLayer,
)

""" Some checks which can be done on a layer. """

Expand Down Expand Up @@ -316,3 +322,25 @@ def project_trust_layer_metadata(project: QgsProject, fix: bool = False) -> bool

project.setTrustLayerMetadata(True)
return True


def trailing_layer_group_name(layer_tree: QgsLayerTreeNode, project, results: List) -> List:
""" Check for a trailing space in layer or group name. """
for child in layer_tree.children():
# noinspection PyArgumentList
if QgsLayerTree.isLayer(child):
child = cast_to_layer(child)
layer = project.mapLayer(child.layerId())
if layer.name().strip() != layer.name():
results.append(
Error(layer.name(), Checks.TrailingSpaceLayerGroupName, SourceLayer(layer.name(), layer.id())))
else:
child = cast_to_group(child)
if child.name().strip() != child.name():
results.append(
Error(child.name(), Checks.TrailingSpaceLayerGroupName, SourceGroup))

# Recursive call
results = trailing_layer_group_name(child, project, results)

return results
2 changes: 1 addition & 1 deletion lizmap/resources/ui/ui_lizmap.ui
Original file line number Diff line number Diff line change
Expand Up @@ -4658,7 +4658,7 @@ This is different to the map maximum extent (defined in QGIS project properties,
<item>
<widget class="QLabel" name="label_85">
<property name="text">
<string>Some checks might be blocking the CFG file generation. To fix, either use the tooltip in the last column, or check the documentation in the next tab for all errors which can be reported.</string>
<string>Some checks might be blocking the Lizmap configuration file generation. To fix, either use the tooltip (by overing your mouse pointer on the table row) in the last column, or check the documentation in the next tab for all errors which can be reported.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
Expand Down
24 changes: 24 additions & 0 deletions lizmap/test/test_project_checker_tools.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unittest

from qgis.core import QgsProject, QgsVectorLayer

from lizmap.project_checker_tools import trailing_layer_group_name
from lizmap.widgets.check_project import Error

__copyright__ = 'Copyright 2023, 3Liz'
__license__ = 'GPL version 3'
__email__ = '[email protected]'


class TestProjectTable(unittest.TestCase):

def test_trailing_spaces(self):
""" Test about trailing spaces in the table. """
layer = QgsVectorLayer('None?field=field_a:string', ' table', 'memory')
project = QgsProject()
project.addMapLayer(layer)

results = trailing_layer_group_name(project.layerTreeRoot(), project, [])
self.assertEqual(1, len(results))
self.assertIsInstance(results[0], Error)
self.assertEqual(' table', results[0].source)
4 changes: 4 additions & 0 deletions lizmap/test/test_table_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

from lizmap.widgets.check_project import Checks, Error, TableCheck

__copyright__ = 'Copyright 2023, 3Liz'
__license__ = 'GPL version 3'
__email__ = '[email protected]'


class TestProjectTable(unittest.TestCase):

Expand Down
42 changes: 38 additions & 4 deletions lizmap/widgets/check_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,23 @@ class Checks(Check, Enum):
Severities.Blocking if qgis_version() >= 32200 else Severities.Important,
QIcon(':/images/themes/default/mIconQgsProjectFile.svg'),
)
TrailingSpaceLayerGroupName = (
'trailing_space_group_name',
tr('Trailing space in layer/group name'),
tr(
'The layer/group name has some trailing spaces. It must be removed and the configuration in the plugin '
'might be needed.'
), (
'<ul>'
'<li>{edit_layer}</li>'
'</ul>'.format(
edit_layer=tr('Rename your layer/group to remove trailing spaces (left and right)'),
)
),
Levels.Layer,
Severities.Blocking,
QIcon(':/images/themes/default/algorithms/mAlgorithmMergeLayers.svg'),
)
PreventEcw = (
Settings.PreventEcw,
tr('ECW raster'),
Expand Down Expand Up @@ -586,19 +603,33 @@ def html(cls, severity: Severity, lizmap_cloud: bool) -> str:
return html_str


class SourceLayer:
class Source:

def __init__(self, name):
self.name = name


class SourceLayer(Source):

""" For identifying a layer in a project. """
def __init__(self, layer_name, layer_id):
def __init__(self, name, layer_id):
super().__init__(name)
self.layer_id = layer_id
self.layer_name = layer_name


class SourceGroup(Source):

""" For identifying a group in a project. """
def __init__(self, name):
super().__init__(name)


class SourceType:

""" List of sources in the project. """

Layer = SourceLayer
Groupe = SourceGroup


class Error:
Expand Down Expand Up @@ -743,8 +774,11 @@ def add_error(self, error: Error, lizmap_cloud: bool = False, severity=None):
layer = QgsProject.instance().mapLayer(error.source_type.layer_id)
item.setIcon(QgsMapLayerModel.iconForLayer(layer))
item.setData(self.JSON, error.source_type.layer_id)
elif isinstance(error.source_type, SourceType.Groupe):
item.setToolTip(error.source_type.name)
item.setIcon(QIcon(":images/themes/default/mActionFolder.svg"))
item.setData(self.JSON, error.source_type.name)
else:
# Project only for now
# TODO fix else
item.setData(self.JSON, error.source)
self.setItem(row, column, item)
Expand Down

0 comments on commit 29a9f9a

Please sign in to comment.