コード例 #1
0
def search(request):
    if request.method != 'POST':
        return JsonResponse({
            "error": "Must use POST to this endpoint"
        }, status=405)

    search = SearchService.SearchService()
    post_request = loads(request.body)
    sort_field = post_request.get("sortField", None)
    filter_field = post_request.get("filterField", None)
    filter_topics = post_request.get("filterTopics", None)
    query = post_request.get("query", None)
    sort_order = post_request.get("sortOrder", None)
    page = post_request.get("page", None)

    if sort_field is None and filter_field is None and query is None:
        found_questions = QuestionService.allQuestions()
        return page_response(found_questions, page)

    if sort_field is not None:
        search.addSort(sort_field, sort_order)

    if filter_field is not None:
        search.addFilter(filter_field)

    if filter_topics is not None:
        search.addTopicFilter(filter_topics)

    if query is not None:
        search.textSearch(query)

    try:
        search_result = search.execute()
        return page_response(search_result, page)
    except TypeError:
        all_questions = QuestionService.allQuestions()
        return page_response(all_questions, page)
コード例 #2
0
def all(request):
    all_questions = [x.toJSON() for x in QuestionService.allQuestions()]
    return JsonResponse(all_questions, safe=False)
コード例 #3
0
def page(request, page):
    return page_response(QuestionService.allQuestions(), page)