Beispiel #1
0
def question(request, question_id):
    """Displays a Question."""
    if not request.user.is_authenticated():
        question = get_object_or_404(Question, id=question_id)
        favourite = False
    else:
        question = get_object_or_404(Question.objects.extra(
            select={
                'user_favourite_id':
                ('SELECT id FROM soclone_favouritequestion '
                 'WHERE question_id = soclone_question.id '
                 'AND user_id = %s'),
            },
            select_params=[request.user.id]),
                                     id=question_id)
        favourite = (question.user_favourite_id is not None)

    if 'showcomments' in request.GET:
        return question_comments(request, question)

    answer_sort_type = request.GET.get('sort', DEFAULT_ANSWER_SORT)
    if answer_sort_type not in ANSWER_SORT:
        answer_sort_type = DEFAULT_ANSWER_SORT
    order_by = ANSWER_SORT[answer_sort_type]
    paginator = Paginator(
        Answer.objects.for_question(question,
                                    request.user).order_by(*order_by),
        AUTO_WIKI_ANSWER_COUNT)
    # Save ourselves a COUNT() query by using the denormalised count
    paginator._count = question.answer_count
    page = get_page(request, paginator)
    answers = page.object_list

    populate_foreign_key_caches(User,
                                (((question, ),
                                  ('author', 'last_edited_by', 'closed_by')),
                                 (answers, ('author', 'last_edited_by'))),
                                fields=('username', 'gravatar', 'reputation',
                                        'gold', 'silver', 'bronze'))

    # Look up vote status for the current user
    question_vote, answer_votes = Vote.objects.get_for_question_and_answers(
        request.user, question, page.object_list)

    title = question.title
    if question.closed:
        title = '%s [closed]' % title
    return render_to_response('question.html', {
        'title': title,
        'question': question,
        'question_vote': question_vote,
        'favourite': favourite,
        'answers': page.object_list,
        'answer_votes': answer_votes,
        'page': page,
        'answer_sort': answer_sort_type,
        'answer_form': AddAnswerForm(),
        'tags': question.tags.all(),
    },
                              context_instance=RequestContext(request))
Beispiel #2
0
def question_list(request, question_views, template, questions_per_page=None,
                  page_number=None, extra_context=None):
    """
    Question list generic view.

    Allows the user to select from a number of ways of viewing questions,
    rendered with the given template.
    """
    view_id = request.GET.get('sort', None)
    view = dict([(q.id, q) for q in question_views]).get(view_id,
                                                         question_views[0])
    if questions_per_page is None:
        questions_per_page = get_questions_per_page(request.user)
    paginator = Paginator(view.get_queryset(), questions_per_page)
    if page_number is None:
        page = get_page(request, paginator)
    else:
        page = paginator.page(page_number)
    populate_foreign_key_caches(User, ((page.object_list, (view.user,)),),
                                fields=view.user_fields)
    context = {
        'title': view.page_title,
        'page': page,
        'questions': page.object_list,
        'current_view': view,
        'question_views': question_views,
    }
    if extra_context is not None:
        context.update(extra_context)
    return render_to_response(template, context,
                              context_instance=RequestContext(request))
Beispiel #3
0
def question(request, question_id):
    """Displays a Question."""
    if not request.user.is_authenticated():
        question = get_object_or_404(Question, id=question_id)
        favourite = False
    else:
        question = get_object_or_404(Question.objects.extra(
            select={
                'user_favourite_id': (
                    'SELECT id FROM soclone_favouritequestion '
                    'WHERE question_id = soclone_question.id '
                      'AND user_id = %s'),
            },
            select_params=[request.user.id]
        ), id=question_id)
        favourite = (question.user_favourite_id is not None)

    if 'showcomments' in request.GET:
        return question_comments(request, question)

    answer_sort_type = request.GET.get('sort', DEFAULT_ANSWER_SORT)
    if answer_sort_type not in ANSWER_SORT:
        answer_sort_type = DEFAULT_ANSWER_SORT
    order_by = ANSWER_SORT[answer_sort_type]
    paginator = Paginator(Answer.objects.for_question(
                              question, request.user).order_by(*order_by),
                          AUTO_WIKI_ANSWER_COUNT)
    # Save ourselves a COUNT() query by using the denormalised count
    paginator._count = question.answer_count
    page = get_page(request, paginator)
    answers = page.object_list

    populate_foreign_key_caches(User, (
            ((question,), ('author', 'last_edited_by', 'closed_by')),
            (answers,     ('author', 'last_edited_by'))
         ),
         fields=('username', 'gravatar', 'reputation', 'gold', 'silver',
                 'bronze'))

    # Look up vote status for the current user
    question_vote, answer_votes = Vote.objects.get_for_question_and_answers(
        request.user, question, page.object_list)

    title = question.title
    if question.closed:
        title = '%s [closed]' % title
    return render_to_response('question.html', {
        'title': title,
        'question': question,
        'question_vote': question_vote,
        'favourite': favourite,
        'answers': page.object_list,
        'answer_votes': answer_votes,
        'page': page,
        'answer_sort': answer_sort_type,
        'answer_form': AddAnswerForm(),
        'tags': question.tags.all(),
    }, context_instance=RequestContext(request))
