<s> from __future__ import absolute_import , unicode_literals <EOL> import json <EOL> from django . core . urlresolvers import reverse <EOL> from django . shortcuts import get_object_or_404 , render <EOL> from django . utils . six import text_type <EOL> from django . utils . translation import ugettext as _ <EOL> from wagtail . utils . pagination import paginate <EOL> from wagtail . wagtailadmin . forms import SearchForm <EOL> from wagtail . wagtailadmin . modal_workflow import render_modal_workflow <EOL> from wagtail . wagtailsearch . backends import get_search_backend <EOL> from wagtail . wagtailsearch . index import class_is_indexed <EOL> from wagtail . wagtailsnippets . views . snippets import get_snippet_model_from_url_params <EOL> def choose ( request , app_label , model_name ) : <EOL> model = get_snippet_model_from_url_params ( app_label , model_name ) <EOL> items = model . objects . all ( ) <EOL> is_searchable = class_is_indexed ( model ) <EOL> is_searching = False <EOL> search_query = None <EOL> if is_searchable and '<STR_LIT:q>' in request . GET : <EOL> search_form = SearchForm ( request . GET , placeholder = _ ( "<STR_LIT>" ) % { <EOL> '<STR_LIT>' : model . _meta . verbose_name <EOL> } ) <EOL> if search_form . is_valid ( ) : <EOL> search_query = search_form . cleaned_data [ '<STR_LIT:q>' ] <EOL> search_backend = get_search_backend ( ) <EOL> items = search_backend . search ( search_query , items ) <EOL> is_searching = True <EOL> else : <EOL> search_form = SearchForm ( placeholder = _ ( "<STR_LIT>" ) % { <EOL> '<STR_LIT>' : model . _meta . verbose_name <EOL> } ) <EOL> paginator , paginated_items = paginate ( request , items , per_page = <NUM_LIT> ) <EOL> if request . GET . get ( '<STR_LIT>' , None ) == '<STR_LIT:true>' : <EOL> return render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : model . _meta , <EOL> '<STR_LIT>' : paginated_items , <EOL> '<STR_LIT>' : search_query , <EOL> '<STR_LIT>' : is_searching , <EOL> } ) <EOL> return render_modal_workflow ( <EOL> request , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : model . _meta , <EOL> '<STR_LIT>' : paginated_items , <EOL> '<STR_LIT>' : is_searchable , <EOL> '<STR_LIT>' : search_form , <EOL> '<STR_LIT>' : search_query , <EOL> '<STR_LIT>' : is_searching , <EOL> } <EOL> ) <EOL> def chosen ( request , app_label , model_name , id ) : <EOL> model = get_snippet_model_from_url_params ( app_label , model_name ) <EOL> item = get_object_or_404 ( model , id = id ) <EOL> snippet_json = json . dumps ( { <EOL> '<STR_LIT:id>' : item . id , <EOL> '<STR_LIT:string>' : text_type ( item ) , <EOL> '<STR_LIT>' : reverse ( '<STR_LIT>' , args = ( <EOL> app_label , model_name , item . id ) ) <EOL> } ) <EOL> return render_modal_workflow ( <EOL> request , <EOL> None , '<STR_LIT>' , <EOL> { <EOL> '<STR_LIT>' : snippet_json , <EOL> } <EOL> ) </s>
