Skip to content

Commit

Permalink
Merge pull request #530 from Gustry/lwc_versions_refactoring
Browse files Browse the repository at this point in the history
Review LwcVersions to nto have a hardcoded value in the plugin
  • Loading branch information
Gustry authored Dec 5, 2023
2 parents f8cfda0 + 84793ca commit 21a203c
Show file tree
Hide file tree
Showing 24 changed files with 139 additions and 105 deletions.
5 changes: 0 additions & 5 deletions lizmap/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
__email__ = '[email protected]'

try:
from lizmap.definitions.definitions import LwcVersions

# TODO, change this behavior. Use only the online JSON file.
DEFAULT_LWC_VERSION = LwcVersions.Lizmap_3_6

# noinspection PyPep8Naming
def classFactory(iface):
from lizmap.plugin import Lizmap
Expand Down
15 changes: 13 additions & 2 deletions lizmap/definitions/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,26 @@ def as_list():

@staticmethod
def latest():
# Latest is used in test by default
# As the plugin is not fetching the online JSON file, we still need to choose one LWC version
return LwcVersions.as_list()[-1]

@classmethod
def find(cls, version_string: str):
def find(cls, version_string: str, raise_exception: bool = False):
"""Return the LWC version for the given string."""
for lwc_version in cls.__members__.values():
if version_string.startswith(lwc_version.value):
return lwc_version
return None

if raise_exception:
raise Exception(
f'The version string "{version_string}" was not found in Python files. Developers, please add it. No '
f'stress, nothing in production ;-)'
)
else:
# For non developers, we just return the latest...
# Better than nothing
return LwcVersions.latest()


# Possible prefix before a stable release
Expand Down
17 changes: 7 additions & 10 deletions lizmap/dialogs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ def __init__(self, parent=None, is_dev_version=True):
self.project = QgsProject.instance()

self.is_dev_version = is_dev_version
self.navigation_menu_ok = None

self.label_lizmap_logo.setText('')
pixmap = QPixmap(resources_path('icons', 'logo.png'))
Expand Down Expand Up @@ -513,15 +514,10 @@ def current_server_info(self, info: ServerComboData):

def current_lwc_version(self) -> Optional[LwcVersions]:
""" Return the current LWC version from the server combobox. """
return self.metadata_to_lwc_version(self.current_server_info(ServerComboData.JsonMetadata.value))

@classmethod
def metadata_to_lwc_version(cls, metadata: dict) -> Optional[LwcVersions]:
""" Check in a metadata for the LWC version."""
if not metadata:
# When the server is not reachable
return None
return LwcVersions.find(metadata['info']['version'])
metadata = self.current_server_info(ServerComboData.JsonMetadata.value)
# In tests, we might not have metadata in the combobox
if metadata:
return LwcVersions.find(metadata['info']['version'])

def current_repository(self, role=RepositoryComboData.Id) -> str:
""" Fetch the current directory on the server if available. """
Expand All @@ -535,7 +531,7 @@ def tooltip_server_combo(self, index: int):
# This method must use itemData() as we are not the current selected server in the combobox.
url = self.server_combo.itemData(index, ServerComboData.ServerUrl.value)
metadata = self.server_combo.itemData(index, ServerComboData.JsonMetadata.value)
target_version = self.metadata_to_lwc_version(metadata)
target_version = LwcVersions.find(metadata['info']['version'])
if target_version:
target_version = target_version.value
else:
Expand Down Expand Up @@ -953,6 +949,7 @@ def save_settings(self):

def allow_navigation(self, allow_navigation: bool, message: str = ''):
""" Allow the navigation or not in the UI. """
self.navigation_menu_ok = allow_navigation
for i in range(1, self.mOptionsListWidget.count()):
item = self.mOptionsListWidget.item(i)
if allow_navigation:
Expand Down
5 changes: 3 additions & 2 deletions lizmap/forms/atlas_edition.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from qgis.core import QgsMapLayerProxyModel

from lizmap.definitions.atlas import AtlasDefinitions
from lizmap.definitions.definitions import LwcVersions
from lizmap.forms.base_edition_dialog import BaseEditionDialog
from lizmap.qgis_plugin_tools.tools.i18n import tr
from lizmap.qgis_plugin_tools.tools.resources import load_ui
Expand All @@ -17,8 +18,8 @@

