19
19
class _MetaDocument (_MetaFieldContainer ):
20
20
21
21
def __init__ (cls , name , bases , classdict ):
22
- cls ._set_tablename ()
22
+ cls ._tablename = cls . _get_tablename ()
23
23
24
24
# make sure that the following runs only for subclasses of Document.
25
25
# There's no really nice way to do this AFAIK, because 'Document' is
@@ -37,12 +37,9 @@ class Document(FieldContainer, metaclass = _MetaDocument):
37
37
Non-obvious customization:
38
38
cls._table_create_options dict with extra kwargs for rethinkdb.table_create
39
39
"""
40
- _tablename = None # customize with _set_tablename
40
+ _tablename = None # customize with _get_tablename - don't set this attr
41
41
_table_create_options = None # dict with extra kwargs for rethinkdb.table_create
42
42
43
- pkey = None # alias for whichever field is the primary key. set
44
- # automatically for subclasses upon class creation
45
-
46
43
47
44
def __init__ (self , ** kwargs ):
48
45
"""Makes a Document object, but does not save it yet to the database.
@@ -62,15 +59,15 @@ def __init__(self, **kwargs):
62
59
###########################################################################
63
60
64
61
@classmethod
65
- def _set_tablename (cls ):
62
+ def _get_tablename (cls ):
66
63
"""Override this in subclasses if you want anything other than a table
67
64
name automatically derived from the class name using
68
65
inflection.tableize().
69
66
70
67
Mind the dangers of further subclassing, and of using the same table
71
68
for different Document classes.
72
69
"""
73
- cls . _tablename = inflection .tableize (cls .__name__ )
70
+ return inflection .tableize (cls .__name__ )
74
71
75
72
76
73
@classmethod
@@ -392,24 +389,33 @@ async def __aiter__(self):
392
389
393
390
394
391
async def __anext__ (self ):
395
- try :
396
- msg = await self .cursor .next ()
397
- except r .ReqlCursorEmpty :
398
- raise StopAsyncIteration
392
+ while True :
393
+ try :
394
+ msg = await self .cursor .next ()
395
+ except r .ReqlCursorEmpty :
396
+ raise StopAsyncIteration
399
397
400
- doc = self .doc
398
+ if "new_val" not in msg :
399
+ continue
401
400
402
- # update doc
403
- if msg ["new_val" ] == None :
404
- doc ._stored_in_db = False
405
- else :
406
- changed_fields = {k : v for k , v in msg ["new_val" ].items ()
407
- if k not in doc or v != doc .get_dbvalue (k )}
408
- for k , v in changed_fields .items ():
409
- doc_key = doc .get_key_for_dbkey (k )
410
- doc .set_dbvalue (doc_key , v , mark_updated = False )
401
+ doc = self .doc
402
+
403
+ # update doc and return changed fields
404
+ if msg ["new_val" ] == None :
405
+ doc ._stored_in_db = False
406
+ return doc , None , msg
407
+ else :
408
+ changed_fields = {k : v for k , v in msg ["new_val" ].items ()
409
+ if k not in doc or v != doc .get_dbvalue (k )}
410
+
411
+ if not changed_fields :
412
+ continue
413
+
414
+ for k , v in changed_fields .items ():
415
+ doc_key = doc .get_key_for_dbkey (k )
416
+ doc .set_dbvalue (doc_key , v , mark_updated = False )
411
417
412
- return doc , list (changed_fields .keys ()), msg
418
+ return doc , list (changed_fields .keys ()), msg
413
419
414
420
415
421
async def aiter_changes (self , conn = None ):
0 commit comments