示例#1
0
def search(request):
    num_pages = 0
    limit = None
    nonvouched_only = False
    picture_only = False
    people = []
    show_pagination = False
    form = forms.SearchForm(request.GET)
    groups = None
    curated_groups = None

    if form.is_valid():
        query = form.cleaned_data.get('q', u'')
        limit = form.cleaned_data['limit']
        vouched = False if form.cleaned_data['nonvouched_only'] else None
        profilepic = True if form.cleaned_data['picture_only'] else None
        page = request.GET.get('page', 1)
        curated_groups = Group.get_curated()

        # If nothing has been entered don't load any searches.
        if not (not query and vouched is None and profilepic is None):
            profiles = UserProfile.search(query,
                                          vouched=vouched,
                                          photo=profilepic)
            groups = Group.search(query)

            paginator = Paginator(profiles, limit)

            try:
                people = paginator.page(page)
            except PageNotAnInteger:
                people = paginator.page(1)
            except EmptyPage:
                people = paginator.page(paginator.num_pages)

            if len(profiles) == 1 and not groups:
                return redirect(
                    reverse('profile', args=[people[0].user.username]))

            if paginator.count > forms.PAGINATION_LIMIT:
                show_pagination = True
                num_pages = len(people.paginator.page_range)

    d = dict(people=people,
             form=form,
             limit=limit,
             nonvouched_only=nonvouched_only,
             picture_only=picture_only,
             show_pagination=show_pagination,
             num_pages=num_pages,
             groups=groups,
             curated_groups=curated_groups)

    if request.is_ajax():
        return render(request, 'search_ajax.html', d)

    return render(request, 'phonebook/search.html', d)
示例#2
0
def search(request):
    num_pages = 0
    limit = None
    nonvouched_only = False
    picture_only = False
    people = []
    show_pagination = False
    form = forms.SearchForm(request.GET)
    groups = None
    curated_groups = None

    if form.is_valid():
        query = form.cleaned_data.get('q', u'')
        limit = form.cleaned_data['limit']
        vouched = False if form.cleaned_data['nonvouched_only'] else None
        profilepic = True if form.cleaned_data['picture_only'] else None
        page = request.GET.get('page', 1)
        curated_groups = Group.get_curated()

        # If nothing has been entered don't load any searches.
        if not (not query and vouched is None and profilepic is None):
            profiles = UserProfile.search(query,
                                          vouched=vouched,
                                          photo=profilepic)
            groups = Group.search(query)

            paginator = Paginator(profiles, limit)

            try:
                people = paginator.page(page)
            except PageNotAnInteger:
                people = paginator.page(1)
            except EmptyPage:
                people = paginator.page(paginator.num_pages)

            if len(profiles) == 1 and not groups:
                return redirect(reverse('profile',
                                        args=[people[0].user.username]))

            if paginator.count > forms.PAGINATION_LIMIT:
                show_pagination = True
                num_pages = len(people.paginator.page_range)

    d = dict(people=people,
             form=form,
             limit=limit,
             nonvouched_only=nonvouched_only,
             picture_only=picture_only,
             show_pagination=show_pagination,
             num_pages=num_pages,
             groups=groups,
             curated_groups=curated_groups)

    if request.is_ajax():
        return render(request, 'search_ajax.html', d)

    return render(request, 'phonebook/search.html', d)