lsbardel / python-stdnet

Object-data mapper and advanced query manager for non relational databases
http://lsbardel.github.com/python-stdnet/
BSD 3-Clause "New" or "Revised" License
120 stars 20 forks source link

Filtering on datetime raises TypeError #70

Open sloria opened 10 years ago

sloria commented 10 years ago

The following code is meant to filter the Items that are checked out and updated in the past hour. However, the updated__gt query causes a TypeError to be raised.

from datetime import datetime, timedelta
from stdnet import odm

class Item(odm.StdModel):
    name = odm.CharField(required=True)
    checked_out = odm.BooleanField(default=False)
    updated = odm.DateTimeField(default=datetime.utcnow)

models = odm.Router("redis://")
models.register(Item)

item = models.item.new(name="foo", checked_out=True)
item.save()
hour_ago  = datetime.utcnow() - timedelta(hours=1)
recent = models.item.filter(checked_out=True,
                            updated__gt=hour_ago).sort_by("-updated").all()

Stack trace:

Traceback (most recent call last):
  File "tmp.py", line 16, in <module>
    updated__gt=hour_ago).sort_by("-updated").all()
  File "/Users/sloria1/Envs/tmp/lib/python2.7/site-packages/stdnet/odm/query.py", line 256, in all
    return self.items()
  File "/Users/sloria1/Envs/tmp/lib/python2.7/site-packages/stdnet/odm/query.py", line 581, in items
    return self.backend_query().items(callback=callback)
  File "/Users/sloria1/Envs/tmp/lib/python2.7/site-packages/stdnet/odm/query.py", line 615, in backend_query
    return q if isinstance(q, EmptyQuery) else q.backend_query(**kwargs)
  File "/Users/sloria1/Envs/tmp/lib/python2.7/site-packages/stdnet/odm/query.py", line 198, in backend_query
    self.__backend_query = self.backend.Query(self, **kwargs)
  File "/Users/sloria1/Envs/tmp/lib/python2.7/site-packages/stdnet/utils/async.py", line 116, in __init__
    self._build(**kwargs)
  File "/Users/sloria1/Envs/tmp/lib/python2.7/site-packages/stdnet/backends/redisb/__init__.py", line 149, in _build
    be = value.backend_query(pipe=pipe)
  File "/Users/sloria1/Envs/tmp/lib/python2.7/site-packages/stdnet/odm/query.py", line 198, in backend_query
    self.__backend_query = self.backend.Query(self, **kwargs)
  File "/Users/sloria1/Envs/tmp/lib/python2.7/site-packages/stdnet/utils/async.py", line 116, in __init__
    self._build(**kwargs)
  File "/Users/sloria1/Envs/tmp/lib/python2.7/site-packages/stdnet/backends/redisb/__init__.py", line 154, in _build
    value = self.dump_nested(*value)
  File "/Users/sloria1/Envs/tmp/lib/python2.7/site-packages/stdnet/backends/redisb/__init__.py", line 255, in dump_nested
    return json.dumps((value, nested_args))
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 243, in dumps
    return _default_encoder.encode(obj)
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
  File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 184, in default
    raise TypeError(repr(o) + " is not JSON serializable")
TypeError: datetime.datetime(2013, 12, 1, 1, 6, 4, 110880) is not JSON serializable
lsbardel commented 10 years ago

Looks like a bug, Could you write a test using your example? You could put the test into a new file dates.py within the tests.all.fields module.

Something like this

class TestItem(test.TestCase):
     model = Item
     def test_filter(self):
          models = self.mapper
          ...

Once done that I can take a look. Thanks