Skip to content

Commit

Permalink
Add a class to manage webdav and add some logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustry committed Jun 23, 2023
1 parent ff6c132 commit d30bf72
Show file tree
Hide file tree
Showing 10 changed files with 585 additions and 21 deletions.
1 change: 1 addition & 0 deletions .docker/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ echo 'Wait 10 seconds'
sleep 10
echo 'Installation of the plugin'
docker exec -t qgis sh -c "qgis_setup.sh lizmap"
docker exec -t qgis sh -c "pip install webdavclient3"
echo 'Containers are running'
7 changes: 7 additions & 0 deletions lizmap/definitions/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,10 @@ class ServerComboData(Enum):
JsonMetadata = Qt.UserRole + 2 # JSON from the server, raw
# LwcVersion = Qt.UserRole + 3 # Enum item with the LWC version
LwcBranchStatus = Qt.UserRole + 4 # Enum item about the release status at that time.


@unique
class RepositoryComboData(Enum):
""" The repository combobox. """
Id = Qt.UserRole # ID of the repository
Path = Qt.UserRole + 1 # Path on the server
16 changes: 12 additions & 4 deletions lizmap/dialogs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
except ModuleNotFoundError:
WEBKIT_AVAILABLE = False

from lizmap.definitions.definitions import LwcVersions, ServerComboData
from lizmap.definitions.definitions import (
LwcVersions,
RepositoryComboData,
ServerComboData,
)
from lizmap.qgis_plugin_tools.tools.i18n import tr
from lizmap.qgis_plugin_tools.tools.resources import load_ui, resources_path
from lizmap.qt_style_sheets import COMPLETE_STYLE_SHEET
Expand Down Expand Up @@ -174,12 +178,12 @@ def metadata_to_lwc_version(cls, metadata: dict) -> Optional[LwcVersions]:
return None
return LwcVersions.find(metadata['info']['version'])

def current_repository(self) -> str:
def current_repository(self, role=RepositoryComboData.Id) -> str:
""" Fetch the current directory on the server if available. """
if not self.repository_combo.isVisible():
return ''

return self.repository_combo.currentData()
return self.repository_combo.currentData(role.value)

def tooltip_server_combo(self, index: int):
""" Set the tooltip for a given row in the server combo. """
Expand Down Expand Up @@ -249,7 +253,11 @@ def refresh_combo_repositories(self):
for repository_id, repository_data in repositories.items():
self.repository_combo.addItem(repository_data['label'], repository_id)
index = self.repository_combo.findData(repository_id)
self.repository_combo.setItemData(index, repository_id, Qt.ToolTipRole)
self.repository_combo.setItemData(
index,
"ID : {}<br>Path : {}".format(repository_id, repository_data['path']),
Qt.ToolTipRole)
self.repository_combo.setItemData(index, repository_data['path'], RepositoryComboData.Path.value)

# Restore the previous value if possible
previous = self.project.customVariables().get('lizmap_repository')
Expand Down
128 changes: 116 additions & 12 deletions lizmap/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
QStorageInfo,
Qt,
QTranslator,
QUrl,
)
from qgis.PyQt.QtGui import (
QBrush,
Expand All @@ -58,6 +59,7 @@
QTreeWidgetItem,
QWidget,
)
from qgis.utils import OverrideCursor

from lizmap import DEFAULT_LWC_VERSION
from lizmap.definitions.atlas import AtlasDefinitions
Expand All @@ -68,6 +70,7 @@
LayerProperties,
LwcVersions,
ReleaseStatus,
RepositoryComboData,
ServerComboData,
)
from lizmap.definitions.edition import EditionDefinitions
Expand Down Expand Up @@ -146,6 +149,8 @@

if qgis_version() >= 31400:
from qgis.core import QgsProjectServerValidator
if qgis_version() >= 32200:
from lizmap.server_dav import WebDav

LOGGER = logging.getLogger(plugin_name())
VERSION_URL = 'https://raw.githubusercontent.com/3liz/lizmap-web-client/versions/versions.json'
Expand Down Expand Up @@ -199,6 +204,14 @@ def __init__(self, iface):
self.dlg.label_dev_version.setText(text)
self.dlg.label_dev_version.setVisible(True)

if Qgis.QGIS_VERSION_INT >= 32200:
self.webdav = WebDav()
# Give the dialog only the first time
self.webdav.setup_webdav_dialog(self.dlg)
else:
self.webdav = None
self.check_webdav()

self.layers_table = dict()

# List of ui widget for data driven actions and checking
Expand Down Expand Up @@ -701,6 +714,7 @@ def target_server_changed(self):
if current_version != old_version:
self.lwc_version_changed()
# self.dlg.check_qgis_version()
self.check_webdav()

# For deprecated features in LWC 3.7 about base layers
self.check_visibility_crs_3857()
Expand Down Expand Up @@ -782,6 +796,21 @@ def lwc_version_changed(self):
else:
self.dlg.qgis_and_lwc_versions_issue.setVisible(True)

def check_webdav(self):
""" Check if we can enable or the webdav, according to the current selected server. """
if not self.webdav:
# QGIS <= 3.22
self.dlg.send_webdav.setChecked(False)
self.dlg.send_webdav.setEnabled(False)
return

