Skip to content

Commit

Permalink
Ajout de la variable netads_prefix_parcelle au projet
Browse files Browse the repository at this point in the history
  • Loading branch information
René-Luc D'Hont authored and Gustry committed Oct 23, 2023
1 parent 4a0ee1d commit 8a08c22
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 17 deletions.
86 changes: 71 additions & 15 deletions netads/processing_netads/data/load_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
from netads.processing_netads.data.base import BaseDataAlgorithm


class Key:
Code = "netads_idclient"
Prefix = "netads_prefix_parcelle"


class LoadLayersAlgorithm(BaseDataAlgorithm):
"""
Chargement des couches netads depuis la base de données
Expand All @@ -28,10 +33,9 @@ class LoadLayersAlgorithm(BaseDataAlgorithm):
CONNECTION_NAME = "CONNECTION_NAME"
SCHEMA = "SCHEMA"
CODE_CLIENT = "CODE_CLIENT"
PREFIX_PARCELLE = "PREFIX_PARCELLE"
OUTPUT = "OUTPUT"

KEY = "netads_idclient"

def name(self):
return "load_layers"

Expand All @@ -42,8 +46,8 @@ def shortHelpString(self):
return (
"Charger les couches de la base de données."
"<br>"
f"Le code client est obligatoire si il n'est pas fourni dans le projet dans une variable de "
f"projet '{self.KEY}'."
"Le code client est obligatoire si il n'est pas fourni dans"
f" le projet dans une variable de projet '{self.KEY}'."
)

def initAlgorithm(self, config: Dict):
Expand Down Expand Up @@ -82,6 +86,17 @@ def initAlgorithm(self, config: Dict):
param.setHelp("Code client NetADS attribué à la collectivité")
self.addParameter(param)

param = QgsProcessingParameterString(
self.PREFIX_PARCELLE,
"Préfixe parcellaire",
optional=True,
)
param.setHelp(
"Code départemental (2 caractères) et "
"code de direction (1 caractère)"
)
self.addParameter(param)

self.addOutput(
QgsProcessingOutputMultipleLayers(self.OUTPUT, "Couches de sortie")
)
Expand All @@ -97,23 +112,64 @@ def processAlgorithm(
)
schema = self.parameterAsSchema(parameters, self.SCHEMA, context)

code_client = self.parameterAsString(parameters, self.CODE_CLIENT, context)
if code_client:
QgsExpressionContextUtils.setProjectVariable(context.project(), self.KEY, code_client)
feedback.pushInfo(
f"Ajout du code client NetADS {code_client} dans la variable du projet '{self.KEY}'."
)
else:
# The input was empty so the variable must be in the project already
code_client = self.parameterAsString(
parameters,
self.CODE_CLIENT,
context
)
prefix_parcelle = self.parameterAsString(
parameters,
self.PREFIX_PARCELLE,
context
)

# Check parameters and raise errors
if not code_client:
# The input was empty so the variable must be in
# the project already
variables = context.project().customVariables()
url = variables.get(self.KEY)
url = variables.get(Key.Code)
if not url:
# The virtual field needs this variable on runtime.
raise QgsProcessingException(
f"Votre projet ne contient pas la variable {self.KEY}, vous devez donc renseigner la "
f"valeur pour l'identifiant client NetADS."
f"Votre projet ne contient pas la variable {Key.Code}, "
"vous devez donc renseigner la "
"valeur pour l'identifiant client NetADS."
)

if prefix_parcelle and len(prefix_parcelle) != 3:
# The virtual field needs this variable on runtime.
raise QgsProcessingException(
"Le préfixe parcellaire doit contenir 3 caractères : "
"le code département (2 caractères) + "
"le code de direction (1 caractère). "
f"Le préfixe parcellaire proposé `{prefix_parcelle}` est "
f"de longueur {len(prefix_parcelle)}."
)

# Then set variables
if code_client:
QgsExpressionContextUtils.setProjectVariable(
context.project(),
Key.Code,
code_client
)
feedback.pushInfo(
f"Ajout du code client NetADS {code_client} dans "
f"la variable du projet '{Key.Code}'."
)

if prefix_parcelle:
QgsExpressionContextUtils.setProjectVariable(
context.project(),
Key.Prefix,
prefix_parcelle
)
feedback.pushInfo(
f"Ajout du préfixe parcellaire {prefix_parcelle} dans "
f"la variable du projet '{Key.Prefix}'."
)

feedback.pushInfo("## Connexion à la base de données ##")

metadata = QgsProviderRegistry.instance().providerMetadata("postgres")
Expand Down
129 changes: 127 additions & 2 deletions netads/tests/test_load_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from qgis.core import QgsProcessingContext, QgsProcessingException, QgsProject

from netads.processing_netads.data.load_layers import Key

from netads.tests.base_database import DatabaseTestCase
from netads.tests.feedbacks import FeedbackPrint

Expand All @@ -16,7 +18,8 @@ def test_import_layers(self):
"""Test import layers."""
project = QgsProject()
variables = project.customVariables()
self.assertNotIn("netads_idclient", list(variables.keys()))
self.assertNotIn(Key.Code, list(variables.keys()))
self.assertNotIn(Key.Prefix, list(variables.keys()))
context = QgsProcessingContext()
context.setProject(project)

Expand All @@ -31,6 +34,7 @@ def test_import_layers(self):
processing.run(alg, params, feedback=FeedbackPrint(), context=context)

params["CODE_CLIENT"] = "test"
params["PREFIX_PARCELLE"] = "140"

results = processing.run(
alg,
Expand All @@ -40,10 +44,131 @@ def test_import_layers(self):
)

variables = project.customVariables()
self.assertEqual(variables["netads_idclient"], "test")
self.assertEqual(variables[Key.Code], "test")
self.assertEqual(variables[Key.Prefix], "140")

self.assertEqual(3, len(results["OUTPUT"]))

def test_import_layers_no_prefix(self):
"""Test import layers."""
project = QgsProject()
variables = project.customVariables()
self.assertNotIn(Key.Code, list(variables.keys()))
self.assertNotIn(Key.Prefix, list(variables.keys()))
context = QgsProcessingContext()
context.setProject(project)

params = {
"CONNECTION_NAME": "test_database",
"SCHEMA": "netads",
}
alg = "netads:load_layers"

with self.assertRaises(QgsProcessingException):
# Code client is not correct
processing.run(alg, params, feedback=FeedbackPrint(), context=context)

params["CODE_CLIENT"] = "test"

results = processing.run(
alg,
params,
feedback=FeedbackPrint(),
context=context,
)

variables = project.customVariables()
self.assertEqual(variables[Key.Code], "test")
self.assertNotIn(Key.Prefix, list(variables.keys()))

self.assertEqual(3, len(results["OUTPUT"]))

def test_import_layers_error_prefix(self):
"""Test import layers."""
project = QgsProject()
variables = project.customVariables()
self.assertNotIn(Key.Code, list(variables.keys()))
self.assertNotIn(Key.Prefix, list(variables.keys()))
context = QgsProcessingContext()
context.setProject(project)

params = {
"CONNECTION_NAME": "test_database",
"SCHEMA": "netads",
}
alg = "netads:load_layers"

with self.assertRaises(QgsProcessingException):
# Code client is not correct
processing.run(alg, params, feedback=FeedbackPrint(), context=context)

params["CODE_CLIENT"] = "test"
# Too short
params["PREFIX_PARCELLE"] = "14"

self.assertRaises(
QgsProcessingException,
processing.run,
alg,
params,
feedback=FeedbackPrint(),
context=context,
)

variables = project.customVariables()
self.assertNotIn(Key.Code, list(variables.keys()))
self.assertNotIn(Key.Prefix, list(variables.keys()))

# Too long
params["PREFIX_PARCELLE"] = "test"

self.assertRaises(
QgsProcessingException,
processing.run,
alg,
params,
feedback=FeedbackPrint(),
context=context,
)

variables = project.customVariables()
self.assertNotIn(Key.Code, list(variables.keys()))
self.assertNotIn(Key.Prefix, list(variables.keys()))

def test_import_layers_code_client_mandatory(self):
"""Test import layers."""
project = QgsProject()
variables = project.customVariables()
self.assertNotIn(Key.Code, list(variables.keys()))
self.assertNotIn(Key.Prefix, list(variables.keys()))
context = QgsProcessingContext()
context.setProject(project)

params = {
"CONNECTION_NAME": "test_database",
"SCHEMA": "netads",
}
alg = "netads:load_layers"

with self.assertRaises(QgsProcessingException):
# Code client is not correct
processing.run(alg, params, feedback=FeedbackPrint(), context=context)

params["PREFIX_PARCELLE"] = "140"

self.assertRaises(
QgsProcessingException,
processing.run,
alg,
params,
feedback=FeedbackPrint(),
context=context,
)

variables = project.customVariables()
self.assertNotIn(Key.Code, list(variables.keys()))
self.assertNotIn(Key.Prefix, list(variables.keys()))


if __name__ == "__main__":
from qgis.testing import start_app
Expand Down

0 comments on commit 8a08c22

Please sign in to comment.