#NEWLINE# @ get ( '/s/sets/{set_id}/tree' ) #NEWLINE# def get_set_tree_route( request , set_id ) : #NEWLINE# #INDENT# '\n    Render the tree of units that exists within a set.\n\n    Contexts:\n    - Search set, preview units in set\n    - Pre diagnosis\n    - Learner view progress in set\n    - Set complete\n\n    NEXT STATE\n    GET View Set Tree\n        -> GET Choose Set    ...when set is complete\n        -> GET Choose Unit   ...when in learn or review mode\n        -> GET Learn Card    ...when in diagnosis\n            (Unit auto chosen)\n\n    TODO-2 merge with get_set_units_route\n    TODO-2 simplify this method\n    ' #NEWLINE# db_conn = request [ 'db_conn' ] #NEWLINE# set_ = Set . get ( db_conn , entity_id = set_id ) #NEWLINE# if ( not set_ ) : #NEWLINE# #INDENT# return abort ( 404 ) #NEWLINE# #UNINDENT# units = set_ . list_units ( db_conn ) #NEWLINE# output = { #NEWLINE# #INDENT# 'set' : set_ . deliver ( ) , #NEWLINE# 'units' : [ u . deliver ( ) for u in units ] , #NEWLINE# #UNINDENT# } #NEWLINE# current_user = get_current_user ( request ) #NEWLINE# if ( not current_user ) : #NEWLINE# #INDENT# return ( 200 , output ) #NEWLINE# #UNINDENT# context = ( current_user . get_learning_context ( ) if current_user else { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) #NEWLINE# buckets = traverse ( db_conn , current_user , set_ ) #NEWLINE# output [ 'buckets' ] = { #NEWLINE# #INDENT# 'diagnose' : [ u [ 'entity_id' ] for u in buckets [ 'diagnose' ] ] , #NEWLINE# 'review' : [ u [ 'entity_id' ] for u in buckets [ 'review' ] ] , #NEWLINE# 'learn' : [ u [ 'entity_id' ] for u in buckets [ 'learn' ] ] , #NEWLINE# 'done' : [ u [ 'entity_id' ] for u in buckets [ 'done' ] ] , #NEWLINE# #UNINDENT# } #NEWLINE# if ( set_id != context . get ( 'set' , { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } ) . get ( 'entity_id' ) ) : #NEWLINE# #INDENT# return ( 200 , output ) #NEWLINE# #UNINDENT# if buckets [ 'diagnose' ] : #NEWLINE# #INDENT# unit = buckets [ 'diagnose' ] [ 0 ] #NEWLINE# card = choose_card ( db_conn , current_user , unit ) #NEWLINE# next_ = { #NEWLINE# #INDENT# 'method' : 'GET' , #NEWLINE# 'path' : '/s/cards/{card_id}/learn' . format ( card_id = card [ 'entity_id' ] ) , #NEWLINE# #UNINDENT# } #NEWLINE# current_user . set_learning_context ( next = next_ , unit = unit . data , card = card . data ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( buckets [ 'review' ] or buckets [ 'learn' ] ) : #NEWLINE# #INDENT# next_ = { #NEWLINE# #INDENT# 'method' : 'GET' , #NEWLINE# 'path' : '/s/sets/{set_id}/units' . format ( set_id = set_id ) , #NEWLINE# #UNINDENT# } #NEWLINE# current_user . set_learning_context ( next = next_ ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# next_ = { #NEWLINE# #INDENT# 'method' : 'GET' , #NEWLINE# 'path' : '/s/users/{user_id}/sets' . format ( user_id = current_user [ 'id' ] ) , #NEWLINE# #UNINDENT# } #NEWLINE# current_user . set_learning_context ( next = next_ , unit = None , set = None ) #NEWLINE# #UNINDENT# #UNINDENT# output [ 'next' ] = next_ #NEWLINE# return ( 200 , output )