<s> from __future__ import absolute_import , unicode_literals <EOL> from django . apps import apps <EOL> from django . core . urlresolvers import reverse <EOL> from django . http import Http404 <EOL> from django . shortcuts import get_object_or_404 , redirect , render <EOL> from django . utils . text import capfirst <EOL> from django . utils . translation import ugettext as _ <EOL> from wagtail . utils . pagination import paginate <EOL> from wagtail . wagtailadmin import messages <EOL> from wagtail . wagtailadmin . edit_handlers import ( <EOL> ObjectList , extract_panel_definitions_from_model_class ) <EOL> from wagtail . wagtailadmin . forms import SearchForm <EOL> from wagtail . wagtailadmin . utils import permission_denied <EOL> from wagtail . wagtailsearch . backends import get_search_backend <EOL> from wagtail . wagtailsearch . index import class_is_indexed <EOL> from wagtail . wagtailsnippets . models import get_snippet_models <EOL> from wagtail . wagtailsnippets . permissions import get_permission_name , user_can_edit_snippet_type <EOL> def get_snippet_model_from_url_params ( app_name , model_name ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> model = apps . get_model ( app_name , model_name ) <EOL> except LookupError : <EOL> raise Http404 <EOL> if model not in get_snippet_models ( ) : <EOL> raise Http404 <EOL> return model <EOL> SNIPPET_EDIT_HANDLERS = { } <EOL> def get_snippet_edit_handler ( model ) : <EOL> if model not in SNIPPET_EDIT_HANDLERS : <EOL> if hasattr ( model , '<STR_LIT>' ) : <EOL> edit_handler = model . edit_handler <EOL> else : <EOL> panels = extract_panel_definitions_from_model_class ( model ) <EOL> edit_handler = ObjectList ( panels ) <EOL> SNIPPET_EDIT_HANDLERS [ model ] = edit_handler . bind_to_model ( model ) <EOL> return SNIPPET_EDIT_HANDLERS [ model ] <EOL> def index ( request ) : <EOL> snippet_model_opts = [ <EOL> model . _meta for model in get_snippet_models ( ) <EOL> if user_can_edit_snippet_type ( request . user , model ) ] <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : sorted ( <EOL> snippet_model_opts , key = lambda x : x . verbose_name . lower ( ) ) } ) <EOL> def list ( request , app_label , model_name ) : <EOL> model = get_snippet_model_from_url_params ( app_label , model_name ) <EOL> permissions = [ <EOL> get_permission_name ( action , model ) <EOL> for action in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ] <EOL> if not any ( [ request . user . has_perm ( perm ) for perm in permissions ] ) : <EOL> return permission_denied ( request ) <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_plural <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_plural <EOL> } ) <EOL> paginator , paginated_items = paginate ( request , items ) <EOL> if request . is_ajax ( ) : <EOL> template = '<STR_LIT>' <EOL> else : <EOL> template = '<STR_LIT>' <EOL> return render ( request , template , { <EOL> '<STR_LIT>' : model . _meta , <EOL> '<STR_LIT>' : paginated_items , <EOL> '<STR_LIT>' : request . user . has_perm ( get_permission_name ( '<STR_LIT>' , model ) ) , <EOL> '<STR_LIT>' : is_searchable , <EOL> '<STR_LIT>' : search_form , <EOL> '<STR_LIT>' : is_searching , <EOL> '<STR_LIT>' : search_query , <EOL> } ) <EOL> def create ( request , app_label , model_name ) : <EOL> model = get_snippet_model_from_url_params ( app_label , model_name ) <EOL> permission = get_permission_name ( '<STR_LIT>' , model ) <EOL> if not request . user . has_perm ( permission ) : <EOL> return permission_denied ( request ) <EOL> instance = model ( ) <EOL> edit_handler_class = get_snippet_edit_handler ( model ) <EOL> form_class = edit_handler_class . get_form_class ( model ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = form_class ( request . POST , request . FILES , instance = instance ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> messages . success ( <EOL> request , <EOL> _ ( "<STR_LIT>" ) . format ( <EOL> snippet_type = capfirst ( model . _meta . verbose_name ) , <EOL> instance = instance <EOL> ) , <EOL> buttons = [ <EOL> messages . button ( reverse ( <EOL> '<STR_LIT>' , args = ( app_label , model_name , instance . id ) <EOL> ) , _ ( '<STR_LIT>' ) ) <EOL> ] <EOL> ) <EOL> return redirect ( '<STR_LIT>' , app_label , model_name ) <EOL> else : <EOL> messages . error ( request , _ ( "<STR_LIT>" ) ) <EOL> edit_handler = edit_handler_class ( instance = instance , form = form ) <EOL> else : <EOL> form = form_class ( instance = instance ) <EOL> edit_handler = edit_handler_class ( instance = instance , form = form ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : model . _meta , <EOL> '<STR_LIT>' : edit_handler , <EOL> '<STR_LIT>' : form , <EOL> } ) <EOL> def edit ( request , app_label , model_name , id ) : <EOL> model = get_snippet_model_from_url_params ( app_label , model_name ) <EOL> permission = get_permission_name ( '<STR_LIT>' , model ) <EOL> if not request . user . has_perm ( permission ) : <EOL> return permission_denied ( request ) <EOL> instance = get_object_or_404 ( model , id = id ) <EOL> edit_handler_class = get_snippet_edit_handler ( model ) <EOL> form_class = edit_handler_class . get_form_class ( model ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> form = form_class ( request . POST , request . FILES , instance = instance ) <EOL> if form . is_valid ( ) : <EOL> form . save ( ) <EOL> messages . success ( <EOL> request , <EOL> _ ( "<STR_LIT>" ) . format ( <EOL> snippet_type = capfirst ( model . _meta . verbose_name_plural ) , <EOL> instance = instance <EOL> ) , <EOL> buttons = [ <EOL> messages . button ( reverse ( <EOL> '<STR_LIT>' , args = ( app_label , model_name , instance . id ) <EOL> ) , _ ( '<STR_LIT>' ) ) <EOL> ] <EOL> ) <EOL> return redirect ( '<STR_LIT>' , app_label , model_name ) <EOL> else : <EOL> messages . error ( request , _ ( "<STR_LIT>" ) ) <EOL> edit_handler = edit_handler_class ( instance = instance , form = form ) <EOL> else : <EOL> form = form_class ( instance = instance ) <EOL> edit_handler = edit_handler_class ( instance = instance , form = form ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : model . _meta , <EOL> '<STR_LIT>' : instance , <EOL> '<STR_LIT>' : edit_handler , <EOL> '<STR_LIT>' : form , <EOL> } ) <EOL> def delete ( request , app_label , model_name , id ) : <EOL> model = get_snippet_model_from_url_params ( app_label , model_name ) <EOL> permission = get_permission_name ( '<STR_LIT>' , model ) <EOL> if not request . user . has_perm ( permission ) : <EOL> return permission_denied ( request ) <EOL> instance = get_object_or_404 ( model , id = id ) <EOL> if request . method == '<STR_LIT:POST>' : <EOL> instance . delete ( ) <EOL> messages . success ( <EOL> request , <EOL> _ ( "<STR_LIT>" ) . format ( <EOL> snippet_type = capfirst ( model . _meta . verbose_name_plural ) , <EOL> instance = instance <EOL> ) <EOL> ) <EOL> return redirect ( '<STR_LIT>' , app_label , model_name ) <EOL> return render ( request , '<STR_LIT>' , { <EOL> '<STR_LIT>' : model . _meta , <EOL> '<STR_LIT>' : instance , <EOL> } ) <EOL> def usage ( request , app_label , model_name , id ) : <EOL> model = get_snippet_model_from_url_params ( app_label , model_name ) <EOL> instance = get_object_or_404 ( model , id = id ) <EOL> paginator , used_by = paginate ( request , instance . get_usage ( ) ) <EOL> return render ( request , "<STR_LIT>" , { <EOL> '<STR_LIT>' : instance , <EOL> '<STR_LIT>' : used_by <EOL> } ) </s>