class AtlasEditionDialog(BaseEditionDialog, CLASS):

def __init__(self, parent=None, unicity=None):
super().__init__(parent, unicity)
def __init__(self, parent=None, unicity=None, lwc_version: LwcVersions = None):
super().__init__(parent, unicity, lwc_version)
self.setupUi(self)
self.config = AtlasDefinitions()
self.config.add_layer_widget('layer', self.layer)
Expand Down
5 changes: 3 additions & 2 deletions lizmap/forms/attribute_table_edition.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
AttributeTableDefinitions,
layer_has_custom_attribute_table,
)
from lizmap.definitions.definitions import LwcVersions
from lizmap.forms.base_edition_dialog import BaseEditionDialog
from lizmap.qgis_plugin_tools.tools.i18n import tr
from lizmap.qgis_plugin_tools.tools.resources import load_ui
Expand All @@ -20,8 +21,8 @@

class AttributeTableEditionDialog(BaseEditionDialog, CLASS):

def __init__(self, parent=None, unicity=None):
super().__init__(parent, unicity)
def __init__(self, parent=None, unicity=None, lwc_version: LwcVersions = None):
super().__init__(parent, unicity, lwc_version)
self.setupUi(self)
self.config = AttributeTableDefinitions()
self.config.add_layer_widget('layerId', self.layer)
Expand Down
17 changes: 11 additions & 6 deletions lizmap/forms/base_edition_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
QPlainTextEdit,
)

from lizmap import DEFAULT_LWC_VERSION
from lizmap.definitions.base import InputType
from lizmap.definitions.definitions import LwcVersions, ServerComboData
from lizmap.definitions.online_help import online_lwc_help
Expand All @@ -35,7 +34,7 @@ class BaseEditionDialog(QDialog):

""" Class managing the edition form, either creation or editing. """

def __init__(self, parent: QDialog = None, unicity=None):
def __init__(self, parent: QDialog = None, unicity=None, lwc_version: LwcVersions = None):
""" Constructor. """
# parent is the main UI of the plugin
# noinspection PyArgumentList
Expand All @@ -44,6 +43,9 @@ def __init__(self, parent: QDialog = None, unicity=None):
self.config = None
self.unicity = unicity

# Should only be used in tests, otherwise, the version is coming from the parent.
self._version = lwc_version

# Most edition dialogs are based on a layer as input. Only the layout dialog is not.
self.layer = None
self.primary_key = None
Expand Down Expand Up @@ -153,12 +155,15 @@ def open_help(self):
# noinspection PyArgumentList
QDesktopServices.openUrl(online_lwc_help(self.config.help_path()))

def current_lwc_version(self) -> LwcVersions:
""" If a parent is defined, by default on production, return the version of the current selected server. """
if self.parent:
return self.parent.current_lwc_version()
return self._version

def version_lwc(self):
""" Make all colors about widgets if it is available or not. """
if self.parent:
current_version = self.parent.current_lwc_version()
else:
current_version = DEFAULT_LWC_VERSION
current_version = self.current_lwc_version()

