Skip to content

Commit

Permalink
Default to requesting all attributes for python expression functions
Browse files Browse the repository at this point in the history
Fix #14985
  • Loading branch information
nyalldawson committed Jun 10, 2016
1 parent 9e54287 commit 1bc17e6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 4 additions & 4 deletions python/core/__init__.py
Expand Up @@ -30,7 +30,7 @@
from qgis._core import *


def register_function(function, arg_count, group, usesgeometry=False, **kwargs):
def register_function(function, arg_count, group, usesgeometry=False, referenced_columns=[QgsFeatureRequest.AllAttributes], **kwargs):
"""
Register a Python function to be used as a expression function.
Expand Down Expand Up @@ -59,8 +59,8 @@ def myfunc(values, *args):
"""
class QgsExpressionFunction(QgsExpression.Function):

def __init__(self, func, name, args, group, helptext='', usesgeometry=True, expandargs=False):
QgsExpression.Function.__init__(self, name, args, group, helptext, usesgeometry)
def __init__(self, func, name, args, group, helptext='', usesgeometry=True, referencedColumns=QgsFeatureRequest.AllAttributes, expandargs=False):
QgsExpression.Function.__init__(self, name, args, group, helptext, usesgeometry, referencedColumns)
self.function = func
self.expandargs = expandargs

Expand Down Expand Up @@ -100,7 +100,7 @@ def func(self, values, feature, parent):

function.__name__ = name
helptext = helptemplate.safe_substitute(name=name, doc=helptext)
f = QgsExpressionFunction(function, name, arg_count, group, helptext, usesgeometry, expandargs)
f = QgsExpressionFunction(function, name, arg_count, group, helptext, usesgeometry, referenced_columns, expandargs)

# This doesn't really make any sense here but does when used from a decorator context
# so it can stay.
Expand Down
20 changes: 19 additions & 1 deletion tests/src/python/test_qgsexpression.py
Expand Up @@ -16,7 +16,7 @@

from qgis.testing import unittest
from qgis.utils import qgsfunction
from qgis.core import QgsExpression
from qgis.core import QgsExpression, QgsFeatureRequest


class TestQgsExpressionCustomFunctions(unittest.TestCase):
Expand Down Expand Up @@ -46,6 +46,14 @@ def sqrt(values, feature, parent):
def geomtest(values, feature, parent):
pass

@qgsfunction(args=0, group='testing', register=False)
def no_referenced_columns_set(values, feature, parent):
return 1

@qgsfunction(args=0, group='testing', register=False, referenced_columns=['a', 'b'])
def referenced_columns_set(values, feature, parent):
return 2

def tearDown(self):
QgsExpression.unregisterFunction('testfun')

Expand Down Expand Up @@ -118,6 +126,16 @@ def testCanRegisterGeometryFunction(self):
success = QgsExpression.registerFunction(self.geomtest)
self.assertTrue(success)

def testReferencedColumnsNoSet(self):
success = QgsExpression.registerFunction(self.no_referenced_columns_set)
exp = QgsExpression('no_referenced_columns_set()')
self.assertEqual(exp.referencedColumns(), [QgsFeatureRequest.AllAttributes])

def testReferencedColumnsSet(self):
success = QgsExpression.registerFunction(self.referenced_columns_set)
exp = QgsExpression('referenced_columns_set()')
self.assertEqual(exp.referencedColumns(), ['a', 'b'])

def testCantOverrideBuiltinsWithUnregister(self):
success = QgsExpression.unregisterFunction("sqrt")
self.assertFalse(success)
Expand Down

0 comments on commit 1bc17e6

Please sign in to comment.