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 move_choose_destination(request, page_to_move_id, viewed_page_id=None): page_to_move = get_object_or_404(Page, id=page_to_move_id) page_perms = page_to_move.permissions_for_user(request.user) if not page_perms.can_move(): raise PermissionDenied if viewed_page_id: viewed_page = get_object_or_404(Page, id=viewed_page_id) else: viewed_page = Page.get_first_root_node() viewed_page.can_choose = page_perms.can_move_to(viewed_page) child_pages = [] for target in viewed_page.get_children(): # can't move the page into itself or its descendants target.can_choose = page_perms.can_move_to(target) target.can_descend = ( not (target == page_to_move or target.is_child_of(page_to_move)) and target.get_children_count()) child_pages.append(target) # Pagination paginator, child_pages = paginate(request, child_pages, per_page=50) return render( request, 'tuiuiuadmin/pages/move_choose_destination.html', { 'page_to_move': page_to_move, 'viewed_page': viewed_page, 'child_pages': child_pages, })
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 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 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 index(request): form_pages = get_forms_for_user(request.user) paginator, form_pages = paginate(request, form_pages) return render(request, 'tuiuiuforms/index.html', { 'form_pages': form_pages, })
def usage(request, image_id): image = get_object_or_404(get_image_model(), id=image_id) paginator, used_by = paginate(request, image.get_usage()) return render(request, "tuiuiuimages/images/usage.html", { 'image': image, 'used_by': used_by })
def usage(request, document_id): Document = get_document_model() doc = get_object_or_404(Document, id=document_id) paginator, used_by = paginate(request, doc.get_usage()) return render(request, "tuiuiudocs/documents/usage.html", { 'document': doc, 'used_by': used_by })
def usage(request, app_label, model_name, id): model = get_snippet_model_from_url_params(app_label, model_name) instance = get_object_or_404(model, id=id) paginator, used_by = paginate(request, instance.get_usage()) return render(request, "tuiuiusnippets/snippets/usage.html", { 'instance': instance, 'used_by': used_by })
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 get_context(self, request): # Get blog entries entries = self.get_blog_entries() # Filter by tag tag = request.GET.get('tag') if tag: entries = entries.filter(tags__name=tag) paginator, entries = paginate(request, entries, page_key='page', per_page=10) # Update template context context = super(BlogIndexPage, self).get_context(request) context['entries'] = entries return context
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 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 revisions_index(request, page_id): page = get_object_or_404(Page, id=page_id).specific # Get page ordering ordering = request.GET.get('ordering', '-created_at') if ordering not in [ 'created_at', '-created_at', ]: ordering = '-created_at' revisions = page.revisions.order_by(ordering) paginator, revisions = paginate(request, revisions) return render( request, 'tuiuiuadmin/pages/revisions/index.html', { 'page': page, 'ordering': ordering, 'pagination_query_params': "ordering=%s" % ordering, 'revisions': revisions, })
def content_type_use(request, content_type_app_name, content_type_model_name): try: content_type = ContentType.objects.get_by_natural_key( content_type_app_name, content_type_model_name) except ContentType.DoesNotExist: raise Http404 page_class = content_type.model_class() # page_class must be a Page type and not some other random model if not issubclass(page_class, Page): raise Http404 pages = page_class.objects.all() paginator, pages = paginate(request, pages, per_page=10) return render( request, 'tuiuiuadmin/pages/content_type_use.html', { 'pages': pages, 'app_name': content_type_app_name, 'content_type': content_type, 'page_class': page_class, })
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 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 index(request, parent_page_id=None): if parent_page_id: parent_page = get_object_or_404(Page, id=parent_page_id).specific else: parent_page = Page.get_first_root_node().specific pages = parent_page.get_children().prefetch_related( 'content_type', 'sites_rooted_here') # Get page ordering ordering = request.GET.get('ordering', '-latest_revision_created_at') if ordering not in [ 'title', '-title', 'content_type', '-content_type', 'live', '-live', 'latest_revision_created_at', '-latest_revision_created_at', 'ord' ]: ordering = '-latest_revision_created_at' if ordering == 'ord': # preserve the native ordering from get_children() pass elif ordering == 'latest_revision_created_at': # order by oldest revision first. # Special case NULL entries - these should go at the top of the list. # Do this by annotating with Count('latest_revision_created_at'), # which returns 0 for these pages = pages.annotate( null_position=Count('latest_revision_created_at')).order_by( 'null_position', 'latest_revision_created_at') elif ordering == '-latest_revision_created_at': # order by oldest revision first. # Special case NULL entries - these should go at the end of the list. pages = pages.annotate( null_position=Count('latest_revision_created_at')).order_by( '-null_position', '-latest_revision_created_at') else: pages = pages.order_by(ordering) # Don't paginate if sorting by page order - all pages must be shown to # allow drag-and-drop reordering do_paginate = ordering != 'ord' if do_paginate: # Retrieve pages in their most specific form. # Only do this for paginated listings, as this could potentially be a # very expensive operation when performed on a large queryset. pages = pages.specific() # allow hooks to modify the queryset for hook in hooks.get_hooks('construct_explorer_page_queryset'): pages = hook(parent_page, pages, request) # Pagination if do_paginate: paginator, pages = paginate(request, pages, per_page=50) return render( request, 'tuiuiuadmin/pages/index.html', { 'parent_page': parent_page.specific, 'ordering': ordering, 'pagination_query_params': "ordering=%s" % ordering, 'pages': pages, 'do_paginate': do_paginate, })
def list_submissions(request, page_id): if not get_forms_for_user(request.user).filter(id=page_id).exists(): raise PermissionDenied form_page = get_object_or_404(Page, id=page_id).specific form_submission_class = form_page.get_submission_class() data_fields = form_page.get_data_fields() submissions = form_submission_class.objects.filter( page=form_page).order_by('submit_time') data_headings = [label for name, label in data_fields] select_date_form = SelectDateForm(request.GET) if select_date_form.is_valid(): date_from = select_date_form.cleaned_data.get('date_from') date_to = select_date_form.cleaned_data.get('date_to') # careful: date_to should be increased by 1 day since the submit_time # is a time so it will always be greater if date_to: date_to += datetime.timedelta(days=1) if date_from and date_to: submissions = submissions.filter( submit_time__range=[date_from, date_to]) elif date_from and not date_to: submissions = submissions.filter(submit_time__gte=date_from) elif not date_from and date_to: submissions = submissions.filter(submit_time__lte=date_to) if request.GET.get('action') == 'CSV': # return a CSV instead response = HttpResponse(content_type='text/csv; charset=utf-8') response['Content-Disposition'] = 'attachment;filename=export.csv' # Prevents UnicodeEncodeError for labels with non-ansi symbols data_headings = [smart_str(label) for label in data_headings] writer = csv.writer(response) writer.writerow(data_headings) for s in submissions: data_row = [] form_data = s.get_data() for name, label in data_fields: val = form_data.get(name) if isinstance(val, list): val = ', '.join(val) data_row.append(smart_str(val)) writer.writerow(data_row) return response paginator, submissions = paginate(request, submissions) data_rows = [] for s in submissions: form_data = s.get_data() data_row = [] for name, label in data_fields: val = form_data.get(name) if isinstance(val, list): val = ', '.join(val) data_row.append(val) data_rows.append({"model_id": s.id, "fields": data_row}) return render( request, 'tuiuiuforms/index_submissions.html', { 'form_page': form_page, 'select_date_form': select_date_form, 'submissions': submissions, 'data_headings': data_headings, 'data_rows': data_rows })
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, })
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, })