def add_items(self, model, items): ''' Called by update_index management command Need to override so that ingest_plugin attachment items get recreated correctly ''' if not class_is_indexed(model): return # Get mapping mapping = self.mapping_class(model) doc_type = mapping.get_document_type() if mapping.get_document_type() == JOURNAL_DOCUMENT_TYPE: # Create list of actions self.add_ingest_pipeline() actions = [] for item in items: # Create the action action = { '_index': self.name, '_type': doc_type, '_id': mapping.get_document_id(item), 'pipeline': INGEST_ATTACHMENT_ID, } action.update(mapping.get_document(item)) actions.append(action) log.info('in add_items with attachment') # Run the actions bulk(self.es, actions) else: super(JournalsearchIndex, self).add_items(model, items)
def search(self, query_string, model_or_queryset, fields=None, filters=None, prefetch_related=None): # Find model/queryset if isinstance(model_or_queryset, QuerySet): model = model_or_queryset.model queryset = model_or_queryset else: model = model_or_queryset queryset = model_or_queryset.objects.all() # Model must be a class that is in the index if not class_is_indexed(model): return [] # Check that theres still a query string after the clean up if query_string == "": return [] # Apply filters to queryset if filters: queryset = queryset.filter(**filters) # Prefetch related if prefetch_related: for prefetch in prefetch_related: queryset = queryset.prefetch_related(prefetch) # Search return self._search(queryset, query_string, fields=fields)
def search(self, query_string, model_or_queryset, fields=None, filters=None, prefetch_related=None, operator=None): # Find model/queryset if isinstance(model_or_queryset, QuerySet): model = model_or_queryset.model queryset = model_or_queryset else: model = model_or_queryset queryset = model_or_queryset.objects.all() # Model must be a class that is in the index if not class_is_indexed(model): return [] # Check that theres still a query string after the clean up if query_string == "": return [] # Apply filters to queryset if filters: queryset = queryset.filter(**filters) # Prefetch related if prefetch_related: for prefetch in prefetch_related: queryset = queryset.prefetch_related(prefetch) # Check operator if operator is not None: operator = operator.lower() if operator not in ['or', 'and']: raise ValueError("operator must be either 'or' or 'and'") # Search search_query = self.search_query_class(queryset, query_string, fields=fields, operator=operator) return self.search_results_class(self, search_query)
def list(request, app_label, model_name): model = get_snippet_model_from_url_params(app_label, model_name) permissions = [ get_permission_name(action, model) for action in ['add', 'change', 'delete'] ] if not any([request.user.has_perm(perm) for perm in permissions]): return permission_denied(request) items = model.objects.all() # Search is_searchable = class_is_indexed(model) is_searching = False search_query = None if is_searchable and 'q' in request.GET: search_form = SearchForm( request.GET, placeholder=_("Search %(snippet_type_name)s") % {'snippet_type_name': model._meta.verbose_name_plural}) if search_form.is_valid(): search_query = search_form.cleaned_data['q'] search_backend = get_search_backend() items = search_backend.search(search_query, items) is_searching = True else: search_form = SearchForm( placeholder=_("Search %(snippet_type_name)s") % {'snippet_type_name': model._meta.verbose_name_plural}) paginator, paginated_items = paginate(request, items) # Template if request.is_ajax(): template = 'wagtailsnippets/snippets/results.html' else: template = 'wagtailsnippets/snippets/type_index.html' return render( request, template, { 'model_opts': model._meta, 'items': paginated_items, 'can_add_snippet': request.user.has_perm(get_permission_name('add', model)), 'is_searchable': is_searchable, 'search_form': search_form, 'is_searching': is_searching, 'query_string': search_query, })
def choose(request, content_type_app_name, content_type_model_name): content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name) model = content_type.model_class() snippet_type_name, snippet_type_name_plural = get_snippet_type_name( content_type) items = model.objects.all() # Search is_searchable = class_is_indexed(model) is_searching = False search_query = None if is_searchable and 'q' in request.GET: search_form = SearchForm( request.GET, placeholder=_("Search %(snippet_type_name)s") % {'snippet_type_name': snippet_type_name_plural}) if search_form.is_valid(): search_query = search_form.cleaned_data['q'] search_backend = get_search_backend() items = search_backend.search(search_query, items) is_searching = True else: search_form = SearchForm( placeholder=_("Search %(snippet_type_name)s") % {'snippet_type_name': snippet_type_name_plural}) # Pagination paginator, paginated_items = paginate(request, items, per_page=25) # If paginating or searching, render "results.html" if request.GET.get('results', None) == 'true': return render( request, "wagtailsnippets/chooser/results.html", { 'content_type': content_type, 'snippet_type_name': snippet_type_name, 'items': paginated_items, 'query_string': search_query, 'is_searching': is_searching, }) return render_modal_workflow( request, 'wagtailsnippets/chooser/choose.html', 'wagtailsnippets/chooser/choose.js', { 'content_type': content_type, 'snippet_type_name': snippet_type_name, 'items': paginated_items, 'is_searchable': is_searchable, 'search_form': search_form, 'query_string': search_query, 'is_searching': is_searching, })
def add(self, obj): # Make sure the object can be indexed if not class_is_indexed(obj.__class__): return # Get mapping mapping = ElasticSearchMapping(obj.__class__) # Add document to index self.es.index(self.es_index, mapping.get_document_type(), mapping.get_document(obj), id=mapping.get_document_id(obj))
def choose(request, app_label, model_name): model = get_snippet_model_from_url_params(app_label, model_name) items = model.objects.all() # Preserve the snippet's model-level ordering if specified, but fall back on PK if not # (to ensure pagination is consistent) if not items.ordered: items = items.order_by('pk') # Search is_searchable = class_is_indexed(model) is_searching = False search_query = None if is_searchable and 'q' in request.GET: search_form = SearchForm( request.GET, placeholder=_("Search %(snippet_type_name)s") % {'snippet_type_name': model._meta.verbose_name}) if search_form.is_valid(): search_query = search_form.cleaned_data['q'] search_backend = get_search_backend() items = search_backend.search(search_query, items) is_searching = True else: search_form = SearchForm( placeholder=_("Search %(snippet_type_name)s") % {'snippet_type_name': model._meta.verbose_name}) # Pagination paginator, paginated_items = paginate(request, items, per_page=25) # If paginating or searching, render "results.html" if request.GET.get('results', None) == 'true': return render( request, "wagtailsnippets/chooser/results.html", { 'model_opts': model._meta, 'items': paginated_items, 'query_string': search_query, 'is_searching': is_searching, }) return render_modal_workflow( request, 'wagtailsnippets/chooser/choose.html', 'wagtailsnippets/chooser/choose.js', { 'model_opts': model._meta, 'items': paginated_items, 'is_searchable': is_searchable, 'search_form': search_form, 'query_string': search_query, 'is_searching': is_searching, })
def list(request, app_label, model_name): model = get_snippet_model_from_url_params(app_label, model_name) permissions = [ get_permission_name(action, model) for action in ['add', 'change', 'delete'] ] if not any([request.user.has_perm(perm) for perm in permissions]): return permission_denied(request) items = model.objects.all() # Preserve the snippet's model-level ordering if specified, but fall back on PK if not # (to ensure pagination is consistent) if not items.ordered: items = items.order_by('pk') # Search is_searchable = class_is_indexed(model) is_searching = False search_query = None if is_searchable and 'q' in request.GET: search_form = SearchForm(request.GET, placeholder=_("Search %(snippet_type_name)s") % { 'snippet_type_name': model._meta.verbose_name_plural }) if search_form.is_valid(): search_query = search_form.cleaned_data['q'] search_backend = get_search_backend() items = search_backend.search(search_query, items) is_searching = True else: search_form = SearchForm(placeholder=_("Search %(snippet_type_name)s") % { 'snippet_type_name': model._meta.verbose_name_plural }) paginator, paginated_items = paginate(request, items) # Template if request.is_ajax(): template = 'wagtailsnippets/snippets/results.html' else: template = 'wagtailsnippets/snippets/type_index.html' return render(request, template, { 'model_opts': model._meta, 'items': paginated_items, 'can_add_snippet': request.user.has_perm(get_permission_name('add', model)), 'is_searchable': is_searchable, 'search_form': search_form, 'is_searching': is_searching, 'query_string': search_query, })
def choose(request, app_label, model_name): model = get_snippet_model_from_url_params(app_label, model_name) items = model.objects.all() # Search is_searchable = class_is_indexed(model) is_searching = False search_query = None if is_searchable and "q" in request.GET: search_form = SearchForm( request.GET, placeholder=_("Search %(snippet_type_name)s") % {"snippet_type_name": model._meta.verbose_name} ) if search_form.is_valid(): search_query = search_form.cleaned_data["q"] search_backend = get_search_backend() items = search_backend.search(search_query, items) is_searching = True else: search_form = SearchForm( placeholder=_("Search %(snippet_type_name)s") % {"snippet_type_name": model._meta.verbose_name} ) # Pagination paginator, paginated_items = paginate(request, items, per_page=25) # If paginating or searching, render "results.html" if request.GET.get("results", None) == "true": return render( request, "wagtailsnippets/chooser/results.html", { "model_opts": model._meta, "items": paginated_items, "query_string": search_query, "is_searching": is_searching, }, ) return render_modal_workflow( request, "wagtailsnippets/chooser/choose.html", "wagtailsnippets/chooser/choose.js", { "model_opts": model._meta, "items": paginated_items, "is_searchable": is_searchable, "search_form": search_form, "query_string": search_query, "is_searching": is_searching, }, )
def list(request, content_type_app_name, content_type_model_name): content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name) model = content_type.model_class() permissions = [ get_permission_name(action, model) for action in ['add', 'change', 'delete'] ] if not any([request.user.has_perm(perm) for perm in permissions]): return permission_denied(request) snippet_type_name, snippet_type_name_plural = get_snippet_type_name(content_type) items = model.objects.all() # Search is_searchable = class_is_indexed(model) is_searching = False search_query = None if is_searchable and 'q' in request.GET: search_form = SearchForm(request.GET, placeholder=_("Search %(snippet_type_name)s") % { 'snippet_type_name': snippet_type_name_plural }) if search_form.is_valid(): search_query = search_form.cleaned_data['q'] search_backend = get_search_backend() items = search_backend.search(search_query, items) is_searching = True else: search_form = SearchForm(placeholder=_("Search %(snippet_type_name)s") % { 'snippet_type_name': snippet_type_name_plural }) paginator, paginated_items = paginate(request, items) # Template if request.is_ajax(): template = 'wagtailsnippets/snippets/results.html' else: template = 'wagtailsnippets/snippets/type_index.html' return render(request, template, { 'content_type': content_type, 'snippet_type_name': snippet_type_name, 'snippet_type_name_plural': snippet_type_name_plural, 'items': paginated_items, 'can_add_snippet': request.user.has_perm(get_permission_name('add', model)), 'is_searchable': is_searchable, 'search_form': search_form, 'is_searching': is_searching, 'query_string': search_query, })
def choose(request, content_type_app_name, content_type_model_name): content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name) model = content_type.model_class() snippet_type_name, snippet_type_name_plural = get_snippet_type_name(content_type) items = model.objects.all() # Search is_searchable = class_is_indexed(model) is_searching = False search_query = None if is_searchable and 'q' in request.GET: search_form = SearchForm(request.GET, placeholder=_("Search %(snippet_type_name)s") % { 'snippet_type_name': snippet_type_name_plural }) if search_form.is_valid(): search_query = search_form.cleaned_data['q'] search_backend = get_search_backend() items = search_backend.search(search_query, items) is_searching = True else: search_form = SearchForm(placeholder=_("Search %(snippet_type_name)s") % { 'snippet_type_name': snippet_type_name_plural }) # Pagination paginator, paginated_items = paginate(request, items, per_page=25) # If paginating or searching, render "results.html" if request.GET.get('results', None) == 'true': return render(request, "wagtailsnippets/chooser/results.html", { 'content_type': content_type, 'snippet_type_name': snippet_type_name, 'items': paginated_items, 'query_string': search_query, 'is_searching': is_searching, }) return render_modal_workflow( request, 'wagtailsnippets/chooser/choose.html', 'wagtailsnippets/chooser/choose.js', { 'content_type': content_type, 'snippet_type_name': snippet_type_name, 'items': paginated_items, 'is_searchable': is_searchable, 'search_form': search_form, 'query_string': search_query, 'is_searching': is_searching, } )
def add_item(self, item): # Make sure the object can be indexed if not class_is_indexed(item.__class__): return # Get mapping mapping = self.mapping_class(item.__class__) # Add document to index self.es.index( self.name, mapping.get_document_type(), mapping.get_document(item), id=mapping.get_document_id(item) )
def list(request, app_label, model_name): model = get_snippet_model_from_url_params(app_label, model_name) permissions = [get_permission_name(action, model) for action in ["add", "change", "delete"]] if not any([request.user.has_perm(perm) for perm in permissions]): return permission_denied(request) items = model.objects.all() # Search is_searchable = class_is_indexed(model) is_searching = False search_query = None if is_searchable and "q" in request.GET: search_form = SearchForm( request.GET, placeholder=_("Search %(snippet_type_name)s") % {"snippet_type_name": model._meta.verbose_name_plural}, ) if search_form.is_valid(): search_query = search_form.cleaned_data["q"] search_backend = get_search_backend() items = search_backend.search(search_query, items) is_searching = True else: search_form = SearchForm( placeholder=_("Search %(snippet_type_name)s") % {"snippet_type_name": model._meta.verbose_name_plural} ) paginator, paginated_items = paginate(request, items) # Template if request.is_ajax(): template = "wagtailsnippets/snippets/results.html" else: template = "wagtailsnippets/snippets/type_index.html" return render( request, template, { "model_opts": model._meta, "items": paginated_items, "can_add_snippet": request.user.has_perm(get_permission_name("add", model)), "is_searchable": is_searchable, "search_form": search_form, "is_searching": is_searching, "query_string": search_query, }, )
def delete(self, obj): # Make sure the object can be indexed if not class_is_indexed(obj.__class__): return # Get mapping mapping = ElasticSearchMapping(obj.__class__) # Delete document try: self.es.delete(self.es_index, mapping.get_document_type(), mapping.get_document_id(obj)) except NotFoundError: pass # Document doesn't exist, ignore this exception
def choose(request): # TODO: Ideally this would return the endnotes for the current article. items = EndNote.objects.all() # Search is_searchable = class_is_indexed(EndNote) is_searching = False search_query = None if is_searchable and 'q' in request.GET: search_form = SearchForm(request.GET, placeholder=_("Search End Notes")) if search_form.is_valid(): search_query = search_form.cleaned_data['q'] search_backend = get_search_backend() items = search_backend.search(search_query, items) is_searching = True else: search_form = SearchForm(placeholder=_("Search End Notes")) # Pagination p = request.GET.get("p", 1) paginator = Paginator(items, 25) try: paginated_items = paginator.page(p) except PageNotAnInteger: paginated_items = paginator.page(1) except EmptyPage: paginated_items = paginator.page(paginator.num_pages) # If paginating or searching, render "results.html" if request.GET.get('results', None) == 'true': return render( request, "content_notes/chooser/results.html", { 'items': paginated_items, 'query_string': search_query, 'is_searching': is_searching, }) return render_modal_workflow( request, 'content_notes/chooser/choose.html', 'content_notes/chooser/choose.js', { 'items': paginated_items, 'is_searchable': is_searchable, 'search_form': search_form, 'query_string': search_query, 'is_searching': is_searching, })
def search(self, query_string, model_or_queryset, fields=None, filters=None, prefetch_related=None, operator=None, order_by_relevance=True): # Find model/queryset if isinstance(model_or_queryset, QuerySet): model = model_or_queryset.model queryset = model_or_queryset else: model = model_or_queryset queryset = model_or_queryset.objects.all() # Model must be a class that is in the index if not class_is_indexed(model): return [] # Check that theres still a query string after the clean up if query_string == "": return [] # Only fields that are indexed as a SearchField can be passed in fields if fields: allowed_fields = {field.field_name for field in model.get_searchable_search_fields()} for field_name in fields: if field_name not in allowed_fields: raise FieldError( 'Cannot search with field "' + field_name + '". Please add index.SearchField(\'' + field_name + '\') to ' + model.__name__ + '.search_fields.' ) # Apply filters to queryset if filters: queryset = queryset.filter(**filters) # Prefetch related if prefetch_related: for prefetch in prefetch_related: queryset = queryset.prefetch_related(prefetch) # Check operator if operator is not None: operator = operator.lower() if operator not in ['or', 'and']: raise ValueError("operator must be either 'or' or 'and'") # Search search_query = self.query_class( queryset, query_string, fields=fields, operator=operator, order_by_relevance=order_by_relevance ) return self.results_class(self, search_query)
def search(self, query_string, model_or_queryset, fields=None, filters=None, prefetch_related=None, operator=None, order_by_relevance=True, extra_raw_filters=None): # Find model/queryset if isinstance(model_or_queryset, QuerySet): model = model_or_queryset.model queryset = model_or_queryset else: model = model_or_queryset queryset = model_or_queryset.objects.all() # Model must be a class that is in the index if not class_is_indexed(model): return [] # Check that theres still a query string after the clean up if query_string == "": return [] # Apply filters to queryset if filters: queryset = queryset.filter(**filters) # Prefetch related if prefetch_related: for prefetch in prefetch_related: queryset = queryset.prefetch_related(prefetch) # Check operator if operator is not None: operator = operator.lower() if operator not in ['or', 'and']: raise ValueError("operator must be either 'or' or 'and'") # Search search_query = self.query_class(queryset, query_string, fields=fields, operator=operator, order_by_relevance=order_by_relevance, extra_raw_filters=extra_raw_filters) return self.results_class(self, search_query)
def list(request): items = EndNote.objects.all() # Search is_searchable = class_is_indexed(EndNote) is_searching = False search_query = None if is_searchable and 'q' in request.GET: search_form = SearchForm(request.GET, placeholder=_("Search End Notes")) if search_form.is_valid(): search_query = search_form.cleaned_data['q'] search_backend = get_search_backend() items = search_backend.search(search_query, items) is_searching = True else: search_form = SearchForm(placeholder=_("Search End Notes")) # Pagination p = request.GET.get('p', 1) paginator = Paginator(items, 20) try: paginated_items = paginator.page(p) except PageNotAnInteger: paginated_items = paginator.page(1) except EmptyPage: paginated_items = paginator.page(paginator.num_pages) # Template if request.is_ajax(): template = 'content_notes/endnotes/results.html' else: template = 'content_notes/endnotes/type_index.html' return render( request, template, { 'items': paginated_items, 'is_searchable': is_searchable, 'search_form': search_form, 'is_searching': is_searching, 'query_string': search_query, })
def delete(self, obj): # Make sure the object can be indexed if not class_is_indexed(obj.__class__): return # Get mapping mapping = ElasticSearchMapping(obj.__class__) # Delete document try: self.es.delete( self.es_index, mapping.get_document_type(), mapping.get_document_id(obj), ) except NotFoundError: pass # Document doesn't exist, ignore this exception
def delete_item(self, item): # Make sure the object can be indexed if not class_is_indexed(item.__class__): return # Get mapping mapping = self.mapping_class(item.__class__) # Delete document try: self.es.delete( self.name, mapping.get_document_type(), mapping.get_document_id(item), ) except NotFoundError: pass # Document doesn't exist, ignore this exception
def add_bulk(self, model, obj_list): if not class_is_indexed(model): return # Get mapping mapping = ElasticSearchMapping(model) doc_type = mapping.get_document_type() # Create list of actions actions = [] for obj in obj_list: # Create the action action = {"_index": self.es_index, "_type": doc_type, "_id": mapping.get_document_id(obj)} action.update(mapping.get_document(obj)) actions.append(action) # Run the actions bulk(self.es, actions)
def get_queryset(self): queryset = super(SearchableListMixin, self).get_queryset() search_form = self.get_search_form() if search_form.is_valid(): q = search_form.cleaned_data['q'] if class_is_indexed(queryset.model): search_backend = get_search_backend() queryset = search_backend.search(q, queryset, fields=self.search_fields) else: filters = { field + '__icontains': q for field in self.search_fields or [] } queryset = queryset.filter(**filters) return queryset
def add_items(self, model, items): if not class_is_indexed(model): return # Get mapping mapping = self.mapping_class(model) doc_type = mapping.get_document_type() # Create list of actions actions = [] for item in items: # Create the action action = { '_index': self.name, '_type': doc_type, '_id': mapping.get_document_id(item), } action.update(mapping.get_document(item)) actions.append(action) # Run the actions bulk(self.es, actions)
def add_items(self, model, obj_list): if not class_is_indexed(model): return # Get mapping mapping = ElasticSearchMapping(model) doc_type = mapping.get_document_type() # Create list of actions actions = [] for obj in obj_list: # Create the action action = { '_index': self.index_name, '_type': doc_type, '_id': mapping.get_document_id(obj), } action.update(mapping.get_document(obj)) actions.append(action) # Run the actions bulk(self.es, actions)
def add_bulk(self, model, obj_list): if not class_is_indexed(model): return # Get mapping mapping = ElasticSearchMapping(model) doc_type = mapping.get_document_type() # Create list of actions actions = [] for obj in obj_list: # Create the action action = { '_index': self.es_index, '_type': doc_type, '_id': mapping.get_document_id(obj), } action.update(mapping.get_document(obj)) actions.append(action) # Run the actions bulk(self.es, actions)
def add_item(self, item): ''' Called when new item added to the index Need to override to include pipeline attribute for ingest_plugin attachment ''' # Make sure the object can be indexed if not class_is_indexed(item.__class__): return # Get mapping mapping = self.mapping_class(item.__class__) if mapping.get_document_type() == JOURNAL_DOCUMENT_TYPE: # sometime pipeline is missing. adding them to make sure they exists before using them in indexing) self.add_ingest_pipeline() results = self.es.index(self.name, mapping.get_document_type(), mapping.get_document(item), pipeline=INGEST_ATTACHMENT_ID, id=mapping.get_document_id(item)) log.info('in add_item with attachment results={results}'.format( results=results)) else: super(JournalsearchIndex, self).add_item(item)
def search(self, query_string, model_or_queryset, fields=None, filters=None, prefetch_related=None, operator=None, order_by_relevance=True): # Find model/queryset if isinstance(model_or_queryset, QuerySet): model = model_or_queryset.model queryset = model_or_queryset else: model = model_or_queryset queryset = model_or_queryset.objects.all() # Model must be a class that is in the index if not class_is_indexed(model): return [] # Check that theres still a query string after the clean up if query_string == "": return [] # Only fields that are indexed as a SearchField can be passed in fields if fields: allowed_fields = { field.field_name for field in model.get_searchable_search_fields() } for field_name in fields: if field_name not in allowed_fields: raise FieldError('Cannot search with field "' + field_name + '". Please add index.SearchField(\'' + field_name + '\') to ' + model.__name__ + '.search_fields.') # Apply filters to queryset if filters: queryset = queryset.filter(**filters) # Prefetch related if prefetch_related: for prefetch in prefetch_related: queryset = queryset.prefetch_related(prefetch) # Check operator if operator is not None: operator = operator.lower() if operator not in ['or', 'and']: raise ValueError("operator must be either 'or' or 'and'") # Search search_query = self.query_class(queryset, query_string, fields=fields, operator=operator, order_by_relevance=order_by_relevance) return self.results_class(self, search_query)
def list(request, content_type_app_name, content_type_model_name): content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name) model = content_type.model_class() permissions = [get_permission_name(action, model) for action in ["add", "change", "delete"]] if not any([request.user.has_perm(perm) for perm in permissions]): return permission_denied(request) snippet_type_name, snippet_type_name_plural = get_snippet_type_name(content_type) items = model.objects.all() # Search is_searchable = class_is_indexed(model) is_searching = False search_query = None if is_searchable and "q" in request.GET: search_form = SearchForm( request.GET, placeholder=_("Search %(snippet_type_name)s") % {"snippet_type_name": snippet_type_name_plural} ) if search_form.is_valid(): search_query = search_form.cleaned_data["q"] search_backend = get_search_backend() items = search_backend.search(search_query, items) is_searching = True else: search_form = SearchForm( placeholder=_("Search %(snippet_type_name)s") % {"snippet_type_name": snippet_type_name_plural} ) # Pagination p = request.GET.get("p", 1) paginator = Paginator(items, 20) try: paginated_items = paginator.page(p) except PageNotAnInteger: paginated_items = paginator.page(1) except EmptyPage: paginated_items = paginator.page(paginator.num_pages) # Template if request.is_ajax(): template = "wagtailsnippets/snippets/results.html" else: template = "wagtailsnippets/snippets/type_index.html" return render( request, template, { "content_type": content_type, "snippet_type_name": snippet_type_name, "snippet_type_name_plural": snippet_type_name_plural, "items": paginated_items, "can_add_snippet": request.user.has_perm(get_permission_name("add", model)), "is_searchable": is_searchable, "search_form": search_form, "is_searching": is_searching, "query_string": search_query, }, )
def list(request, content_type_app_name, content_type_model_name): content_type = get_content_type_from_url_params(content_type_app_name, content_type_model_name) model = content_type.model_class() permissions = [ get_permission_name(action, model) for action in ['add', 'change', 'delete'] ] if not any([request.user.has_perm(perm) for perm in permissions]): return permission_denied(request) snippet_type_name, snippet_type_name_plural = get_snippet_type_name( content_type) items = model.objects.all() # Search is_searchable = class_is_indexed(model) is_searching = False search_query = None if is_searchable and 'q' in request.GET: search_form = SearchForm( request.GET, placeholder=_("Search %(snippet_type_name)s") % {'snippet_type_name': snippet_type_name_plural}) if search_form.is_valid(): search_query = search_form.cleaned_data['q'] search_backend = get_search_backend() items = search_backend.search(search_query, items) is_searching = True else: search_form = SearchForm( placeholder=_("Search %(snippet_type_name)s") % {'snippet_type_name': snippet_type_name_plural}) # Pagination p = request.GET.get('p', 1) paginator = Paginator(items, 20) try: paginated_items = paginator.page(p) except PageNotAnInteger: paginated_items = paginator.page(1) except EmptyPage: paginated_items = paginator.page(paginator.num_pages) # Template if request.is_ajax(): template = 'wagtailsnippets/snippets/results.html' else: template = 'wagtailsnippets/snippets/type_index.html' return render( request, template, { 'content_type': content_type, 'snippet_type_name': snippet_type_name, 'snippet_type_name_plural': snippet_type_name_plural, 'items': paginated_items, 'can_add_snippet': request.user.has_perm(get_permission_name('add', model)), 'is_searchable': is_searchable, 'search_form': search_form, 'is_searching': is_searching, 'query_string': search_query, })