Skip to content

Commit

Permalink
Strict and shared True/False representation for rest controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
manasdk committed Jun 15, 2016
1 parent 081e0d3 commit 31c3e22
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
4 changes: 3 additions & 1 deletion st2api/st2api/controllers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from pecan.rest import RestController
from six.moves.urllib import parse as urlparse # pylint: disable=import-error

from st2api.controllers.controller_transforms import transform_to_bool

__all__ = [
'BaseRestControllerMixin'
]
Expand Down Expand Up @@ -59,6 +61,6 @@ def _get_query_param_value(self, request, param_name, param_type, default_value=
value = query_params.get(param_name, default_value)

if param_type == 'bool' and isinstance(value, six.string_types):
value = value.lower() in ['1', 'true']
value = transform_to_bool(value)

return value
34 changes: 34 additions & 0 deletions st2api/st2api/controllers/controller_transforms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


__all__ = [
'transform_to_bool'
]


def transform_to_bool(value):
"""
Transforms a certain set of values to True or False.
True can be represented by '1', 'True' and 'true.'
False can be represented by '1', 'False' and 'false.'
Any other representation will be rejected.
"""
if value in ['1', 'true', 'True']:
return True
elif value in ['0', 'false', 'False']:
return False
raise ValueError('Invalid bool representation "%s" provided.' % value)
3 changes: 2 additions & 1 deletion st2api/st2api/controllers/v1/rbac.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from pecan import rest

from st2api.controllers.controller_transforms import transform_to_bool
from st2api.controllers.resource import ResourceController
from st2common.models.api.base import jsexpose
from st2common.models.api.rbac import RoleAPI
Expand All @@ -39,7 +40,7 @@ class RolesController(ResourceController):
}

filter_transform_functions = {
'system': lambda value: value in ['1', 'true', 'True']
'system': transform_to_bool
}

query_options = {
Expand Down
3 changes: 2 additions & 1 deletion st2api/st2api/controllers/v1/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from st2common.exceptions.apivalidation import ValueValidationException
from st2common.exceptions.triggers import TriggerDoesNotExistException
from st2api.controllers import resource
from st2api.controllers.controller_transforms import transform_to_bool
from st2api.controllers.v1.ruleviews import RuleViewController
from st2common.models.api.rule import RuleAPI
from st2common.models.api.base import jsexpose
Expand Down Expand Up @@ -57,7 +58,7 @@ class RuleController(resource.ContentPackResourceController):
}

filter_transform_functions = {
'enabled': lambda value: True if value.lower() == 'true' else False
'enabled': transform_to_bool
}

query_options = {
Expand Down

0 comments on commit 31c3e22

Please sign in to comment.