diff --git a/neo4django/db/models/__init__.py b/neo4django/db/models/__init__.py index ae84c38..8e2e2c4 100644 --- a/neo4django/db/models/__init__.py +++ b/neo4django/db/models/__init__.py @@ -11,4 +11,4 @@ IntegerProperty, DateProperty, DateTimeProperty, ArrayProperty, StringArrayProperty, IntArrayProperty, URLArrayProperty, AutoProperty, BooleanProperty, - FloatProperty) + FloatProperty, FloatArrayProperty) diff --git a/neo4django/db/models/properties.py b/neo4django/db/models/properties.py index dbc1281..c049744 100644 --- a/neo4django/db/models/properties.py +++ b/neo4django/db/models/properties.py @@ -19,6 +19,7 @@ from neo4django.validators import (validate_array, validate_str_array, validate_int_array, + validate_float_array, ElementValidator) from neo4django.utils import AttrRouter, write_through from neo4django.decorators import borrows_methods @@ -632,6 +633,9 @@ def get_default(self): def to_neo(self, value): return float(value) + def from_neo(self, value): + return float(value) + def to_neo_index(self, value): # as with ints, we use a fixed-width binary decimal encoding with a # '-' for negative and '0' for positive or 0. we get a long-type @@ -807,9 +811,9 @@ def __init__(self, *args, **kwargs): el_val = ElementValidator(vals_or_tuple) self.validators.append(el_val) - #Store array values as a token separated string. For use in the event - #the user needs to access the neo4j data multiple ways. - #For example using REST interface you cannot store an empty array + # Store array values as a token separated string. For use in the event + # the user needs to access the neo4j data multiple ways. + # For example using REST interface you cannot store an empty array self.use_string = kwargs.get("use_string", False) self.token = kwargs.get("token", ",") self.escape_token = kwargs.get("escape_token", "+") @@ -868,6 +872,18 @@ class IntArrayProperty(ArrayProperty): member_to_neo_index = IntegerProperty.to_neo_index.im_func +class FloatArrayProperty(ArrayProperty): + _internal_type_ = 'FloatArrayProperty' + default_validators = [validate_float_array] + + member_to_neo_index = FloatProperty.to_neo_index.im_func + + def to_neo(self, value): + return tuple(FloatProperty.to_neo.im_func(self, v) for v in value) + + def from_neo(self, value): + return tuple(FloatProperty.from_neo.im_func(self, v) for v in value) + class BooleanProperty(Property): _internal_type_ = 'BooleanProperty' formfield = formfields.BooleanField diff --git a/neo4django/validators.py b/neo4django/validators.py index 6730ab8..26da5fc 100644 --- a/neo4django/validators.py +++ b/neo4django/validators.py @@ -29,6 +29,11 @@ def validate_int(value): raise exceptions.ValidationError('Enter a valid int.') +def validate_float(value): + if not isinstance(value, (float, int)): + raise exceptions.ValidationError('Enter a valid float.') + + class ElementValidator(object): """Validates a sequence element by element with a list of validators.""" def __init__(self, validators, message='Invalid sequence of elements.', @@ -69,5 +74,13 @@ def __init__(self, *args, **kwargs): super(StringArrayValidator, self).__init__([validate_basestring], *args, **kwargs) + +class FloatArrayValidator(ArrayValidator, ElementValidator): + def __init__(self, *args, **kwargs): + kwargs.setdefault('message', 'Enter a sequence of valid floats.') + super(FloatArrayValidator, self).__init__([validate_float], + *args, **kwargs) + validate_str_array = StringArrayValidator() validate_int_array = IntArrayValidator() +validate_float_array = FloatArrayValidator() diff --git a/reg_settings.py b/reg_settings.py index 32e4f09..4fc6bc4 100644 --- a/reg_settings.py +++ b/reg_settings.py @@ -199,5 +199,4 @@ 'test_relationship_models', 'test_typenode_transactionality', 'test_autoproperty_transactionality', - 'test_relationship_filter_many_to_many', - 'test_float_array_property'] + 'test_relationship_filter_many_to_many']