Skip to content

Commit

Permalink
make sure not to reduce the precision of datetime input, fix deletion…
Browse files Browse the repository at this point in the history
… code
  • Loading branch information
Simon Zimmermann committed Feb 10, 2012
1 parent 8ac59ce commit 64adebd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
16 changes: 10 additions & 6 deletions odis/__init__.py
Expand Up @@ -9,7 +9,7 @@
import functools

from . import config
from .utils import safe_bytestr, safe_unicode, timeit
from .utils import safe_bytestr, safe_unicode, s

try:
import odisconfig
Expand Down Expand Up @@ -851,12 +851,12 @@ def __get__(self, instance, owner):
return value

def to_python(self, value):
if isinstance(value, datetime.date):
return datetime.datetime(value.year, value.month, value.day)
elif isinstance(value, datetime.datetime):
if isinstance(value, datetime.datetime):
# make sure we always use the same precision 1 == 100000
value.replace(microsecond=int(str(value.microsecond).ljust(6, '0')))
return value
elif isinstance(value, datetime.date):
return datetime.datetime(value.year, value.month, value.day)
try:
return datetime.datetime.fromtimestamp(float(value))
except TypeError, e:
Expand Down Expand Up @@ -971,6 +971,7 @@ def delete(self, *objs):
def replace(self, *objs):
'replace all members in a set with new members'
self.delete(*self.all())
self.model._db.delete(key)
self.add(*objs)

return RelQuerySet(model, key=key)
Expand Down Expand Up @@ -1097,19 +1098,22 @@ def save(self):

def delete(self, write=True, pipe=None, clean_cache=True):
p = pipe or self._db.pipeline()
# first we delete prior data
# first we delete hash
p.delete(self.key)
# rm from index for model
p.srem(self.key_for('all'), self.pk)
# make sure we delete indices not more in use.
pattern = re.compile(r'%s_zindex\:' % self._namespace)
indices_key = self.key_for('indices', pk=self.pk)

for k in self._db.smembers(self.key_for('indices', pk=self.pk)):
for k in self._db.smembers(indices_key):
if pattern.search(k):
p.zrem(k, self.pk)
else:
p.srem(k, self.pk)

p.delete(indices_key)

if clean_cache:
self.flush_query_cache(pk=self.pk)

Expand Down
24 changes: 8 additions & 16 deletions simpletest.py
@@ -1,38 +1,30 @@
import random
import time

from odis import Model, Field, ForeignField
from odis import Model, CharField, RelField, IntegerField

from odis.utils import s

class Foo(Model):
username = Field(index=True)
username = CharField(index=True, unique=True)
active = IntegerField(index=True)

class Bar(Model):
foo = ForeignField(Foo, nil=True)
foob = ForeignField(Foo)
fooc = ForeignField(Foo)
users = RelField(Foo)

db = Foo._db
db.flushdb()
usernames = ['foo', 'bar', 'baz', 'qux', 'foobar', 'foobaz', 'fooqux', 'barfoo', 'barbar']

for u in range(10):
Foo(username=random.choice(usernames), active=random.randint(0, 1)).save()

for i in range(1000):
Bar(foob=random.randint(1, 10), fooc=random.randint(1, 10)).save()

s()
for u in ['foo', 'bar', 'baz', 'qux']:
Foo(username=u, active=random.randint(0, 1)).save()

obj = Bar()
obj.is_valid()
obj = Bar(foob=random.randint(1, 10), fooc=random.randint(1, 10))
obj.save()

print obj.as_dict()
print obj.as_dict(to_db=True)
total = time.time()
qs = Bar.obj.include('foo')
qs = Bar.obj.all()
#qs = Bar.obj
obj = qs[0]
print 'total %.3fms' % (time.time() - total)
Expand Down

0 comments on commit 64adebd

Please sign in to comment.