Skip to content

Commit 73dc6f6

Browse files
committed
Improve redis_cli add test_first option
Fix objcache.get_object process None bug Enable _xxx_ of ORM can support Lazy process
1 parent 2d23929 commit 73dc6f6

6 files changed

Lines changed: 69 additions & 39 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Uliweb Change Log
3333
* Add CHECK_MAX_LENGTH to orm app. So if it's True, and if max_length is not passed
3434
a value, it'll raise an Exception. Or if CHECK_MAX_LENGTH is False, max_length will
3535
be 255 by default.
36-
* Add `post_do` and `print_sql` function to orm.
36+
* Add `post_do` and `print_` function to orm.
3737
* Change `generic/template_files/common/views.py` to `views.py.tmpl`
3838
* Add GenericReference value `(Model, object_id)` support
3939
* Fix pyini save process

‎test/test_orm.py‎

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,10 @@ def test_reference_not_id():
710710
<Test {'username':u'limodou1','year':20,'id':1}>
711711
>>> print b2._test_
712712
limodou1
713+
>>> #Test get with fields and lazy load _field_
714+
>>> b3 = Test1.get(Test1.c.name=='aaaa', fields=['name'])
715+
>>> print b3._test_
716+
limodou1
713717
>>> print a1.tttt.has(b1, b2)
714718
True
715719
>>> print a1.tttt.ids()
@@ -1901,35 +1905,22 @@ def test_post_do():
19011905
# db = get_connection('sqlite://')
19021906
# db.echo = True
19031907
# db.metadata.drop_all()
1904-
# class User(Model):
1905-
# username = Field(unicode)
1906-
# class Group(Model):
1907-
# name = Field(str)
1908-
# deleted = Field(bool)
1909-
# users = ManyToMany(User)
1910-
# a = User(username='limodou')
1911-
# a.save()
1912-
#
1913-
# b = User(username='user')
1914-
# b.save()
1915-
#
1916-
# c = User(username='abc')
1917-
# c.save()
1918-
#
1919-
# g1 = Group(name='python')
1920-
# g1.save()
1921-
#
1922-
# g2 = Group(name='perl')
1923-
# g2.save()
1924-
#
1925-
# g3 = Group(name='java')
1926-
# g3.save()
1927-
#
1928-
# g1.users.add(a)
1929-
#
1930-
# g1.users.add(b, 3) #add can support multiple object, and object can also int
1931-
#
1932-
# g1.users.add(a, b) #can has duplicated records
1933-
#
1934-
# list(g1.users.all().fields('username'))
1935-
#
1908+
# class Test(Model):
1909+
# username = Field(CHAR, max_length=20)
1910+
# year = Field(int)
1911+
# class Test1(Model):
1912+
# test = Reference(Test, collection_name='tttt', reference_fieldname='username')
1913+
# year = Field(int)
1914+
# name = Field(CHAR, max_length=20)
1915+
# a1 = Test(username='limodou1', year=20)
1916+
# a1.save()
1917+
#
1918+
# b1 = Test1(name='user', year=5, test=a1)
1919+
# b1.save()
1920+
#
1921+
# b2 = Test1(name='aaaa', year=10, test=a1)
1922+
# b2.save()
1923+
#
1924+
# b3 = Test1.get(Test1.c.name=='aaaa', fields=['name'])
1925+
# print b3
1926+
# print b3._test_

‎uliweb/contrib/objcache/__init__.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,14 @@ def check_enable():
3030
def get_object(model, tablename, id):
3131
"""
3232
Get cached object from redis
33+
34+
if id is None then return None:
3335
"""
3436
from uliweb.utils.common import log
3537

38+
if not id:
39+
return
40+
3641
if not check_enable():
3742
return
3843

‎uliweb/contrib/redis_cli/__init__.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
def get_redis():
44
from uliweb import settings
5+
from uliweb.utils.common import log
56
import redis
67

78
options = settings.REDIS
@@ -15,5 +16,12 @@ def get_redis():
1516
d.update(options['connection_pool'])
1617
__connection_pool__ = (d, redis.ConnectionPool(**d))
1718
client = redis.Redis(connection_pool=__connection_pool__[1])
19+
20+
if settings.REDIS.test_first:
21+
try:
22+
client.info()
23+
except Exception as e:
24+
log.exception(e)
25+
client = None
1826
return client
1927

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[REDIS]
22
#unix_socket_path = '/tmp/redis.sock'
33
connection_pool = {'host':'localhost', 'port':6379}
4+
#if test after created redis client object
5+
test_first = False
46

