Пример #1
0
def search(request) -> HttpResponse:
    """Search for tasks user has permission to see.
    """

    query_string = ''

    if request.POST:
        form = SearchForm(request.POST)

        if form.is_valid():
            query_string = form.cleaned_data.get('q', '').strip()

    else:
        if "q" in request.GET:
            query_string = request.GET["q"].strip()

        initial = {'q': query_string}
        form = SearchForm(initial=initial)

    context = {"form": form}

    if query_string != '':
        found_tasks = Task.objects.filter(is_active=True).filter(
            Q(title__icontains=query_string) | Q(note__icontains=query_string))

    else:
        found_tasks = None

    # Only include tasks that are in groups of which this user is a member:
    # if not request.user.is_superuser:
    if found_tasks is not None:
        if not staff_check(request.user):  # pai
            found_tasks = found_tasks.filter(
                Q(created_by=request.user) | Q(assigned_to=request.user)
                | Q(assigned_to__isnull=True,
                    task_list__group__in=get_user_groups(request.user)))

    # Pagination
    paginator = Paginator(found_tasks if found_tasks is not None else [], 10)

    page_number = request.GET.get('page')
    page_obj = paginator.get_page(page_number)

    context["page_obj"] = page_obj

    context["query_string"] = query_string
    context["found_tasks"] = found_tasks

    return render(request, "todo/search_results.html", context)
Пример #2
0
def search(request):
    response_data = {}
    if request.method == "POST":
        if request.user.is_authenticated:
            form = SearchForm(request.POST)
            if form.is_valid():
                string = form.data['result']
                response_data = {
                    'todo_list': Todos.search_todos(string, request.user)
                }
                result = 'Success'
            else:
                result = 'Form is not valid'
        else:
            result = 'user is not authenticated'
        response_data['result'] = result
        return HttpResponse(json.dumps(response_data),
                            content_type="application/json")
    else:
        return HttpResponseRedirect('/')