Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions motorengine/fields/binary_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,10 @@
class BinaryField(BaseField):
'''
Field responsible for storing binary values.

Usage:

.. testcode:: modeling_fields

name = BinaryField(required=True)

Available arguments (apart from those in `BaseField`):

* `max_bytes` - The maximum number of bytes that can be stored in this field
'''

Expand All @@ -26,18 +21,27 @@ def __init__(self, max_bytes=None, *args, **kwargs):
self.max_bytes = max_bytes

def to_son(self, value):
if value is None:
return None

if not isinstance(value, (six.binary_type, )):
return six.b(value)

return value

def from_son(self, value):
if value is None:
return None

if not isinstance(value, (six.binary_type, )):
return six.b(value)

return value

def validate(self, value):
if value is None:
return True

if not isinstance(value, (six.binary_type, )):
return False

Expand Down
20 changes: 10 additions & 10 deletions motorengine/fields/decimal_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,15 @@
class DecimalField(BaseField):
'''
Field responsible for storing fixed-point decimal numbers (:py:class:`decimal.Decimal`).

Usage:

.. testcode:: modeling_fields

import decimal

name = DecimalField(required=True, min_value=None, max_value=None, precision=2, rounding=decimal.ROUND_HALF_UP)

Available arguments (apart from those in `BaseField`):

* `min_value` - Raises a validation error if the decimal being stored is lesser than this value
* `max_value` - Raises a validation error if the decimal being stored is greather than this value
* `precision` - Number of decimal places to store.
* `rounding` - The rounding rule from the python decimal library:

* decimal.ROUND_CEILING (towards Infinity)
* decimal.ROUND_DOWN (towards zero)
* decimal.ROUND_FLOOR (towards -Infinity)
Expand All @@ -35,9 +28,7 @@ class DecimalField(BaseField):
* decimal.ROUND_HALF_UP (to nearest with ties going away from zero)
* decimal.ROUND_UP (away from zero)
* decimal.ROUND_05UP (away from zero if last digit after rounding towards zero would have been 0 or 5; otherwise towards zero)

.. note::

Decimal field stores the value as a string in MongoDB to preserve the precision.
'''

Expand All @@ -55,15 +46,24 @@ def __init__(self, min_value=None, max_value=None, precision=2, rounding=decimal
self.rounding = rounding

def to_son(self, value):
if value is None:
return None

value = decimal.Decimal(value)
return six.u(str(value.quantize(self.precision, rounding=self.rounding)))

def from_son(self, value):
if value is None:
return None

value = decimal.Decimal(value)

return value.quantize(self.precision, rounding=self.rounding)

def validate(self, value):
if value is None:
return True

try:
value = decimal.Decimal(value)
except:
Expand All @@ -75,4 +75,4 @@ def validate(self, value):
if self.max_value is not None and value > self.max_value:
return False

return True
return True
11 changes: 4 additions & 7 deletions motorengine/fields/url_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@
class URLField(BaseField):
'''
Field responsible for storing URLs.

Usage:

.. testcode:: modeling_fields

name = URLField(required=True)

Available arguments (apart from those in `BaseField`): `None`

.. note::

MotorEngine does not implement the `verify_exists` parameter
as MongoEngine due to async http requiring the current io_loop.
'''
Expand All @@ -34,6 +28,9 @@ class URLField(BaseField):
)

def validate(self, value):
if value is None:
return True

is_url = URLField.URL_REGEX.match(value)

return is_url
return is_url
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
packages=find_packages(),
include_package_data=True,
install_requires=[
'pymongo==3.6',
'tornado',
'pymongo==3.8',
'tornado==5.1.1',
'motor==1.2.1',
'six',
'easydict'
Expand Down