#NEWLINE# @ permission_checker . require_any ( 'add' , 'change' , 'delete' ) #NEWLINE# @ vary_on_headers ( 'X-Requested-With' ) #NEWLINE# def index( request ) : #NEWLINE# #INDENT# Document = get_document_model ( ) #NEWLINE# documents = permission_policy . instances_user_has_any_permission_for ( request . user , [ 'change' , 'delete' ] ) #NEWLINE# if ( ( 'ordering' in request . GET ) and ( request . GET [ 'ordering' ] in [ 'title' , '-created_at' ] ) ) : #NEWLINE# #INDENT# ordering = request . GET [ 'ordering' ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# ordering = '-created_at' #NEWLINE# #UNINDENT# documents = documents . order_by ( ordering ) #NEWLINE# current_collection = None #NEWLINE# collection_id = request . GET . get ( 'collection_id' ) #NEWLINE# if collection_id : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# current_collection = Collection . objects . get ( id = collection_id ) #NEWLINE# documents = documents . filter ( collection = current_collection ) #NEWLINE# #UNINDENT# except ( ValueError , Collection . DoesNotExist ) : #NEWLINE# #INDENT# pass #NEWLINE# #UNINDENT# #UNINDENT# query_string = None #NEWLINE# if ( 'q' in request . GET ) : #NEWLINE# #INDENT# form = SearchForm ( request . GET , placeholder = _ ( 'Search documents' ) ) #NEWLINE# if form . is_valid ( ) : #NEWLINE# #INDENT# query_string = form . cleaned_data [ 'q' ] #NEWLINE# documents = documents . search ( query_string ) #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# form = SearchForm ( placeholder = _ ( 'Search documents' ) ) #NEWLINE# #UNINDENT# ( paginator , documents ) = paginate ( request , documents ) #NEWLINE# collections = permission_policy . collections_user_has_any_permission_for ( request . user , [ 'add' , 'change' ] ) #NEWLINE# if ( len ( collections ) < 2 ) : #NEWLINE# #INDENT# collections = None #NEWLINE# #UNINDENT# if request . is_ajax ( ) : #NEWLINE# #INDENT# return render ( request , 'wagtaildocs/documents/results.html' , { #NEWLINE# #INDENT# 'ordering' : ordering , #NEWLINE# 'documents' : documents , #NEWLINE# 'query_string' : query_string , #NEWLINE# 'is_searching' : bool ( query_string ) , #NEWLINE# #UNINDENT# } ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# return render ( request , 'wagtaildocs/documents/index.html' , { #NEWLINE# #INDENT# 'ordering' : ordering , #NEWLINE# 'documents' : documents , #NEWLINE# 'query_string' : query_string , #NEWLINE# 'is_searching' : bool ( query_string ) , #NEWLINE# 'search_form' : form , #NEWLINE# 'popular_tags' : Document . popular_tags ( ) , #NEWLINE# 'user_can_add' : permission_policy . user_has_permission ( request . user , 'add' ) , #NEWLINE# 'collections' : collections , #NEWLINE# 'current_collection' : current_collection , #NEWLINE# #UNINDENT# } )