<s> from mongokit . document import Document <EOL> class RevisionDocument ( Document ) : <EOL> structure = { <EOL> "<STR_LIT:id>" : unicode , <EOL> "<STR_LIT>" : int , <EOL> "<STR_LIT>" : dict <EOL> } <EOL> class VersionedDocument ( Document ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , doc = None , * args , ** kwargs ) : <EOL> super ( VersionedDocument , self ) . __init__ ( doc = doc , * args , ** kwargs ) <EOL> if kwargs . get ( '<STR_LIT>' , None ) : <EOL> self . versioning_collection = self . db [ "<STR_LIT>" % self . collection . name ] <EOL> self . versioning_collection . ensure_index ( [ ( '<STR_LIT:id>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:1> ) ] , unique = True ) <EOL> self . versioning_collection . database . connection . register ( [ self . __class__ , RevisionDocument ] ) <EOL> def save ( self , versioning = True , * args , ** kwargs ) : <EOL> if versioning : <EOL> if '<STR_LIT>' in self : <EOL> self . pop ( '<STR_LIT>' ) <EOL> self [ '<STR_LIT>' ] = self . get_last_revision_id ( ) <EOL> else : <EOL> self [ '<STR_LIT>' ] = <NUM_LIT:0> <EOL> self [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> super ( VersionedDocument , self ) . save ( * args , ** kwargs ) <EOL> versionned_doc = RevisionDocument ( { "<STR_LIT:id>" : unicode ( self [ '<STR_LIT>' ] ) , "<STR_LIT>" : self [ '<STR_LIT>' ] } , <EOL> collection = self . versioning_collection ) <EOL> versionned_doc [ '<STR_LIT>' ] = dict ( self ) <EOL> versionned_doc . save ( ) <EOL> else : <EOL> super ( VersionedDocument , self ) . save ( * args , ** kwargs ) <EOL> return self <EOL> def delete ( self , versioning = False , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if versioning : <EOL> self . versioning_collection . remove ( { '<STR_LIT:id>' : self [ '<STR_LIT>' ] } ) <EOL> super ( VersionedDocument , self ) . delete ( * args , ** kwargs ) <EOL> def remove ( self , query , versioning = False , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if versioning : <EOL> id_lists = [ i [ '<STR_LIT>' ] for i in self . collection . find ( query , fields = [ '<STR_LIT>' ] ) ] <EOL> self . versioning_collection . remove ( { '<STR_LIT:id>' : { '<STR_LIT>' : id_lists } } ) <EOL> self . collection . remove ( spec_or_id = query , * args , ** kwargs ) <EOL> def get_revision ( self , revision_number ) : <EOL> doc = self . versioning_collection . RevisionDocument . find_one ( <EOL> { "<STR_LIT:id>" : self [ '<STR_LIT>' ] , '<STR_LIT>' : revision_number } ) <EOL> if doc : <EOL> return self . __class__ ( doc [ '<STR_LIT>' ] , collection = self . collection ) <EOL> def get_revisions ( self ) : <EOL> versionned_docs = self . versioning_collection . find ( { "<STR_LIT:id>" : self [ '<STR_LIT>' ] } ) <EOL> for verdoc in versionned_docs : <EOL> yield self . __class__ ( verdoc [ '<STR_LIT>' ] , collection = self . collection ) <EOL> def get_last_revision_id ( self ) : <EOL> last_doc = self . versioning_collection . find ( { '<STR_LIT:id>' : unicode ( self [ '<STR_LIT>' ] ) } ) . sort ( '<STR_LIT>' , - <NUM_LIT:1> ) . next ( ) <EOL> if last_doc : <EOL> return last_doc [ '<STR_LIT>' ] </s>