57
[FUNCTIONS]
68
get_redis = 'uliweb.contrib.redis_cli.get_redis'

‎uliweb/orm/__init__.py‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
'set_debug_query', 'set_auto_create', 'set_auto_set_model',
77
'get_model', 'set_model', 'engine_manager', 'set_auto_dotransaction',
88
'set_tablename_converter', 'set_check_max_length', 'set_post_do',
9-
'print_sql', 'Lazy',
9+
'print_', 'Lazy',
1010
'CHAR', 'BLOB', 'TEXT', 'DECIMAL', 'Index', 'datetime', 'decimal',
1111
'Begin', 'Commit', 'Rollback', 'Reset', 'ResetAll', 'CommitAll', 'RollbackAll',
1212
'PICKLE', 'BIGINT', 'set_pk_type', 'PKTYPE',
@@ -351,7 +351,7 @@ def default_post_do(sender, query, conn):
351351
if __default_post_do__:
352352
__default_post_do__(query, conn)
353353

354-
def print_sql(query):
354+
def print_(query):
355355
d2 = query.compile()
356356
d2.visit_bindparam = d2.render_literal_bindparam
357357
return d2.process(query)
@@ -617,6 +617,26 @@ def __init__(cls, name, bases, dct):
617617
cls._bound = False
618618
cls.bind(auto_create=__auto_create__)
619619

620+
class LazyValue(object):
621+
def __init__(self, name, property):
622+
self.name = name
623+
self.property = property
624+
625+
def __get__(self, model_instance, model_class):
626+
if model_instance is None:
627+
return self
628+
629+
try:
630+
return self.property.get_lazy(model_instance, self.name, self.property.default_value())
631+
except AttributeError:
632+
return None
633+
634+
def __set__(self, model_instance, value):
635+
if model_instance is None:
636+
return
637+
638+
setattr(model_instance, self.name, value)
639+
620640
class Property(object):
621641
data_type = str
622642
field_class = String
@@ -690,7 +710,8 @@ def __property_config__(self, model_class, property_name):
690710
self.property_name = property_name
691711
if not self.name:
692712
self.name = property_name
693-
713+
setattr(model_class, self._lazy_value(), LazyValue(self._attr_name(), self))
714+
694715
def get_lazy(self, model_instance, name, default=None):
695716
v = getattr(model_instance, name, default)
696717
if v is Lazy:
@@ -828,6 +849,9 @@ def __repr__(self):
828849
)
829850

830851
def _attr_name(self):
852+
return '_STORED_' + self.name + '_'
853+
854+
def _lazy_value(self):
831855
return '_' + self.name + '_'
832856

833857
def to_str(self, v):
@@ -1156,7 +1180,7 @@ def __property_config__(self, model_class, property_name):
11561180
self.collection_name = self.reference_class.get_collection_name(self.collection_name, model_class.tablename)
11571181
setattr(self.reference_class, self.collection_name,
11581182
_ReverseReferenceProperty(model_class, property_name, self._id_attr_name()))
1159-
1183+
11601184
def __get__(self, model_instance, model_class):
11611185
"""Get reference object.
11621186
@@ -1210,7 +1234,7 @@ def __set__(self, model_instance, value):
12101234
else:
12111235
setattr(model_instance, self._attr_name(), None)
12121236
setattr(model_instance, self._resolved_attr_name(), None)
1213-
1237+
12141238
def validate(self, value):
12151239
"""Validate reference.
12161240
@@ -2628,7 +2652,7 @@ def get(cls, condition=None, connection=None, fields=None, **kwargs):
26282652
else:
26292653
_cond = condition
26302654
#if there is no cached object, then just fetch from database
2631-
return cls.connect(connection).filter(_cond, **kwargs).fields(fields).one()
2655+
return cls.connect(connection).filter(_cond, **kwargs).fields(*(fields or [])).one()
26322656

26332657
@classmethod
26342658
def get_or_notfound(cls, condition=None, connection=None, fields=None):

0 commit comments

Comments
 (0)