Esempio n. 1
0
def degrees(request):
    page = request.GET.get('p', 1)
    degrees = sorted(Degree.objects.all(), key=lambda x: x.name_())

    paginator = PageFilter(request.GET, degrees, 15)

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

    return render(request, 'degrees.html', { 'degrees': degrees })
Esempio n. 2
0
def categories(request):
    page = request.GET.get('p', 1)
    categories = sorted(Category.objects.all(), key=lambda x: x.name_())

    paginator = PageFilter(request.GET, categories, 15)

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

    return render(request, 'categories.html', { 'categories': categories })
Esempio n. 3
0
def questions(request):
    query = request.GET.get('q', None)
    c = request.GET.get('c', [])
    if len(c) > 0:
        c = clean_list(c.split('.'))
    d = request.GET.get('d', [])
    if len(d) > 0:
        d = clean_list(d.split('.'))
    page = request.GET.get('p', 1)
    o = request.GET.get('o', [])

    categories = []
    degrees = []

    questions = Question.objects.all()

    if query and not request.is_ajax():
        # custom search
        q = query.split()
        raw_query, params = searchQuestionSQL(q, limit=10, degrees=d, categories=c)
        questions = Question.objects.raw(raw_query, params)
    else:
        raw_query, params = searchQuestionSQL(degrees=d,
                categories=c)
        questions = Question.objects.raw(raw_query, params)
        # if len(c) > 0:
        #     questions = questions.filter(categories__in=c)
        # if len(d) > 0:
        #     questions = questions.filter(degrees__in=d)
        # if len(o) > 0:
        #     order = clean_order_list(o.split('.'))
        #     if len(order) > 0:
        #         questions = questions.order_by(*order)

        paginator = PageFilter(request.GET, list(questions), 10)

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

    # check if request is ajax
    if request.is_ajax() and query:
        # custom search
        q = query.split()
        raw_query, params = searchQuestionSQL(q, limit=5, degrees=d, categories=c)
        questions = Question.objects.raw(raw_query, params)

        user = request.user
        edit_perm = user.has_perm('qa.change_question')
        delete_perm = user.has_perm('qa.delete_question')
        # TODO grap category and degree from backend UI
        data = serializers.serialize("json", questions)
        response = { 'delete': delete_perm,
                     'edit': edit_perm,
                     'questions': data }
        return HttpResponse(json.dumps(response), content_type="application/json")


    filters = { 'categories': sorted(Category.objects.all(),
                                     key=lambda x: x.name_()),
                'degrees': sorted(Degree.objects.all(),
                                  key=lambda x: x.name_()) }

    return render(request, 'questions.html', { 'questions': questions,
                                               'filters': filters,
                                               'c': c,
                                               'd': d,
                                               'q': query })