<s> from casexml . apps . phone . models import SyncLog <EOL> from dimagi . utils . couch . database import get_db <EOL> def get_last_synclog_for_user ( user_id ) : <EOL> results = synclog_view ( <EOL> "<STR_LIT>" , <EOL> startkey = [ user_id , { } ] , <EOL> endkey = [ user_id ] , <EOL> descending = True , <EOL> limit = <NUM_LIT:1> , <EOL> reduce = False , <EOL> include_docs = True , <EOL> ) <EOL> if results : <EOL> row , = results <EOL> return SyncLog . wrap ( row [ '<STR_LIT>' ] ) <EOL> else : <EOL> return None <EOL> def synclog_view ( view_name , ** params ) : <EOL> return combine_views ( [ SyncLog . get_db ( ) , get_db ( None ) ] , view_name , ** params ) <EOL> def combine_views ( dbs , view_name , ** params ) : <EOL> assert params . get ( '<STR_LIT>' ) is False , '<STR_LIT>' <EOL> assert not params . get ( '<STR_LIT>' ) , '<STR_LIT>' <EOL> rows = [ ] <EOL> for db in dbs : <EOL> rows . extend ( db . view ( view_name , ** params ) ) <EOL> if params . get ( '<STR_LIT>' ) : <EOL> rows . sort ( key = lambda row : row [ '<STR_LIT:key>' ] , reverse = True ) <EOL> else : <EOL> rows . sort ( key = lambda row : row [ '<STR_LIT:key>' ] ) <EOL> if '<STR_LIT>' in params : <EOL> return rows [ : params [ '<STR_LIT>' ] ] <EOL> else : <EOL> return rows </s>
