Added BooleanField

This commit is contained in:
Harry Marr 2010-01-05 18:17:44 +00:00
parent 3bead80f96
commit af1d7ef664
4 changed files with 51 additions and 12 deletions

View File

@ -2,8 +2,6 @@
User Guide
==========
.. _guide-connecting:
Installing
==========
MongoEngine is available on PyPI, so to use it you can use
@ -20,6 +18,8 @@ Alternatively, if you don't have setuptools installed, `download it from PyPi
# python setup.py install
.. _guide-connecting:
Connecting to MongoDB
=====================
To connect to a running instance of :program:`mongod`, use the

View File

@ -7,9 +7,9 @@ import pymongo
import datetime
__all__ = ['StringField', 'IntField', 'FloatField', 'DateTimeField',
'EmbeddedDocumentField', 'ListField', 'ObjectIdField',
'ReferenceField', 'ValidationError']
__all__ = ['StringField', 'IntField', 'FloatField', 'BooleanField',
'DateTimeField', 'EmbeddedDocumentField', 'ListField',
'ObjectIdField', 'ReferenceField', 'ValidationError']
class StringField(BaseField):
@ -25,7 +25,7 @@ class StringField(BaseField):
return unicode(value)
def validate(self, value):
assert(isinstance(value, (str, unicode)))
assert isinstance(value, (str, unicode))
if self.max_length is not None and len(value) > self.max_length:
raise ValidationError('String value is too long')
@ -50,7 +50,7 @@ class IntField(BaseField):
return int(value)
def validate(self, value):
assert(isinstance(value, (int, long)))
assert isinstance(value, (int, long))
if self.min_value is not None and value < self.min_value:
raise ValidationError('Integer value is too small')
@ -71,7 +71,7 @@ class FloatField(BaseField):
return float(value)
def validate(self, value):
assert(isinstance(value, float))
assert isinstance(value, float)
if self.min_value is not None and value < self.min_value:
raise ValidationError('Float value is too small')
@ -80,12 +80,23 @@ class FloatField(BaseField):
raise ValidationError('Float value is too large')
class BooleanField(BaseField):
"""A boolean field type.
"""
def to_python(self, value):
return bool(value)
def validate(self, value):
assert isinstance(value, bool)
class DateTimeField(BaseField):
"""A datetime field.
"""
def validate(self, value):
assert(isinstance(value, datetime.datetime))
assert isinstance(value, datetime.datetime)
class EmbeddedDocumentField(BaseField):
@ -202,7 +213,7 @@ class ReferenceField(BaseField):
return pymongo.dbref.DBRef(collection, id_)
def validate(self, value):
assert(isinstance(value, (self.document_type, pymongo.dbref.DBRef)))
assert isinstance(value, (self.document_type, pymongo.dbref.DBRef))
def lookup_member(self, member_name):
return self.document_type._fields.get(member_name)

View File

@ -1,6 +1,5 @@
from setuptools import setup, find_packages
VERSION = '0.1.1'
import os
DESCRIPTION = "A Python Document-Object Mapper for working with MongoDB"
@ -10,6 +9,20 @@ try:
except:
pass
def get_version(version_tuple):
version = '%s.%s' % (version_tuple[0], version_tuple[1])
if version_tuple[2]:
version = '%s.%s' % (version, version_tuple[2])
return version
# Dirty hack to get version number from monogengine/__init__.py - we can't
# import it as it depends on PyMongo and PyMongo isn't installed until this
# file is read
init = os.path.join(os.path.dirname(__file__), 'mongoengine', '__init__.py')
version_line = filter(lambda l: l.startswith('VERSION'), open(init))[0]
VERSION = get_version(eval(version_line.split('=')[-1]))
print VERSION
CLASSIFIERS = [
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',

View File

@ -113,6 +113,21 @@ class FieldTest(unittest.TestCase):
person.height = 4.0
self.assertRaises(ValidationError, person.validate)
def test_boolean_validation(self):
"""Ensure that invalid values cannot be assigned to boolean fields.
"""
class Person(Document):
admin = BooleanField()
person = Person()
person.admin = True
person.validate()
person.admin = 2
self.assertRaises(ValidationError, person.validate)
person.admin = 'Yes'
self.assertRaises(ValidationError, person.validate)
def test_datetime_validation(self):
"""Ensure that invalid values cannot be assigned to datetime fields.
"""