# The dialog is already given.
if self.webdav.setup_webdav_dialog():
self.dlg.send_webdav.setEnabled(True)
else:
self.dlg.send_webdav.setChecked(False)
self.dlg.send_webdav.setEnabled(False)

# noinspection PyPep8Naming
def initGui(self):
"""Create action that will start plugin configuration"""
Expand Down Expand Up @@ -2614,7 +2643,7 @@ def project_config_file(self, lwc_version: LwcVersions, with_gui: bool = True, c
pass
else:
# We do not want to save this table if it's less than LWC 3.7
LOGGER.info("Skipping the 'layout' table because version if less than LWC 3.7")
LOGGER.info("Skipping the 'layout' table because version is less than LWC 3.7")
continue

if manager.use_single_row() and manager.table.rowCount() == 1:
Expand Down Expand Up @@ -3127,29 +3156,104 @@ def save_cfg_file(
duration=30
)

if auto_save and self.dlg.checkbox_ftp_transfer.isChecked():
valid, message = self.server_ftp.connect(send_files=True)
if not valid:
if not auto_save:
# noinspection PyUnresolvedReferences
self.iface.messageBar().pushMessage(
'Lizmap',
msg,
level=Qgis.Success,
duration=3
)
# No automatic saving, the process is finished
return True

if not self.dlg.send_webdav.isChecked() and not self.dlg.checkbox_ftp_transfer.isChecked():
# No FTP and no webdav
return True

if self.dlg.send_webdav.isChecked():
with OverrideCursor(Qt.WaitCursor):
result, _, url = self.send_webdav()

if result:
# noinspection PyUnresolvedReferences
self.iface.messageBar().pushMessage(
'Lizmap',
message,
level=Qgis.Critical,
msg + '. ' + tr('Project <a href="{}">published !</a>'.format(url)),
level=Qgis.Success,
)
return False
return True

msg = tr(
'Lizmap configuration file has been updated and sent to the FTP {}.'
).format(self.server_ftp.host)
# Only deprecated FTP now
valid, message = self.server_ftp.connect(send_files=True)
if not valid:
# noinspection PyUnresolvedReferences
self.iface.messageBar().pushMessage(
'Lizmap',
message,
level=Qgis.Critical,
)
return False

# noinspection PyUnresolvedReferences
self.iface.messageBar().pushMessage(
'Lizmap',
msg,
tr(
'Lizmap configuration file has been updated and sent to the FTP {}.'
).format(self.server_ftp.host),
level=Qgis.Success,
duration=3
)
return True

def send_webdav(self) -> Tuple[bool, str, str]:
""" Sync the QGS and CFG file over the webdav. """
folder = self.dlg.current_repository(RepositoryComboData.Path)
if not folder:
# Maybe we are on a new server ?
return False, '', ''

qgis_exists, error = self.webdav.check_qgs_exist()
if error:
self.iface.messageBar().pushMessage('Lizmap', error, level=Qgis.Critical)
return False, '', ''

server = self.dlg.server_combo.currentData(ServerComboData.ServerUrl.value)
if not qgis_exists:
box = QMessageBox(self.dlg)
box.setIcon(QMessageBox.Question)
box.setWindowIcon(QIcon(resources_path('icons', 'icon.png')), )
box.setWindowTitle(tr('The project is not published yet'))
box.setText(tr(
'The project <b>"{}"</b> does not exist yet on the server <br>'
'<b>"{}"</b> '
'in the folder <b>"{}"</b>.'
'<br><br>'
'Do you want to publish it for the first time in this directory ?'
).format(
self.project.baseName(), server, folder))
box.setStandardButtons(QMessageBox.Yes | QMessageBox.No)
box.setDefaultButton(QMessageBox.No)
result = box.exec_()
if result == QMessageBox.No:
return False, '', ''

flag, error, url = self.webdav.send_qgs_and_cfg()
if not flag:
# Error while sending files
LOGGER.error(error)
self.iface.messageBar().pushMessage('Lizmap', error, level=Qgis.Critical)
return False, error, ''

LOGGER.debug("Webdav has been OK : {}".format(url))

if flag and qgis_exists:
# Everything went fine
return True, '', url

# Only the first time if the project didn't exist before
# noinspection PyArgumentList
QDesktopServices.openUrl(QUrl(url))
return True, '', url

def check_visibility_crs_3857(self):
""" Check if we display the warning about scales.
Expand Down
9 changes: 8 additions & 1 deletion lizmap/resources/ui/ui_lizmap.ui
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,9 @@ QListWidget::item::selected {
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="repository_combo"/>
</item>
<item>
<widget class="QCheckBox" name="checkbox_save_project">
<property name="toolTip">
Expand All @@ -201,7 +204,11 @@ QListWidget::item::selected {
</widget>
</item>
<item>
<widget class="QComboBox" name="repository_combo"/>
<widget class="QCheckBox" name="send_webdav">
<property name="text">
<string>Send with webdav if possible</string>
</property>
</widget>
</item>
</layout>
</widget>
Expand Down
Loading

0 comments on commit d30bf72

Please sign in to comment.