Skip to content

Commit

Permalink
Minor checks in the Processing about QgsProject dirty, and disable th…
Browse files Browse the repository at this point in the history
…e unique key field check
  • Loading branch information
Gustry committed Nov 22, 2024
1 parent 0eaf4b1 commit 75a750a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dynamic_layers/dynamic_layers_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ def __init__(self, parent: QDialog = None):

self.expression_widgets = []
temporary_list_expressions = [
# Project
# Project server properties
self.inProjectTitle_exp,
self.inProjectShortName_exp,
self.inProjectAbstract_exp,
Expand Down
39 changes: 34 additions & 5 deletions dynamic_layers/processing_provider/generate_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingException,
# QgsFeatureRequest,
QgsProcessingParameterBoolean,
QgsProcessingParameterExpression,
QgsProcessingParameterFeatureSource,
Expand Down Expand Up @@ -106,13 +107,41 @@ def checkParameterValues(self, parameters, context) -> Tuple[bool, str]:
msg = tr("You must have at least one layer with the configuration.")
return False, msg

source = self.parameterAsSource(parameters, self.INPUT, context)
field = self.parameterAsString(parameters, self.FIELD, context)
unique_values = source.uniqueValues(source.fields().indexFromName(field))
if len(unique_values) != source.featureCount():
msg = tr("You field '{}' does not have unique values within the given layer.")
if context.project().isDirty():
msg = tr("You must save your project first.")
return False, msg

# source = self.parameterAsSource(parameters, self.INPUT, context)
# field = self.parameterAsString(parameters, self.FIELD, context)
# index = source.fields().indexFromName(field)
# unique_values = source.uniqueValues(index)
# if len(unique_values) != source.featureCount():
#
# request = QgsFeatureRequest()
# request.setSubsetOfAttributes([field], source.fields())
# request.addOrderBy(field)
# request.setFlags(QgsFeatureRequest.NoGeometry)
# count = {}
# for f in source.getFeatures(request):
# if f[field] not in count.keys():
# count[f[field]] = 0
# count[f[field]] += 1
# debug = ''
# for k, v in count.items():
# debug += f'{k} → {v}, '
#
# # count = {k: v for k, v in count.items() if v >= 2}
# msg = tr(
# "You field '{}' does not have unique values within the given layer : "
# "{} uniques values versus {} features : {}"
# ).format(
# field,
# len(unique_values),
# source.featureCount(),
# debug
# )
# return False, msg

return super().checkParameterValues(parameters, context)

def processAlgorithm(self, parameters, context, feedback):
Expand Down

0 comments on commit 75a750a

Please sign in to comment.