Beispiel #4
0
def tags(request):
    """Searchable Tag list."""
    sort_type = request.GET.get('sort', DEFAULT_TAG_SORT)
    if sort_type not in TAG_SORT:
        sort_type = DEFAULT_TAG_SORT
    tags = Tag.objects.all().order_by(*TAG_SORT[sort_type])
    name_filter = request.GET.get('filter', '')
    if name_filter:
        tags = tags.filter(name__icontains=name_filter)
    paginator = Paginator(tags, 50)
    page = get_page(request, paginator)
    return render_to_response('tags.html', {
        'title': u'Tags',
        'tags': page.object_list,
        'page': page,
        'sort': sort_type,
        'filter': name_filter,
    }, context_instance=RequestContext(request))
Beispiel #5
0
def tags(request):
    """Searchable Tag list."""
    sort_type = request.GET.get('sort', DEFAULT_TAG_SORT)
    if sort_type not in TAG_SORT:
        sort_type = DEFAULT_TAG_SORT
    tags = Tag.objects.all().order_by(*TAG_SORT[sort_type])
    name_filter = request.GET.get('filter', '')
    if name_filter:
        tags = tags.filter(name__icontains=name_filter)
    paginator = Paginator(tags, 50)
    page = get_page(request, paginator)
    return render_to_response('tags.html', {
        'title': u'Tags',
        'tags': page.object_list,
        'page': page,
        'sort': sort_type,
        'filter': name_filter,
    },
                              context_instance=RequestContext(request))
Beispiel #6
0
def users(request):
    """Searchable User list."""
    sort_type = request.GET.get('sort', DEFAULT_USER_SORT)
    if sort_type not in USER_SORT:
        sort_type = DEFAULT_USER_SORT
    users = User.objects.all().order_by(*USER_SORT[sort_type])
    name_filter = request.GET.get('filter', '')
    if name_filter:
        users = users.filter(username__icontains=name_filter)
    users = users.values('id', 'username', 'gravatar',  'reputation', 'gold',
                         'silver', 'bronze')
    paginator = Paginator(users, 28)
    page = get_page(request, paginator)
    return render_to_response('users.html', {
        'title': u'Users',
        'users': page.object_list,
        'page': page,
        'sort': sort_type,
        'filter': name_filter,
    }, context_instance=RequestContext(request))
Beispiel #7
0
def users(request):
    """Searchable User list."""
    sort_type = request.GET.get('sort', DEFAULT_USER_SORT)
    if sort_type not in USER_SORT:
        sort_type = DEFAULT_USER_SORT
    users = User.objects.all().order_by(*USER_SORT[sort_type])
    name_filter = request.GET.get('filter', '')
    if name_filter:
        users = users.filter(username__icontains=name_filter)
    users = users.values('id', 'username', 'gravatar', 'reputation', 'gold',
                         'silver', 'bronze')
    paginator = Paginator(users, 28)
    page = get_page(request, paginator)
    return render_to_response('users.html', {
        'title': u'Users',
        'users': page.object_list,
        'page': page,
        'sort': sort_type,
        'filter': name_filter,
    },
                              context_instance=RequestContext(request))
Beispiel #8
0
def question_list(request,
                  question_views,
                  template,
                  questions_per_page=None,
                  page_number=None,
                  extra_context=None):
    """
    Question list generic view.

    Allows the user to select from a number of ways of viewing questions,
    rendered with the given template.
    """
    view_id = request.GET.get('sort', None)
    view = dict([(q.id, q)
                 for q in question_views]).get(view_id, question_views[0])
    if questions_per_page is None:
        questions_per_page = get_questions_per_page(request.user)
    paginator = Paginator(view.get_queryset(), questions_per_page)
    if page_number is None:
        page = get_page(request, paginator)
    else:
        page = paginator.page(page_number)
    populate_foreign_key_caches(User, ((page.object_list, (view.user, )), ),
                                fields=view.user_fields)
    context = {
        'title': view.page_title,
        'page': page,
        'questions': page.object_list,
        'current_view': view,
        'question_views': question_views,
    }
    if extra_context is not None:
        context.update(extra_context)
    return render_to_response(template,
                              context,
                              context_instance=RequestContext(request))