<s> """<STR_LIT>""" <EOL> from pymongo import ASCENDING , DESCENDING <EOL> from bombolone . config import PORT_DATABASE , DATABASE <EOL> from bombolone . core . utils import ensure_objectid , is_iterable <EOL> def denormalize ( item ) : <EOL> """<STR_LIT:U+0020>""" <EOL> return item <EOL> def sort_if_you_must ( items , only_one , sorted_by , sort_lang , sort_ascending ) : <EOL> """<STR_LIT>""" <EOL> if only_one == False and ( sorted_by or sort_lang ) : <EOL> return items . sort ( sorted_by or '<STR_LIT>' + sort_lang , sort_ascending and ASCENDING or DESCENDING ) <EOL> else : <EOL> return items <EOL> def db_engine ( collection = None , <EOL> item_id = None , <EOL> only_one = False , <EOL> conditions = None , <EOL> sorted_by = None , <EOL> sort_lang = None , <EOL> sort_ascending = True , <EOL> skip = None , <EOL> limit = None , <EOL> count = None , <EOL> denormalize = denormalize ) : <EOL> """<STR_LIT>""" <EOL> if item_id : <EOL> if is_iterable ( item_id ) : <EOL> list_items = collection . find ( { "<STR_LIT>" : { "<STR_LIT>" : [ ensure_objectid ( x ) for x in item_id ] } } ) <EOL> list_items = sort_if_you_must ( list_items , only_one , sorted_by , sort_lang , sort_ascending ) <EOL> return [ denormalize ( item ) for item in list_items ] <EOL> else : <EOL> return denormalize ( collection . find_one ( { "<STR_LIT>" : ensure_objectid ( item_id ) } ) ) <EOL> if only_one : <EOL> f = collection . find_one <EOL> else : <EOL> f = collection . find <EOL> if conditions : <EOL> items = f ( { '<STR_LIT>' : conditions } ) <EOL> else : <EOL> items = f ( ) <EOL> items = sort_if_you_must ( items , only_one , sorted_by , sort_lang , sort_ascending ) <EOL> if skip : <EOL> items = items . skip ( skip * limit ) <EOL> if only_one == False and limit : <EOL> items = items . limit ( limit ) <EOL> if count : <EOL> return items . count ( ) <EOL> if only_one : <EOL> return denormalize ( items ) <EOL> else : <EOL> list_items = list ( items ) <EOL> return [ denormalize ( item ) for item in list_items ] </s>
