def question_revisions(request, question_id): """Revision history for a Question.""" question = get_object_or_404(Question, id=question_id) revisions = list(question.revisions.all()) populate_foreign_key_caches(User, ((revisions, ('author', )), ), fields=('username', 'gravatar', 'reputation', 'gold', 'silver', 'bronze')) for i, revision in enumerate(revisions): revision.html = QUESTION_REVISION_TEMPLATE % { 'title': revision.title, 'html': sanitize_html(markdowner.convert(revision.text)), 'tags': ' '.join([ '<a class="tag">%s</a>' % tag for tag in revision.tagnames.split(' ') ]), } if i > 0: revisions[i - 1].diff = htmldiff(revision.html, revisions[i - 1].html) return render_to_response('question_revisions.html', { 'title': u'Question Revisions', 'question': question, 'revisions': revisions, }, context_instance=RequestContext(request))
def answer_comments(request, answer_id, answer=None, form=None): """ Displays a single Answer and any Comments on it. This is primarily intended as a fallback for users who can't dynamically load Comments. """ if answer is None: answer = get_object_or_404(Answer, id=answer_id) populate_foreign_key_caches(User, (((answer, ), ('author', 'last_edited_by')), ), fields=('username', 'gravatar', 'reputation', 'gold', 'silver', 'bronze')) content_type = ContentType.objects.get_for_model(Answer) comments = Comment.objects.filter(content_type=content_type, object_id=answer.id) if form is None: form = CommentForm() return render_to_response('answer.html', { 'title': u'Answer Comments', 'answer': answer, 'comments': comments, 'comment_form': form, }, context_instance=RequestContext(request))
def question_comments(request, question, form=None): """ Displays a Question and any Comments on it. This is primarily intended as a fallback for users who can't dynamically load Comments. """ populate_foreign_key_caches( User, (((question, ), ('author', 'last_edited_by', 'closed_by')), ), fields=('username', 'gravatar', 'reputation', 'gold', 'silver', 'bronze')) content_type = ContentType.objects.get_for_model(Question) comments = Comment.objects.filter(content_type=content_type, object_id=question.id) if form is None: form = CommentForm() return render_to_response('question.html', { 'title': u'Comments on %s' % question.title, 'question': question, 'tags': question.tags.all(), 'comments': comments, 'comment_form': form, }, context_instance=RequestContext(request))
def answer_comments(request, answer_id, answer=None, form=None): """ Displays a single Answer and any Comments on it. This is primarily intended as a fallback for users who can't dynamically load Comments. """ if answer is None: answer = get_object_or_404(Answer, id=answer_id) populate_foreign_key_caches(User, ( ((answer,), ('author', 'last_edited_by')), ), fields=('username', 'gravatar', 'reputation', 'gold', 'silver', 'bronze')) content_type = ContentType.objects.get_for_model(Answer) comments = Comment.objects.filter(content_type=content_type, object_id=answer.id) if form is None: form = CommentForm() return render_to_response('answer.html', { 'title': u'Answer Comments', 'answer': answer, 'comments': comments, 'comment_form': form, }, context_instance=RequestContext(request))
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))
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))
def question_comments(request, question, form=None): """ Displays a Question and any Comments on it. This is primarily intended as a fallback for users who can't dynamically load Comments. """ populate_foreign_key_caches(User, ( ((question,), ('author', 'last_edited_by', 'closed_by')), ), fields=('username', 'gravatar', 'reputation', 'gold', 'silver', 'bronze')) content_type = ContentType.objects.get_for_model(Question) comments = Comment.objects.filter(content_type=content_type, object_id=question.id) if form is None: form = CommentForm() return render_to_response('question.html', { 'title': u'Comments on %s' % question.title, 'question': question, 'tags': question.tags.all(), 'comments': comments, 'comment_form': form, }, context_instance=RequestContext(request))
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))
def answer_revisions(request, answer_id): """Revision history for an Answer.""" answer = get_object_or_404(Answer, id=answer_id) revisions = list(answer.revisions.all()) populate_foreign_key_caches(User, ((revisions, ('author',)),), fields=('username', 'gravatar', 'reputation', 'gold', 'silver', 'bronze')) for i, revision in enumerate(revisions): revision.html = QUESTION_REVISION_TEMPLATE % { 'html': sanitize_html(markdowner.convert(revision.text)), } if i > 0: revisions[i - 1].diff = htmldiff(revision.html, revisions[i - 1].html) return render_to_response('answer_revisions.html', { 'title': u'Answer Revisions', 'answer': answer, 'revisions': revisions, }, context_instance=RequestContext(request))
def answer_revisions(request, answer_id): """Revision history for an Answer.""" answer = get_object_or_404(Answer, id=answer_id) revisions = list(answer.revisions.all()) populate_foreign_key_caches(User, ((revisions, ('author', )), ), fields=('username', 'gravatar', 'reputation', 'gold', 'silver', 'bronze')) for i, revision in enumerate(revisions): revision.html = QUESTION_REVISION_TEMPLATE % { 'html': sanitize_html(markdowner.convert(revision.text)), } if i > 0: revisions[i - 1].diff = htmldiff(revision.html, revisions[i - 1].html) return render_to_response('answer_revisions.html', { 'title': u'Answer Revisions', 'answer': answer, 'revisions': revisions, }, context_instance=RequestContext(request))
def question_revisions(request, question_id): """Revision history for a Question.""" question = get_object_or_404(Question, id=question_id) revisions = list(question.revisions.all()) populate_foreign_key_caches(User, ((revisions, ('author',)),), fields=('username', 'gravatar', 'reputation', 'gold', 'silver', 'bronze')) for i, revision in enumerate(revisions): revision.html = QUESTION_REVISION_TEMPLATE % { 'title': revision.title, 'html': sanitize_html(markdowner.convert(revision.text)), 'tags': ' '.join(['<a class="tag">%s</a>' % tag for tag in revision.tagnames.split(' ')]), } if i > 0: revisions[i - 1].diff = htmldiff(revision.html, revisions[i - 1].html) return render_to_response('question_revisions.html', { 'title': u'Question Revisions', 'question': question, 'revisions': revisions, }, context_instance=RequestContext(request))
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))