def search_users(request): users = User.objects filtered = False search_form = UserSearchForm(request.GET, request.FILES) if search_form.is_valid(): filters = get_filters_from_row(search_form) if filters: users = users.filter(**filters) filtered = True if not filtered: users = users.all() query_string = None if ('q' in request.GET) and request.GET['q'].strip(): query_string = request.GET['q'] filter_query = get_query(query_string, [ 'username', 'first_name', 'last_name', 'email', ]) users = users.filter(filter_query) ordering = request.GET.get('order_by', None) if ordering is None: ordering = 'first_name' users = users.order_by(ordering) paginator = Paginator(users, profile.SEARCH_USERS_RESULTS_PER_PAGE) # Make sure page request is an int. If not, deliver first page. try: page = int(request.GET.get('page', '1')) except ValueError: page = 1 try: users = paginator.page(page) except (EmptyPage, InvalidPage): users = paginator.page(paginator.num_pages) return render( request, 'profile/search_user.html', { 'quicksearch': query_string, 'search_form': search_form, 'advanced_search': filtered, 'page': users, 'page_ordering': ordering })
def get_queryset(self): form = self.search_form(self.request.GET) users = User.objects filtered = False if form.is_valid(): filters = utils.get_filters_from_row(form) if filters: users = users.filter(**filters) filtered = True if not filtered: users = users.all() users, custom_filtered = utils.get_users_filtered_by_customfields(users, form) self.filtered = filtered | custom_filtered query_string = self.request.GET.get('q', None) if query_string: profile_fields = ['username', 'first_name', 'last_name', 'email'] users = users.filter(utils.get_query(query_string, profile_fields)) ordering = self.request.GET.get('order_by', self.default_order) return users.distinct().order_by(ordering)