Skip to content

Commit

Permalink
Specify the values parameter value for Boolean. Increase documentatio…
Browse files Browse the repository at this point in the history
…n to give a hint as to how this will be used by Studio.
  • Loading branch information
cahrens committed May 20, 2013
1 parent 84ae82d commit 3016c82
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
21 changes: 16 additions & 5 deletions xblock/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ class ModelType(object):
`default` : static value to default to if not otherwise specified (defaults to None)
`scope` : the scope in which this field class is used (defaults to Scope.content)
`display_name` : the display name for the field class, suitable for presenting in a GUI (defaults to name of class)
`values` : for field classes with a finite number of valid values, provides the ability to specify the set of
`values` : for field classes with a known set of valid values, provides the ability to explicitly specify the
valid values. This can be specified as either a static return value, or a function that generates
the valid values.
the valid values. For example formats, see the values property definition.
"""

def __init__(self, help=None, default=None, scope=Scope.content, display_name=None,
Expand All @@ -92,9 +92,15 @@ def name(self):
@property
def values(self):
"""
Returns the valid values for this class.
Returns the valid values for this class. This is useful for representing possible values in a UI.
If this field class does not define a finite number of valid values, this method will return None.
Example formats:
'[1, 2, 3]' : a finite set of elements
'[{"display_name": "Always", "value": "always"}, {"display_name": "Past Due", "value": "past_due"}] :
a finite set of elements where the display names differ from the values
'{"min" : 0 , "max" : 10, "step": .1}' : a range for floating point numbers with increment .1
If this field class does not define a set of valid values, this method will return None.
"""
if callable(self._values):
return self._values()
Expand Down Expand Up @@ -226,7 +232,12 @@ class Float(ModelType):


class Boolean(ModelType):
pass
"""
A field class for representing a Boolean. This class has the values property predefined.
"""
def __init__(self, help=None, default=None, scope=Scope.content, display_name=None,
values=({'display_name': "True", "value": True}, {'display_name': "False", "value": False})):
super( Boolean, self ).__init__(help, default, scope, display_name, values)


class Object(ModelType):
Expand Down
15 changes: 15 additions & 0 deletions xblock/test/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,9 @@ def test_field_display_name():
attempts._name = "max_problem_attempts"
assert_equals("Maximum Problem Attempts", attempts.display_name)

boolean_field = Boolean(display_name= "boolean field")
assert_equals("boolean field", boolean_field.display_name)

class TestNamespace(Namespace):
field_x = List(display_name="Field Known as X")

Expand All @@ -425,3 +428,15 @@ def test_values():

# default if nothing specified
assert_equals(None, String().values)

# Test boolean, which by default has values specified
test_field = Boolean()
assert_equals(({'display_name': "True", "value": True}, {'display_name': "False", "value": False}), test_field.values)

# Test that you can override the Boolean default for values
test_field = Boolean(values=[False])
assert_equals([False], test_field.values)

# Test the format expected for integers
test_field = Integer(values={"min": 1, "max" : 100})
assert_equals({"min": 1, "max" : 100}, test_field.values)

0 comments on commit 3016c82

Please sign in to comment.