Skip to content

Commit

Permalink
Make BoolenField as descriptor class for proper value serialization i…
Browse files Browse the repository at this point in the history
…nto database and from database
  • Loading branch information
GrAndSE committed May 2, 2012
1 parent c96468a commit de8eeab
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
2 changes: 2 additions & 0 deletions lighty/db/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def get_datastore_operation(operation):
def process_operand(operand):
if operand is None:
return ''
elif isinstance(operand, bool):
return str(operand).lower()
elif isinstance(operand, string_types):
return '"%s"' % operand
elif isinstance(operand, int):
Expand Down
24 changes: 19 additions & 5 deletions lighty/db/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ class IntegerField(Field, NumericField):
'''An integer. The admin represents this as an <input type="text"> (a
single-line input).
'''
pass



class PositiveIntegerField(IntegerField):
'''Like an IntegerField, but must be positive.
Expand All @@ -189,15 +188,13 @@ class AutoField(IntegerField):
automatically be added to your model if you don't specify otherwise. See
Automatic primary key fields.
'''
pass


class FloatField(Field, NumericField):
'''A floating-point number represented in Python by a float instance.
The admin represents this as an <input type="text"> (a single-line input).
'''
pass


class DecimalField(Field, NumericField):
Expand All @@ -224,7 +221,24 @@ class BooleanField(Field):
The admin represents this as a checkbox
'''
pass
def __config__(self, model_name, field_name):
'''Configure field
'''
super(BooleanField, self).__config__(model_name, field_name)
self.model_attr_name = '_field_%s_%s' % (model_name, field_name)

def __set__(self, instance, value):
from ..utils import string_types
if isinstance(value, string_types):
value = value == 'True' or value == 'true' or value == 'TRUE'
elif not isinstance(value, bool):
value = bool(value)
setattr(instance, self.model_attr_name, value)

def __get__(self, instance, owner):
if instance is None:
return self
return getattr(instance, self.model_attr_name)


class NullBooleanField(BooleanField):
Expand Down
10 changes: 4 additions & 6 deletions lighty/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,11 @@ def __init__(self, key_name=None, is_new=True, **kwds):
self._app = None
self._key_name = key_name or '_id'
self._is_saved = not is_new
# Update value
self.__dict__.update(kwds)
# Set the default values for unsetted fields
cls = self.__class__
for field_name in self._fields:
if field_name not in kwds:
self.__dict__[field_name] = cls.__dict__[field_name].default
setattr(self, field_name, kwds[field_name] if field_name in kwds
else cls.__dict__[field_name].default)

def key(self):
"""Unique key for this entity.
Expand Down Expand Up @@ -150,8 +148,8 @@ def put(self):
Returns:
The key of the instance (either the existing key or a new key).
"""
fields = dict([(field, self.__dict__[field])
for field in self._fields])
fields = dict([(field_name, getattr(self, field_name))
for field_name in self._fields])
datastore.put(self.__class__, fields)
return self
save = put
Expand Down

0 comments on commit de8eeab

Please sign in to comment.