def chooser(request, get_results=False): # Get most popular queries queries = models.Query.get_most_popular() # If searching, filter results by query string query_string = None if 'q' in request.GET: searchform = SearchForm(request.GET) if searchform.is_valid(): query_string = searchform.cleaned_data['q'] queries = queries.filter( query_string__icontains=normalise_query_string(query_string)) else: searchform = SearchForm() paginator, queries = paginate(request, queries, per_page=10) # Render if get_results: return render(request, "tuiuiusearch/queries/chooser/results.html", { 'queries': queries, }) else: return render_modal_workflow( request, 'tuiuiusearch/queries/chooser/chooser.html', 'tuiuiusearch/queries/chooser/chooser.js', { 'queries': queries, 'searchform': searchform, })
def chooser(request): Document = get_document_model() if permission_policy.user_has_permission(request.user, 'add'): DocumentForm = get_document_form(Document) uploadform = DocumentForm(user=request.user) else: uploadform = None documents = Document.objects.all() # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_document_chooser_queryset'): documents = hook(documents, request) q = None if 'q' in request.GET or 'p' in request.GET or 'collection_id' in request.GET: collection_id = request.GET.get('collection_id') if collection_id: documents = documents.filter(collection=collection_id) searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] documents = documents.search(q) is_searching = True else: documents = documents.order_by('-created_at') is_searching = False # Pagination paginator, documents = paginate(request, documents, per_page=10) return render( request, "tuiuiudocs/chooser/results.html", { 'documents': documents, 'query_string': q, 'is_searching': is_searching, }) else: searchform = SearchForm() collections = Collection.objects.all() if len(collections) < 2: collections = None documents = documents.order_by('-created_at') paginator, documents = paginate(request, documents, per_page=10) return render_modal_workflow( request, 'tuiuiudocs/chooser/chooser.html', 'tuiuiudocs/chooser/chooser.js', { 'documents': documents, 'uploadform': uploadform, 'searchform': searchform, 'collections': collections, 'is_searching': False, })
def search(request): pages = [] q = None if 'q' in request.GET: form = SearchForm(request.GET) if form.is_valid(): q = form.cleaned_data['q'] pages = Page.objects.all().prefetch_related('content_type').search( q) paginator, pages = paginate(request, pages) else: form = SearchForm() if request.is_ajax(): return render( request, "tuiuiuadmin/pages/search_results.html", { 'pages': pages, 'query_string': q, 'pagination_query_params': ('q=%s' % q) if q else '' }) else: return render( request, "tuiuiuadmin/pages/search.html", { 'search_form': form, 'pages': pages, 'query_string': q, 'pagination_query_params': ('q=%s' % q) if q else '' })
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, "tuiuiusnippets/chooser/results.html", { 'model_opts': model._meta, 'items': paginated_items, 'query_string': search_query, 'is_searching': is_searching, }) return render_modal_workflow( request, 'tuiuiusnippets/chooser/choose.html', 'tuiuiusnippets/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 index(request): form = SearchForm(placeholder=_("Search something")) example_form = ExampleForm() messages.success(request, _("Success message"), buttons=[ messages.button('', _('View live')), messages.button('', _('Edit')) ]) messages.warning(request, _("Warning message"), buttons=[ messages.button('', _('View live')), messages.button('', _('Edit')) ]) messages.error(request, _("Error message"), buttons=[ messages.button('', _('View live')), messages.button('', _('Edit')) ]) paginator = Paginator(list(range(100)), 10) page = paginator.page(2) return render(request, 'styleguide/base.html', { 'search_form': form, 'example_form': example_form, 'example_page': page, })
def index(request): is_searching = False query_string = request.GET.get('q', "") queries = Query.objects.filter( editors_picks__isnull=False).distinct().order_by('query_string') # Search if query_string: queries = queries.filter(query_string__icontains=query_string) is_searching = True paginator, queries = paginate(request, queries) if request.is_ajax(): return render( request, "searchpromotions/results.html", { 'is_searching': is_searching, 'queries': queries, 'query_string': query_string, }) else: return render( request, 'searchpromotions/index.html', { 'is_searching': is_searching, 'queries': queries, 'query_string': query_string, 'search_form': SearchForm(data=dict(q=query_string) if query_string else None, placeholder=_("Search promoted results")), })
def index(request): q = None is_searching = False if 'q' in request.GET: form = SearchForm(request.GET, placeholder=_("Search groups")) if form.is_valid(): q = form.cleaned_data['q'] is_searching = True groups = Group.objects.filter(name__icontains=q) else: form = SearchForm(placeholder=_("Search groups")) if not is_searching: groups = Group.objects groups = groups.order_by('name') if 'ordering' in request.GET: ordering = request.GET['ordering'] if ordering in ['name', 'username']: if ordering != 'name': groups = groups.order_by(ordering) else: ordering = 'name' paginator, groups = paginate(request, groups) if request.is_ajax(): return render( request, "tuiuiuusers/groups/results.html", { 'groups': groups, 'is_searching': is_searching, 'query_string': q, 'ordering': ordering, }) else: return render( request, "tuiuiuusers/groups/index.html", { 'search_form': form, 'groups': groups, 'is_searching': is_searching, 'ordering': ordering, 'query_string': q, })
def render_html(self, request, current=None): search_areas = self.search_items_for_request(request) # Get query parameter form = SearchForm(request.GET) query = '' if form.is_valid(): query = form.cleaned_data['q'] # provide a hook for modifying the search area, if construct_hook_name has been set if self.construct_hook_name: for fn in hooks.get_hooks(self.construct_hook_name): fn(request, search_areas) rendered_search_areas = [] for item in search_areas: rendered_search_areas.append(item.render_html(request, query, current)) return mark_safe(''.join(rendered_search_areas))
def search(request, parent_page_id=None): # A missing or empty page_type parameter indicates 'all page types' (i.e. descendants of tuiuiucore.page) page_type_string = request.GET.get('page_type') or 'tuiuiucore.page' try: desired_classes = page_models_from_string(page_type_string) except (ValueError, LookupError): raise Http404 pages = Page.objects.all() # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_page_chooser_queryset'): pages = hook(pages, request) search_form = SearchForm(request.GET) if search_form.is_valid() and search_form.cleaned_data['q']: pages = pages.exclude(depth=1 # never include root ) pages = filter_page_type(pages, desired_classes) pages = pages.search(search_form.cleaned_data['q'], fields=['title']) else: pages = pages.none() paginator, pages = paginate(request, pages, per_page=25) for page in pages: page.can_choose = True return render( request, 'tuiuiuadmin/chooser/_search_results.html', shared_context( request, { 'searchform': search_form, 'pages': pages, 'page_type_string': page_type_string, }))
def chooser_upload(request): Image = get_image_model() ImageForm = get_image_form(Image) searchform = SearchForm() if request.method == 'POST': image = Image(uploaded_by_user=request.user) form = ImageForm(request.POST, request.FILES, instance=image, user=request.user) if form.is_valid(): form.save() # Reindex the image to make sure all tags are indexed search_index.insert_or_update_object(image) if request.GET.get('select_format'): form = ImageInsertionForm( initial={'alt_text': image.default_alt_text}) return render_modal_workflow( request, 'tuiuiuimages/chooser/select_format.html', 'tuiuiuimages/chooser/select_format.js', { 'image': image, 'form': form }) else: # not specifying a format; return the image details now return render_modal_workflow( request, None, 'tuiuiuimages/chooser/image_chosen.js', {'image_json': get_image_json(image)}) else: form = ImageForm(user=request.user) images = Image.objects.order_by('-created_at') paginator, images = paginate(request, images, per_page=12) return render_modal_workflow(request, 'tuiuiuimages/chooser/chooser.html', 'tuiuiuimages/chooser/chooser.js', { 'images': images, 'uploadform': form, 'searchform': searchform })
def index(request): query_string = request.GET.get('q', "") ordering = request.GET.get('ordering', 'old_path') redirects = models.Redirect.objects.prefetch_related('redirect_page', 'site') # Search if query_string: redirects = redirects.filter(old_path__icontains=query_string) # Ordering (A bit useless at the moment as only 'old_path' is allowed) if ordering not in ['old_path']: ordering = 'old_path' redirects = redirects.order_by(ordering) # Pagination paginator, redirects = paginate(request, redirects) # Render template if request.is_ajax(): return render(request, "tuiuiuredirects/results.html", { 'ordering': ordering, 'redirects': redirects, 'query_string': query_string, }) else: return render(request, "tuiuiuredirects/index.html", { 'ordering': ordering, 'redirects': redirects, 'query_string': query_string, 'search_form': SearchForm( data=dict(q=query_string) if query_string else None, placeholder=_("Search redirects") ), 'user_can_add': permission_policy.user_has_permission(request.user, 'add'), })
def index(request): q = None is_searching = False model_fields = [f.name for f in User._meta.get_fields()] if 'q' in request.GET: form = SearchForm(request.GET, placeholder=_("Search users")) if form.is_valid(): q = form.cleaned_data['q'] is_searching = True conditions = Q() if 'username' in model_fields: conditions |= Q(username__icontains=q) if 'first_name' in model_fields: conditions |= Q(first_name__icontains=q) if 'last_name' in model_fields: conditions |= Q(last_name__icontains=q) if 'email' in model_fields: conditions |= Q(email__icontains=q) users = User.objects.filter(conditions) else: form = SearchForm(placeholder=_("Search users")) if not is_searching: users = User.objects.all() if 'last_name' in model_fields and 'first_name' in model_fields: users = users.order_by('last_name', 'first_name') if 'ordering' in request.GET: ordering = request.GET['ordering'] if ordering == 'username': users = users.order_by(User.USERNAME_FIELD) else: ordering = 'name' paginator, users = paginate(request, users) if request.is_ajax(): return render( request, "tuiuiuusers/users/results.html", { 'users': users, 'is_searching': is_searching, 'query_string': q, 'ordering': ordering, }) else: return render( request, "tuiuiuusers/users/index.html", { 'search_form': form, 'users': users, 'is_searching': is_searching, 'ordering': ordering, 'query_string': q, })
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 = 'tuiuiusnippets/snippets/results.html' else: template = 'tuiuiusnippets/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 index(request): Document = get_document_model() # Get documents (filtered by user permission) documents = permission_policy.instances_user_has_any_permission_for( request.user, ['change', 'delete']) # Ordering if 'ordering' in request.GET and request.GET['ordering'] in [ 'title', '-created_at' ]: ordering = request.GET['ordering'] else: ordering = '-created_at' documents = documents.order_by(ordering) # Filter by collection current_collection = None collection_id = request.GET.get('collection_id') if collection_id: try: current_collection = Collection.objects.get(id=collection_id) documents = documents.filter(collection=current_collection) except (ValueError, Collection.DoesNotExist): pass # Search query_string = None if 'q' in request.GET: form = SearchForm(request.GET, placeholder=_("Search documents")) if form.is_valid(): query_string = form.cleaned_data['q'] documents = documents.search(query_string) else: form = SearchForm(placeholder=_("Search documents")) # Pagination paginator, documents = paginate(request, documents) collections = permission_policy.collections_user_has_any_permission_for( request.user, ['add', 'change']) if len(collections) < 2: collections = None # Create response if request.is_ajax(): return render( request, 'tuiuiudocs/documents/results.html', { 'ordering': ordering, 'documents': documents, 'query_string': query_string, 'is_searching': bool(query_string), }) else: return render( request, 'tuiuiudocs/documents/index.html', { 'ordering': ordering, 'documents': documents, 'query_string': query_string, 'is_searching': bool(query_string), 'search_form': form, 'popular_tags': popular_tags_for_model(Document), 'user_can_add': permission_policy.user_has_permission(request.user, 'add'), 'collections': collections, 'current_collection': current_collection, })
def index(request): Image = get_image_model() # Get images (filtered by user permission) images = permission_policy.instances_user_has_any_permission_for( request.user, ['change', 'delete']).order_by('-created_at') # Search query_string = None if 'q' in request.GET: form = SearchForm(request.GET, placeholder=_("Search images")) if form.is_valid(): query_string = form.cleaned_data['q'] images = images.search(query_string) else: form = SearchForm(placeholder=_("Search images")) # Filter by collection current_collection = None collection_id = request.GET.get('collection_id') if collection_id: try: current_collection = Collection.objects.get(id=collection_id) images = images.filter(collection=current_collection) except (ValueError, Collection.DoesNotExist): pass paginator, images = paginate(request, images) collections = permission_policy.collections_user_has_any_permission_for( request.user, ['add', 'change']) if len(collections) < 2: collections = None # Create response if request.is_ajax(): return render( request, 'tuiuiuimages/images/results.html', { 'images': images, 'query_string': query_string, 'is_searching': bool(query_string), }) else: return render( request, 'tuiuiuimages/images/index.html', { 'images': images, 'query_string': query_string, 'is_searching': bool(query_string), 'search_form': form, 'popular_tags': popular_tags_for_model(Image), 'collections': collections, 'current_collection': current_collection, 'user_can_add': permission_policy.user_has_permission(request.user, 'add'), })
def browse(request, parent_page_id=None): # A missing or empty page_type parameter indicates 'all page types' # (i.e. descendants of tuiuiucore.page) page_type_string = request.GET.get('page_type') or 'tuiuiucore.page' user_perm = request.GET.get('user_perms', False) try: desired_classes = page_models_from_string(page_type_string) except (ValueError, LookupError): raise Http404 # Find parent page if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id) elif desired_classes == (Page, ): # Just use the root page parent_page = Page.get_first_root_node() else: # Find the highest common ancestor for the specific classes passed in # In many cases, such as selecting an EventPage under an EventIndex, # this will help the administrator find their page quicker. all_desired_pages = filter_page_type(Page.objects.all(), desired_classes) parent_page = all_desired_pages.first_common_ancestor() # Get children of parent page pages = parent_page.get_children().specific() # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_page_chooser_queryset'): pages = hook(pages, request) # Filter them by page type if desired_classes != (Page, ): # restrict the page listing to just those pages that: # - are of the given content type (taking into account class inheritance) # - or can be navigated into (i.e. have children) choosable_pages = filter_page_type(pages, desired_classes) descendable_pages = pages.filter(numchild__gt=0) pages = choosable_pages | descendable_pages can_choose_root = request.GET.get('can_choose_root', False) # Do permission lookups for this user now, instead of for every page. permission_proxy = UserPagePermissionsProxy(request.user) # Parent page can be chosen if it is a instance of desired_classes parent_page.can_choose = can_choose_page(parent_page, permission_proxy, desired_classes, can_choose_root, user_perm) # Pagination # We apply pagination first so we don't need to walk the entire list # in the block below paginator, pages = paginate(request, pages, per_page=25) # Annotate each page with can_choose/can_decend flags for page in pages: page.can_choose = can_choose_page(page, permission_proxy, desired_classes, can_choose_root, user_perm) page.can_descend = page.get_children_count() # Render return render_modal_workflow( request, 'tuiuiuadmin/chooser/browse.html', 'tuiuiuadmin/chooser/browse.js', shared_context( request, { 'parent_page': parent_page, 'parent_page_id': parent_page.pk, 'pages': pages, 'search_form': SearchForm(), 'page_type_string': page_type_string, 'page_type_names': [ desired_class.get_verbose_name() for desired_class in desired_classes ], 'page_types_restricted': (page_type_string != 'tuiuiucore.page') }))
def chooser(request): Image = get_image_model() if permission_policy.user_has_permission(request.user, 'add'): ImageForm = get_image_form(Image) uploadform = ImageForm(user=request.user) else: uploadform = None images = Image.objects.order_by('-created_at') # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_image_chooser_queryset'): images = hook(images, request) q = None if ('q' in request.GET or 'p' in request.GET or 'tag' in request.GET or 'collection_id' in request.GET): # this request is triggered from search, pagination or 'popular tags'; # we will just render the results.html fragment collection_id = request.GET.get('collection_id') if collection_id: images = images.filter(collection=collection_id) searchform = SearchForm(request.GET) if searchform.is_valid(): q = searchform.cleaned_data['q'] images = images.search(q) is_searching = True else: is_searching = False tag_name = request.GET.get('tag') if tag_name: images = images.filter(tags__name=tag_name) # Pagination paginator, images = paginate(request, images, per_page=12) return render( request, "tuiuiuimages/chooser/results.html", { 'images': images, 'is_searching': is_searching, 'query_string': q, 'will_select_format': request.GET.get('select_format') }) else: searchform = SearchForm() collections = Collection.objects.all() if len(collections) < 2: collections = None paginator, images = paginate(request, images, per_page=12) return render_modal_workflow( request, 'tuiuiuimages/chooser/chooser.html', 'tuiuiuimages/chooser/chooser.js', { 'images': images, 'uploadform': uploadform, 'searchform': searchform, 'is_searching': False, 'query_string': q, 'will_select_format': request.GET.get('select_format'), 'popular_tags': popular_tags_for_model(Image), 'collections': collections, })