def get(self, uid): """ Send suttaplex for given uid. It is represented in flat list structure where order matters. [vagga, vagga, text, text] represents: vagga vagga text text --- parameters: - in: path name: uid type: string required: true responses: 200: description: Suttaplex list schema: id: suttaplex-list type: array items: $ref: '#/definitions/Suttaplex' definitions: Suttaplex: type: object properties: uid: type: string blurb: type: string difficulty: required: false type: number original_title: type: string type: type: string translations: type: array items: $ref: '#/definitions/Translation' Translation: type: object properties: author: type: string id: type: string lang: type: string title: type: string """ language = request.args.get('language', current_app.config.get('DEFAULT_LANGUAGE')) uid = uid.replace('/', '-').strip('-') db = get_db() results = db.aql.execute(SUTTAPLEX_LIST, bind_vars={ 'language': language, 'uid': uid }) difficulties = {3: 'advanced', 2: 'intermediate', 1: 'beginner'} data = [] edges = {} for result in results: _from = result.pop('from') if result['difficulty']: result['difficulty'] = { 'name': difficulties[result['difficulty']], 'level': result['difficulty'] } parent = None try: parent = edges[_from] except KeyError: data.append(result) _id = f'root/{result["uid"]}' edges[_id] = result result['translations'] = sorted(result['translations'], key=language_sort( result['root_lang'])) if parent: try: parent['children'].append(result) except KeyError: parent['children'] = [result] recursive_sort(data, 'num') # Sorts data inplace data = flat_tree(data) return data, 200
def get(self, uid): """ Send suttaplex for given uid. It is represented in flat list structure where order matters. [vagga, vagga, text, text] represents: vagga vagga text text --- parameters: - in: path name: uid type: string required: true responses: 200: description: Suttaplex list schema: id: suttaplex-list type: array items: $ref: '#/definitions/Suttaplex' definitions: Suttaplex: type: object properties: uid: type: string blurb: type: string difficulty: required: false type: number original_title: type: string type: type: string translations: type: array items: $ref: '#/definitions/Translation' Translation: type: object properties: author: type: string id: type: string lang: type: string title: type: string """ language = request.args.get('language', current_app.config.get('DEFAULT_LANGUAGE')) uid = uid.replace('/', '-').strip('-') db = get_db() results = db.aql.execute(SUTTAPLEX_LIST, bind_vars={'language': language, 'uid': uid}) difficulties = { 3: 'advanced', 2: 'intermediate', 1: 'beginner' } data = [] edges = {} for result in results: _from = result.pop('from') if result['difficulty']: result['difficulty'] = {'name': difficulties[result['difficulty']], 'level': result['difficulty']} parent = None try: parent = edges[_from] except KeyError: data.append(result) _id = f'root/{result["uid"]}' edges[_id] = result result['translations'] = sorted(result['translations'], key=language_sort(result['root_lang'])) if parent: try: parent['children'].append(result) except KeyError: parent['children'] = [result] recursive_sort(data, 'num') # Sorts data inplace data = flat_tree(data) return data, 200