Skip to content

Commit

Permalink
Added FloatArrayProperty.
Browse files Browse the repository at this point in the history
Closes scholrly#197.
  • Loading branch information
mhluongo committed Jan 11, 2014
1 parent f4aa2e9 commit 3cf202d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
2 changes: 1 addition & 1 deletion neo4django/db/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@
IntegerProperty, DateProperty, DateTimeProperty,
ArrayProperty, StringArrayProperty, IntArrayProperty,
URLArrayProperty, AutoProperty, BooleanProperty,
FloatProperty)
FloatProperty, FloatArrayProperty)
22 changes: 19 additions & 3 deletions neo4django/db/models/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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", "+")
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions neo4django/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down Expand Up @@ -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()
3 changes: 1 addition & 2 deletions reg_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']

0 comments on commit 3cf202d

Please sign in to comment.