# For labels in the UI files, which are not part of the definitions.
found = False
Expand Down
11 changes: 4 additions & 7 deletions lizmap/forms/dataviz_edition.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Dialog for dataviz edition."""

from qgis.core import QgsApplication, QgsMapLayerProxyModel, QgsSettings
from qgis.core import QgsApplication, QgsMapLayerProxyModel
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtGui import QColor, QIcon
from qgis.PyQt.QtWidgets import (
Expand All @@ -10,7 +10,6 @@
QTableWidgetItem,
)

from lizmap import DEFAULT_LWC_VERSION
from lizmap.definitions.base import InputType
from lizmap.definitions.dataviz import DatavizDefinitions, GraphType
from lizmap.definitions.definitions import LwcVersions
Expand All @@ -30,8 +29,8 @@

class DatavizEditionDialog(BaseEditionDialog, CLASS):

def __init__(self, parent=None, unicity=None):
super().__init__(parent, unicity)
def __init__(self, parent=None, unicity=None, lwc_version: LwcVersions = None):
super().__init__(parent, unicity, lwc_version)
self.setupUi(self)
self.parent = parent
self.config = DatavizDefinitions()
Expand Down Expand Up @@ -276,9 +275,7 @@ def check_trace_action(self):
item = self.traces.item(i, 0)
self.trace_combo.addItem(item.icon(), item.text(), i + 1)

version = QgsSettings().value(
'lizmap/lizmap_web_client_version', DEFAULT_LWC_VERSION.value, str)
version = LwcVersions(version)
version = self.current_lwc_version()

if version in [LwcVersions.Lizmap_3_1, LwcVersions.Lizmap_3_2, LwcVersions.Lizmap_3_3]:
if self.traces.rowCount() >= 2:
Expand Down
4 changes: 2 additions & 2 deletions lizmap/forms/edition_edition.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

class EditionLayerDialog(BaseEditionDialog, CLASS):

def __init__(self, parent=None, unicity=None):
super().__init__(parent, unicity)
def __init__(self, parent=None, unicity=None, lwc_version: LwcVersions = None):
super().__init__(parent, unicity, lwc_version)
self.setupUi(self)
self.config = EditionDefinitions()
self.config.add_layer_widget('layerId', self.layer)
Expand Down
6 changes: 3 additions & 3 deletions lizmap/forms/filter_by_form_edition.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from qgis.core import QgsFields, QgsMapLayerProxyModel, QgsProject
from qgis.PyQt.QtGui import QIcon

from lizmap import LwcVersions
from lizmap.definitions.definitions import LwcVersions
from lizmap.definitions.filter_by_form import FilterByFormDefinitions
from lizmap.forms.base_edition_dialog import BaseEditionDialog
from lizmap.qgis_plugin_tools.tools.i18n import tr
Expand All @@ -20,8 +20,8 @@

class FilterByFormEditionDialog(BaseEditionDialog, CLASS):

def __init__(self, parent=None, unicity=None):
super().__init__(parent, unicity)
def __init__(self, parent=None, unicity=None, lwc_version: LwcVersions = None):
super().__init__(parent, unicity, lwc_version)
self.setupUi(self)
self.config = FilterByFormDefinitions()
self.config.add_layer_widget('layerId', self.layer)
Expand Down
5 changes: 3 additions & 2 deletions lizmap/forms/filter_by_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from qgis.core import QgsMapLayerProxyModel

from lizmap.definitions.definitions import LwcVersions
from lizmap.definitions.filter_by_login import FilterByLoginDefinitions
from lizmap.forms.base_edition_dialog import BaseEditionDialog
from lizmap.qgis_plugin_tools.tools.i18n import tr
Expand All @@ -17,8 +18,8 @@

class FilterByLoginEditionDialog(BaseEditionDialog, CLASS):

def __init__(self, parent=None, unicity=None):
super().__init__(parent, unicity)
def __init__(self, parent=None, unicity=None, lwc_version: LwcVersions = None):
super().__init__(parent, unicity, lwc_version)
self.setupUi(self)
self.config = FilterByLoginDefinitions()
self.config.add_layer_widget('layerId', self.layer)
Expand Down
5 changes: 3 additions & 2 deletions lizmap/forms/filter_by_polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from qgis.core import QgsMapLayerProxyModel

from lizmap.definitions.definitions import LwcVersions
from lizmap.definitions.filter_by_polygon import (
FilterByPolygonDefinitions,
FilterMode,
Expand All @@ -20,8 +21,8 @@

class FilterByPolygonEditionDialog(BaseEditionDialog, CLASS):

def __init__(self, parent=None, unicity=None):
super().__init__(parent, unicity)
def __init__(self, parent=None, unicity=None, lwc_version: LwcVersions = None):
super().__init__(parent, unicity, lwc_version)
self.setupUi(self)
self.config = FilterByPolygonDefinitions()
self.config.add_layer_widget('layer', self.layer)
Expand Down
5 changes: 3 additions & 2 deletions lizmap/forms/layout_edition.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from qgis.PyQt.QtCore import Qt
from qgis.PyQt.QtGui import QIcon

from lizmap.definitions.definitions import LwcVersions
from lizmap.definitions.layouts import Dpi, FormatType, LayoutsDefinitions
from lizmap.forms.base_edition_dialog import BaseEditionDialog
from lizmap.qgis_plugin_tools.tools.i18n import tr
Expand All @@ -19,8 +20,8 @@

class LayoutEditionDialog(BaseEditionDialog, CLASS):

def __init__(self, parent=None, unicity=None):
super().__init__(parent, unicity)
def __init__(self, parent=None, unicity=None, lwc_version: LwcVersions = None):
super().__init__(parent, unicity, lwc_version)
self.setupUi(self)
self.parent = parent
self.config = LayoutsDefinitions()
Expand Down
5 changes: 3 additions & 2 deletions lizmap/forms/locate_layer_edition.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from qgis.core import QgsMapLayerProxyModel

from lizmap.definitions.definitions import LwcVersions
from lizmap.definitions.locate_by_layer import LocateByLayerDefinitions
from lizmap.forms.base_edition_dialog import BaseEditionDialog
from lizmap.qgis_plugin_tools.tools.i18n import tr
Expand All @@ -18,8 +19,8 @@

class LocateLayerEditionDialog(BaseEditionDialog, CLASS):

def __init__(self, parent=None, unicity=None):
super().__init__(parent, unicity)
def __init__(self, parent=None, unicity=None, lwc_version: LwcVersions = None):
super().__init__(parent, unicity, lwc_version)
self.setupUi(self)
self.config = LocateByLayerDefinitions()
self.config.add_layer_widget('layerId', self.layer)
Expand Down
5 changes: 3 additions & 2 deletions lizmap/forms/time_manager_edition.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
QgsProject,
)

from lizmap.definitions.definitions import LwcVersions
from lizmap.definitions.time_manager import TimeManagerDefinitions
from lizmap.forms.base_edition_dialog import BaseEditionDialog
from lizmap.qgis_plugin_tools.tools.i18n import tr
Expand All @@ -24,8 +25,8 @@

class TimeManagerEditionDialog(BaseEditionDialog, CLASS):

def __init__(self, parent=None, unicity=None):
super().__init__(parent, unicity)
def __init__(self, parent=None, unicity=None, version: LwcVersions = None):
super().__init__(parent, unicity, version)
self.setupUi(self)
self.config = TimeManagerDefinitions()
self.config.add_layer_widget('layerId', self.layer)
Expand Down
5 changes: 3 additions & 2 deletions lizmap/forms/tooltip_edition.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from qgis.core import QgsMapLayerProxyModel
from qgis.PyQt.QtGui import QColor

from lizmap.definitions.definitions import LwcVersions
from lizmap.definitions.tooltip import ToolTipDefinitions
from lizmap.forms.base_edition_dialog import BaseEditionDialog
from lizmap.qgis_plugin_tools.tools.i18n import tr
Expand All @@ -18,8 +19,8 @@

class ToolTipEditionDialog(BaseEditionDialog, CLASS):

def __init__(self, parent=None, unicity=None):
super().__init__(parent, unicity)
def __init__(self, parent=None, unicity=None, lwc_version: LwcVersions = None):
super().__init__(parent, unicity, lwc_version)
self.setupUi(self)
self.config = ToolTipDefinitions()
self.config.add_layer_widget('layerId', self.layer)
Expand Down
3 changes: 1 addition & 2 deletions lizmap/lizmap_api/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from qgis.core import QgsMapLayer, QgsProject

from lizmap import DEFAULT_LWC_VERSION
from lizmap.definitions.definitions import LwcVersions
from lizmap.qgis_plugin_tools.tools.i18n import tr
from lizmap.qgis_plugin_tools.tools.version import (
Expand Down Expand Up @@ -44,7 +43,7 @@ def __init__(self, project, fix_json=False):
}
metadata['lizmap_web_client_target_version'] = {
'wType': 'spinbox', 'type': 'integer',
'default': format_version_integer('{}.0'.format(DEFAULT_LWC_VERSION.value)),
'default': format_version_integer('{}.0'.format(LwcVersions.latest().value)),
}

# We want to translate some items, do not make this variable static
Expand Down
Loading

0 comments on commit 21a203c

Please sign in to comment.