summaryrefslogtreecommitdiffstats
path: root/forum/views.py
diff options
context:
space:
mode:
Diffstat (limited to 'forum/views.py')
-rw-r--r--forum/views.py840
1 files changed, 418 insertions, 422 deletions
diff --git a/forum/views.py b/forum/views.py
index 296745f9..65b80d0e 100644
--- a/forum/views.py
+++ b/forum/views.py
@@ -1,11 +1,6 @@
# encoding:utf-8
-import os.path
-import time, datetime, calendar, random
-import logging
-from urllib import quote, unquote
+import calendar
from django.conf import settings
-from django.core.files.storage import default_storage
-from django.shortcuts import render_to_response, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseForbidden, Http404
from django.core.paginator import Paginator, EmptyPage, InvalidPage
@@ -25,12 +20,17 @@ from django.core.exceptions import PermissionDenied
from utils.html import sanitize_html
from utils.decorators import ajax_method, ajax_login_required
from markdown2 import Markdown
-#from lxml.html.diff import htmldiff
+import os.path
+import random
+import time
+
+import datetime
+from forum import auth
+from forum.auth import *
+from forum.const import *
from forum.diff import textDiff as htmldiff
from forum.forms import *
from forum.models import *
-from forum.auth import *
-from forum.const import *
from forum.user import *
from forum import auth
from django_authopenid.util import get_next_url
@@ -162,10 +162,8 @@ def questions(request, tagname=None, unanswered=False):
# template file
# "questions.html" or maybe index.html in the future
template_file = "questions.html"
- # Set flag to False by default. If it is equal to True, then need to be saved.
- pagesize_changed = False
# get pagesize from session, if failed then get default value
- pagesize = request.session.get("pagesize",10)
+ pagesize = request.session.get("pagesize", 10)
try:
page = int(request.GET.get('page', '1'))
except ValueError:
@@ -277,12 +275,12 @@ def create_new_answer( question=None, author=None,\
#create answer
answer = Answer(
- question = question,
- author = author,
- added_at = added_at,
- wiki = wiki,
- html = html
- )
+ question=question,
+ author=author,
+ added_at=added_at,
+ wiki=wiki,
+ html=html
+ )
if answer.wiki:
answer.last_edited_by = answer.author
answer.last_edited_at = added_at
@@ -298,13 +296,13 @@ def create_new_answer( question=None, author=None,\
#update revision
AnswerRevision.objects.create(
- answer = answer,
- revision = 1,
- author = author,
- revised_at = added_at,
- summary = CONST['default_version'],
- text = text
- )
+ answer=answer,
+ revision=1,
+ author=author,
+ revised_at=added_at,
+ summary=CONST['default_version'],
+ text=text
+ )
#set notification/delete
if email_notify:
@@ -317,24 +315,24 @@ def create_new_answer( question=None, author=None,\
except:
pass
-def create_new_question(title=None,author=None,added_at=None,
- wiki=False,tagnames=None,summary=None,
+def create_new_question(title=None, author=None, added_at=None,
+ wiki=False, tagnames=None, summary=None,
text=None):
"""this is not a view
and maybe should become one of the methods on Question object?
"""
html = sanitize_html(markdowner.convert(text))
question = Question(
- title = title,
- author = author,
- added_at = added_at,
- last_activity_at = added_at,
- last_activity_by = author,
- wiki = wiki,
- tagnames = tagnames,
- html = html,
- summary = summary
- )
+ title=title,
+ author=author,
+ added_at=added_at,
+ last_activity_at=added_at,
+ last_activity_by=author,
+ wiki=wiki,
+ tagnames=tagnames,
+ html=html,
+ summary=summary
+ )
if question.wiki:
question.last_edited_by = question.author
question.last_edited_at = added_at
@@ -344,19 +342,19 @@ def create_new_question(title=None,author=None,added_at=None,
# create the first revision
QuestionRevision.objects.create(
- question = question,
- revision = 1,
- title = question.title,
- author = author,
- revised_at = added_at,
- tagnames = question.tagnames,
- summary = CONST['default_version'],
- text = text
- )
+ question=question,
+ revision=1,
+ title=question.title,
+ author=author,
+ revised_at=added_at,
+ tagnames=question.tagnames,
+ summary=CONST['default_version'],
+ text=text
+ )
return question
#TODO: allow anynomus user to ask question by providing email and username.
-#@login_required
+@login_required
def ask(request):
if request.method == "POST":
form = AskForm(request.POST)
@@ -374,29 +372,28 @@ def ask(request):
author = request.user
question = create_new_question(
- title = title,
- author = author,
- added_at = added_at,
- wiki = wiki,
- tagnames = tagnames,
- summary = summary,
- text = text
- )
-
+ title=title,
+ author=author,
+ added_at=added_at,
+ wiki=wiki,
+ tagnames=tagnames,
+ summary=summary,
+ text=text
+ )
return HttpResponseRedirect(question.get_absolute_url())
else:
request.session.flush()
session_key = request.session.session_key
question = AnonymousQuestion(
- session_key = session_key,
- title = title,
- tagnames = tagnames,
- wiki = wiki,
- text = text,
- summary = summary,
- added_at = added_at,
- ip_addr = request.META['REMOTE_ADDR'],
- )
+ session_key=session_key,
+ title=title,
+ tagnames=tagnames,
+ wiki=wiki,
+ text=text,
+ summary=summary,
+ added_at=added_at,
+ ip_addr=request.META['REMOTE_ADDR'],
+ )
question.save()
return HttpResponseRedirect(reverse('user_signin_new_question'))
else:
@@ -438,8 +435,8 @@ def question(request, id):
question = get_object_or_404(Question, id=id)
if question.deleted and not can_view_deleted_post(request.user, question):
raise Http404
- answer_form = AnswerForm(question,request.user)
- answers = Answer.objects.get_answers_from_question(question, request.user)
+ answer_form = AnswerForm(question, request.user)
+ answers = Answer.objects.get_answers_from_question(question, request.user, orderby)
answers = answers.select_related(depth=1)
favorited = question.has_favorite_by_user(request.user)
@@ -451,9 +448,8 @@ def question(request, id):
question_vote = question_vote[0]
user_answer_votes = {}
- for answer in answers:
- vote = answer.get_user_vote(request.user)
- if vote is not None and not user_answer_votes.has_key(answer.id):
+ for vote in question.get_user_votes_in_answers(request.user):
+ if not user_answer_votes.has_key(vote.object_id):
vote_value = -1
if vote.is_upvote():
vote_value = 1
@@ -505,28 +501,28 @@ def question(request, id):
question_view.save()
return render_to_response('question.html', {
- "question" : question,
- "question_vote" : question_vote,
- "question_comment_count":question.comments.count(),
- "answer" : answer_form,
- "answers" : page_objects.object_list,
- "user_answer_votes": user_answer_votes,
- "tags" : question.tags.all(),
- "tab_id" : view_id,
- "favorited" : favorited,
- "similar_questions" : Question.objects.get_similar_questions(question),
- "context" : {
- 'is_paginated' : True,
- 'pages': objects_list.num_pages,
- 'page': page,
- 'has_previous': page_objects.has_previous(),
- 'has_next': page_objects.has_next(),
- 'previous': page_objects.previous_page_number(),
- 'next': page_objects.next_page_number(),
- 'base_url' : request.path + '?sort=%s&' % view_id,
- 'extend_url' : "#sort-top"
- }
- }, context_instance=RequestContext(request))
+ "question": question,
+ "question_vote": question_vote,
+ "question_comment_count":question.comments.count(),
+ "answer": answer_form,
+ "answers": page_objects.object_list,
+ "user_answer_votes": user_answer_votes,
+ "tags": question.tags.all(),
+ "tab_id": view_id,
+ "favorited": favorited,
+ "similar_questions": Question.objects.get_similar_questions(question),
+ "context": {
+ 'is_paginated': True,
+ 'pages': objects_list.num_pages,
+ 'page': page,
+ 'has_previous': page_objects.has_previous(),
+ 'has_next': page_objects.has_next(),
+ 'previous': page_objects.previous_page_number(),
+ 'next': page_objects.next_page_number(),
+ 'base_url': request.path + '?sort=%s&' % view_id,
+ 'extend_url': "#sort-top"
+ }
+ }, context_instance=RequestContext(request))
@login_required
def close(request, id):
@@ -546,9 +542,9 @@ def close(request, id):
else:
form = CloseForm()
return render_to_response('close.html', {
- 'form' : form,
- 'question' : question,
- }, context_instance=RequestContext(request))
+ 'form': form,
+ 'question': question,
+ }, context_instance=RequestContext(request))
@login_required
def reopen(request, id):
@@ -556,14 +552,14 @@ def reopen(request, id):
# open question
if not can_reopen_question(request.user, question):
return HttpResponse('Permission denied.')
- if request.method == 'POST' :
+ if request.method == 'POST':
Question.objects.filter(id=question.id).update(closed=False,
- closed_by=None, closed_at=None, close_reason=None)
+ closed_by=None, closed_at=None, close_reason=None)
return HttpResponseRedirect(question.get_absolute_url())
else:
return render_to_response('reopen.html', {
- 'question' : question,
- }, context_instance=RequestContext(request))
+ 'question': question,
+ }, context_instance=RequestContext(request))
@login_required
def edit_question(request, id):
@@ -586,25 +582,25 @@ def _retag_question(request, question):
retagged_at = datetime.datetime.now()
# Update the Question itself
Question.objects.filter(id=question.id).update(
- tagnames = form.cleaned_data['tags'],
- last_edited_at = retagged_at,
- last_edited_by = request.user,
- last_activity_at = retagged_at,
- last_activity_by = request.user
- )
+ tagnames=form.cleaned_data['tags'],
+ last_edited_at=retagged_at,
+ last_edited_by=request.user,
+ last_activity_at=retagged_at,
+ last_activity_by=request.user
+ )
# Update the Question's tag associations
tags_updated = Question.objects.update_tags(question,
- form.cleaned_data['tags'], request.user)
+ form.cleaned_data['tags'], request.user)
# Create a new revision
QuestionRevision.objects.create(
- question = question,
- title = latest_revision.title,
- author = request.user,
- revised_at = retagged_at,
- tagnames = form.cleaned_data['tags'],
- summary = CONST['retagged'],
- text = latest_revision.text
- )
+ question=question,
+ title=latest_revision.title,
+ author=request.user,
+ revised_at=retagged_at,
+ tagnames=form.cleaned_data['tags'],
+ summary=CONST['retagged'],
+ text=latest_revision.text
+ )
# send tags updated singal
tags_updated.send(sender=question.__class__, question=question)
@@ -612,10 +608,10 @@ def _retag_question(request, question):
else:
form = RetagQuestionForm(question)
return render_to_response('question_retag.html', {
- 'question': question,
- 'form' : form,
- 'tags' : _get_tags_cache_json(),
- }, context_instance=RequestContext(request))
+ 'question': question,
+ 'form': form,
+ 'tags': _get_tags_cache_json(),
+ }, context_instance=RequestContext(request))
def _edit_question(request, question):
latest_revision = question.get_latest_revision()
@@ -627,8 +623,8 @@ def _edit_question(request, question):
if revision_form.is_valid():
# Replace with those from the selected revision
form = EditQuestionForm(question,
- QuestionRevision.objects.get(question=question,
- revision=revision_form.cleaned_data['revision']))
+ QuestionRevision.objects.get(question=question,
+ revision=revision_form.cleaned_data['revision']))
else:
form = EditQuestionForm(question, latest_revision, request.POST)
else:
@@ -662,20 +658,20 @@ def _edit_question(request, question):
updated_fields['wikified_at'] = edited_at
Question.objects.filter(
- id=question.id).update(**updated_fields)
+ id=question.id).update(** updated_fields)
# Update the Question's tag associations
if tags_changed:
tags_updated = Question.objects.update_tags(
- question, form.cleaned_data['tags'], request.user)
+ question, form.cleaned_data['tags'], request.user)
# Create a new revision
revision = QuestionRevision(
- question = question,
- title = form.cleaned_data['title'],
- author = request.user,
- revised_at = edited_at,
- tagnames = form.cleaned_data['tags'],
- text = form.cleaned_data['text'],
- )
+ question=question,
+ title=form.cleaned_data['title'],
+ author=request.user,
+ revised_at=edited_at,
+ tagnames=form.cleaned_data['tags'],
+ text=form.cleaned_data['text'],
+ )
if form.cleaned_data['summary']:
revision.summary = form.cleaned_data['summary']
else:
@@ -688,11 +684,11 @@ def _edit_question(request, question):
revision_form = RevisionForm(question, latest_revision)
form = EditQuestionForm(question, latest_revision)
return render_to_response('question_edit.html', {
- 'question': question,
- 'revision_form': revision_form,
- 'form' : form,
- 'tags' : _get_tags_cache_json()
- }, context_instance=RequestContext(request))
+ 'question': question,
+ 'revision_form': revision_form,
+ 'form': form,
+ 'tags': _get_tags_cache_json()
+ }, context_instance=RequestContext(request))
@login_required
@@ -726,7 +722,7 @@ def edit_answer(request, id):
'last_edited_by': request.user,
'html': html,
}
- Answer.objects.filter(id=answer.id).update(**updated_fields)
+ Answer.objects.filter(id=answer.id).update(** updated_fields)
revision = AnswerRevision(
answer=answer,
@@ -758,7 +754,6 @@ def edit_answer(request, id):
QUESTION_REVISION_TEMPLATE = ('<h1>%(title)s</h1>\n'
'<div class="text">%(html)s</div>\n'
'<div class="tags">%(tags)s</div>')
-
def question_revisions(request, id):
post = get_object_or_404(Question, id=id)
revisions = list(post.revisions.all())
@@ -768,7 +763,7 @@ def question_revisions(request, id):
'title': revision.title,
'html': sanitize_html(markdowner.convert(revision.text)),
'tags': ' '.join(['<a class="post-tag">%s</a>' % tag
- for tag in revision.tagnames.split(' ')]),
+ for tag in revision.tagnames.split(' ')]),
}
if i > 0:
revisions[i].diff = htmldiff(revisions[i-1].html, revision.html)
@@ -804,6 +799,7 @@ def answer_revisions(request, id):
'revisions': revisions,
}, context_instance=RequestContext(request))
+@login_required
def answer(request, id):
question = get_object_or_404(Question, id=id)
if request.method == "POST":
@@ -853,11 +849,11 @@ def tags(request):
if stag != '':
objects_list = Paginator(Tag.objects.filter(deleted=False).exclude(used_count=0).extra(where=['name like %s'], params=['%' + stag + '%']), DEFAULT_PAGE_SIZE)
else:
- if sortby == "name":
- objects_list = Paginator(Tag.objects.all().filter(deleted=False).exclude(used_count=0).order_by("name"), DEFAULT_PAGE_SIZE)
+ if sortby == "used":
+ sortby = "-used_count"
else:
- objects_list = Paginator(Tag.objects.all().filter(deleted=False).exclude(used_count=0).order_by("-used_count"), DEFAULT_PAGE_SIZE)
-
+ sortby = "name"
+ objects_list = Paginator(Tag.objects.all().filter(deleted=False).exclude(used_count=0).order_by(sortby), DEFAULT_PAGE_SIZE)
try:
tags = objects_list.page(page)
except (EmptyPage, InvalidPage):
@@ -924,9 +920,9 @@ def vote(request, id):
response_data = {
"allowed": 1,
"success": 1,
- "status" : 0,
- "count" : 0,
- "message" : ''
+ "status": 0,
+ "count": 0,
+ "message": ''
}
def can_vote(vote_score, user):
@@ -1151,7 +1147,7 @@ def ajax_command(request):
def users(request):
is_paginated = True
sortby = request.GET.get('sort', 'reputation')
- suser = request.REQUEST.get('q', "")
+ suser = request.REQUEST.get('q', "")
try:
page = int(request.GET.get('page', '1'))
except ValueError:
@@ -1261,43 +1257,43 @@ def edit_user(request, id):
def user_stats(request, user_id, user_view):
user = get_object_or_404(User, id=user_id)
questions = Question.objects.extra(
- select={
- 'vote_count' : 'question.score',
- 'favorited_myself' : 'SELECT count(*) FROM favorite_question f WHERE f.user_id = %s AND f.question_id = question.id',
- 'la_user_id' : 'auth_user.id',
- 'la_username' : 'auth_user.username',
- 'la_user_gold' : 'auth_user.gold',
- 'la_user_silver' : 'auth_user.silver',
- 'la_user_bronze' : 'auth_user.bronze',
- 'la_user_reputation' : 'auth_user.reputation'
- },
- select_params=[user_id],
- tables=['question', 'auth_user'],
- where=['question.deleted = 0 AND question.author_id=%s AND question.last_activity_by_id = auth_user.id'],
- params=[user_id],
- order_by=['-vote_count', '-last_activity_at']
- ).values('vote_count',
- 'favorited_myself',
- 'id',
- 'title',
- 'author_id',
- 'added_at',
- 'answer_accepted',
- 'answer_count',
- 'comment_count',
- 'view_count',
- 'favourite_count',
- 'summary',
- 'tagnames',
- 'vote_up_count',
- 'vote_down_count',
- 'last_activity_at',
- 'la_user_id',
- 'la_username',
- 'la_user_gold',
- 'la_user_silver',
- 'la_user_bronze',
- 'la_user_reputation')[:100]
+ select={
+ 'vote_count': 'question.score',
+ 'favorited_myself': 'SELECT count(*) FROM favorite_question f WHERE f.user_id = %s AND f.question_id = question.id',
+ 'la_user_id': 'auth_user.id',
+ 'la_username': 'auth_user.username',
+ 'la_user_gold': 'auth_user.gold',
+ 'la_user_silver': 'auth_user.silver',
+ 'la_user_bronze': 'auth_user.bronze',
+ 'la_user_reputation': 'auth_user.reputation'
+ },
+ select_params=[user_id],
+ tables=['question', 'auth_user'],
+ where=['question.deleted = 0 AND question.author_id=%s AND question.last_activity_by_id = auth_user.id'],
+ params=[user_id],
+ order_by=['-vote_count', '-last_activity_at']
+ ).values('vote_count',
+ 'favorited_myself',
+ 'id',
+ 'title',
+ 'author_id',
+ 'added_at',
+ 'answer_accepted',
+ 'answer_count',
+ 'comment_count',
+ 'view_count',
+ 'favourite_count',
+ 'summary',
+ 'tagnames',
+ 'vote_up_count',
+ 'vote_down_count',
+ 'last_activity_at',
+ 'la_user_id',
+ 'la_username',
+ 'la_user_gold',
+ 'la_user_silver',
+ 'la_user_bronze',
+ 'la_user_reputation')[:100]
answered_questions = Question.objects.extra(
select={
@@ -1616,30 +1612,30 @@ def user_recent(request, user_id, user_view):
)
if len(accept_answers) > 0:
accept_answers = [(Event(q['added_at'], q['activity_type'], q['title'], '', '0', \
- q['question_id'])) for q in accept_answers]
+ q['question_id'])) for q in accept_answers]
activities.extend(accept_answers)
#award history
awards = Activity.objects.extra(
- select={
- 'badge_id' : 'badge.id',
- 'awarded_at': 'award.awarded_at',
- 'activity_type' : 'activity.activity_type'
- },
- tables=['activity', 'award', 'badge'],
- where=['activity.user_id = award.user_id AND activity.user_id = %s AND '+
- 'award.badge_id=badge.id AND activity.object_id=award.id AND activity.activity_type=%s'],
- params=[user_id, TYPE_ACTIVITY_PRIZE],
- order_by=['-activity.active_at']
- ).values(
- 'badge_id',
- 'awarded_at',
- 'activity_type'
- )
+ select={
+ 'badge_id': 'badge.id',
+ 'awarded_at': 'award.awarded_at',
+ 'activity_type': 'activity.activity_type'
+ },
+ tables=['activity', 'award', 'badge'],
+ where=['activity.user_id = award.user_id AND activity.user_id = %s AND ' +
+ 'award.badge_id=badge.id AND activity.object_id=award.id AND activity.activity_type=%s'],
+ params=[user_id, TYPE_ACTIVITY_PRIZE],
+ order_by=['-activity.active_at']
+ ).values(
+ 'badge_id',
+ 'awarded_at',
+ 'activity_type'
+ )
if len(awards) > 0:
awards = [(AwardEvent(q['awarded_at'], q['activity_type'], q['badge_id'])) for q in awards]
activities.extend(awards)
- activities.sort(lambda x,y: cmp(y.time, x.time))
+ activities.sort(lambda x, y: cmp(y.time, x.time))
return render_to_response(user_view.template_file,{
"tab_name" : user_view.id,
@@ -1696,7 +1692,7 @@ def user_responses(request, user_id, user_view):
if len(answers) > 0:
answers = [(Response(TYPE_RESPONSE['QUESTION_ANSWERED'], a['title'], a['question_id'],
- a['answer_id'], a['added_at'], a['username'], a['user_id'], a['html'])) for a in answers]
+ a['answer_id'], a['added_at'], a['username'], a['user_id'], a['html'])) for a in answers]
responses.extend(answers)
@@ -1726,83 +1722,83 @@ def user_responses(request, user_id, user_view):
if len(comments) > 0:
comments = [(Response(TYPE_RESPONSE['QUESTION_COMMENTED'], c['title'], c['question_id'],
- '', c['added_at'], c['username'], c['user_id'], c['comment'])) for c in comments]
+ '', c['added_at'], c['username'], c['user_id'], c['comment'])) for c in comments]
responses.extend(comments)
# answer comments
comments = Comment.objects.extra(
- select={
- 'title' : 'question.title',
- 'question_id' : 'question.id',
- 'answer_id' : 'answer.id',
- 'added_at' : 'comment.added_at',
- 'comment' : 'comment.comment',
- 'username' : 'auth_user.username',
- 'user_id' : 'auth_user.id'
- },
- tables=['answer', 'auth_user', 'comment', 'question'],
- where=['answer.deleted = 0 AND answer.author_id = %s AND comment.object_id=answer.id AND '+
- 'comment.content_type_id=%s AND comment.user_id <> %s AND comment.user_id = auth_user.id '+
- 'AND question.id = answer.question_id'],
- params=[user_id, answer_type_id, user_id],
- order_by=['-comment.added_at']
- ).values(
- 'title',
- 'question_id',
- 'answer_id',
- 'added_at',
- 'comment',
- 'username',
- 'user_id'
- )
+ select={
+ 'title': 'question.title',
+ 'question_id': 'question.id',
+ 'answer_id': 'answer.id',
+ 'added_at': 'comment.added_at',
+ 'comment': 'comment.comment',
+ 'username': 'auth_user.username',
+ 'user_id': 'auth_user.id'
+ },
+ tables=['answer', 'auth_user', 'comment', 'question'],
+ where=['answer.deleted = 0 AND answer.author_id = %s AND comment.object_id=answer.id AND ' +
+ 'comment.content_type_id=%s AND comment.user_id <> %s AND comment.user_id = auth_user.id ' +
+ 'AND question.id = answer.question_id'],
+ params=[user_id, answer_type_id, user_id],
+ order_by=['-comment.added_at']
+ ).values(
+ 'title',
+ 'question_id',
+ 'answer_id',
+ 'added_at',
+ 'comment',
+ 'username',
+ 'user_id'
+ )
if len(comments) > 0:
comments = [(Response(TYPE_RESPONSE['ANSWER_COMMENTED'], c['title'], c['question_id'],
- c['answer_id'], c['added_at'], c['username'], c['user_id'], c['comment'])) for c in comments]
+ c['answer_id'], c['added_at'], c['username'], c['user_id'], c['comment'])) for c in comments]
responses.extend(comments)
# answer has been accepted
answers = Answer.objects.extra(
- select={
- 'title' : 'question.title',
- 'question_id' : 'question.id',
- 'answer_id' : 'answer.id',
- 'added_at' : 'answer.accepted_at',
- 'html' : 'answer.html',
- 'username' : 'auth_user.username',
- 'user_id' : 'auth_user.id'
- },
- select_params=[user_id],
- tables=['answer', 'question', 'auth_user'],
- where=['answer.question_id = question.id AND answer.deleted=0 AND question.deleted = 0 AND '+
- 'answer.author_id = %s AND answer.accepted=1 AND question.author_id=auth_user.id'],
- params=[user_id],
- order_by=['-answer.id']
- ).values(
- 'title',
- 'question_id',
- 'answer_id',
- 'added_at',
- 'html',
- 'username',
- 'user_id'
- )
+ select={
+ 'title': 'question.title',
+ 'question_id': 'question.id',
+ 'answer_id': 'answer.id',
+ 'added_at': 'answer.accepted_at',
+ 'html': 'answer.html',
+ 'username': 'auth_user.username',
+ 'user_id': 'auth_user.id'
+ },
+ select_params=[user_id],
+ tables=['answer', 'question', 'auth_user'],
+ where=['answer.question_id = question.id AND answer.deleted=0 AND question.deleted = 0 AND ' +
+ 'answer.author_id = %s AND answer.accepted=1 AND question.author_id=auth_user.id'],
+ params=[user_id],
+ order_by=['-answer.id']
+ ).values(
+ 'title',
+ 'question_id',
+ 'answer_id',
+ 'added_at',
+ 'html',
+ 'username',
+ 'user_id'
+ )
if len(answers) > 0:
answers = [(Response(TYPE_RESPONSE['ANSWER_ACCEPTED'], a['title'], a['question_id'],
- a['answer_id'], a['added_at'], a['username'], a['user_id'], a['html'])) for a in answers]
+ a['answer_id'], a['added_at'], a['username'], a['user_id'], a['html'])) for a in answers]
responses.extend(answers)
# sort posts by time
- responses.sort(lambda x,y: cmp(y.time, x.time))
+ responses.sort(lambda x, y: cmp(y.time, x.time))
- return render_to_response(user_view.template_file,{
- "tab_name" : user_view.id,
- "tab_description" : user_view.tab_description,
- "page_title" : user_view.page_title,
- "view_user" : user,
- "responses" : responses[:user_view.data_size],
+ return render_to_response(user_view.template_file, {
+ "tab_name": user_view.id,
+ "tab_description": user_view.tab_description,
+ "page_title": user_view.page_title,
+ "view_user": user,
+ "responses": responses[:user_view.data_size],
- }, context_instance=RequestContext(request))
+ }, context_instance=RequestContext(request))
def user_votes(request, user_id, user_view):
user = get_object_or_404(User, id=user_id)
@@ -1810,61 +1806,61 @@ def user_votes(request, user_id, user_view):
raise Http404
votes = []
question_votes = Vote.objects.extra(
- select={
- 'title' : 'question.title',
- 'question_id' : 'question.id',
- 'answer_id' : 0,
- 'voted_at' : 'vote.voted_at',
- 'vote' : 'vote',
- },
- select_params=[user_id],
- tables=['vote', 'question', 'auth_user'],
- where=['vote.content_type_id = %s AND vote.user_id = %s AND vote.object_id = question.id '+
- 'AND vote.user_id=auth_user.id'],
- params=[question_type_id, user_id],
- order_by=['-vote.id']
- ).values(
- 'title',
- 'question_id',
- 'answer_id',
- 'voted_at',
- 'vote',
- )
+ select={
+ 'title': 'question.title',
+ 'question_id': 'question.id',
+ 'answer_id': 0,
+ 'voted_at': 'vote.voted_at',
+ 'vote': 'vote',
+ },
+ select_params=[user_id],
+ tables=['vote', 'question', 'auth_user'],
+ where=['vote.content_type_id = %s AND vote.user_id = %s AND vote.object_id = question.id ' +
+ 'AND vote.user_id=auth_user.id'],
+ params=[question_type_id, user_id],
+ order_by=['-vote.id']
+ ).values(
+ 'title',
+ 'question_id',
+ 'answer_id',
+ 'voted_at',
+ 'vote',
+ )
if(len(question_votes) > 0):
votes.extend(question_votes)
answer_votes = Vote.objects.extra(
- select={
- 'title' : 'question.title',
- 'question_id' : 'question.id',
- 'answer_id' : 'answer.id',
- 'voted_at' : 'vote.voted_at',
- 'vote' : 'vote',
- },
- select_params=[user_id],
- tables=['vote', 'answer', 'question', 'auth_user'],
- where=['vote.content_type_id = %s AND vote.user_id = %s AND vote.object_id = answer.id '+
- 'AND answer.question_id = question.id AND vote.user_id=auth_user.id'],
- params=[answer_type_id, user_id],
- order_by=['-vote.id']
- ).values(
- 'title',
- 'question_id',
- 'answer_id',
- 'voted_at',
- 'vote',
- )
+ select={
+ 'title': 'question.title',
+ 'question_id': 'question.id',
+ 'answer_id': 'answer.id',
+ 'voted_at': 'vote.voted_at',
+ 'vote': 'vote',
+ },
+ select_params=[user_id],
+ tables=['vote', 'answer', 'question', 'auth_user'],
+ where=['vote.content_type_id = %s AND vote.user_id = %s AND vote.object_id = answer.id ' +
+ 'AND answer.question_id = question.id AND vote.user_id=auth_user.id'],
+ params=[answer_type_id, user_id],
+ order_by=['-vote.id']
+ ).values(
+ 'title',
+ 'question_id',
+ 'answer_id',
+ 'voted_at',
+ 'vote',
+ )
if(len(answer_votes) > 0):
votes.extend(answer_votes)
- votes.sort(lambda x,y: cmp(y['voted_at'], x['voted_at']))
- return render_to_response(user_view.template_file,{
- "tab_name" : user_view.id,
- "tab_description" : user_view.tab_description,
- "page_title" : user_view.page_title,
- "view_user" : user,
- "votes" : votes[:user_view.data_size]
+ votes.sort(lambda x, y: cmp(y['voted_at'], x['voted_at']))
+ return render_to_response(user_view.template_file, {
+ "tab_name": user_view.id,
+ "tab_description": user_view.tab_description,
+ "page_title": user_view.page_title,
+ "view_user": user,
+ "votes": votes[:user_view.data_size]
- }, context_instance=RequestContext(request))
+ }, context_instance=RequestContext(request))
def user_reputation(request, user_id, user_view):
user = get_object_or_404(User, id=user_id)
@@ -1909,52 +1905,52 @@ def user_reputation(request, user_id, user_view):
def user_favorites(request, user_id, user_view):
user = get_object_or_404(User, id=user_id)
questions = Question.objects.extra(
- select={
- 'vote_count' : 'question.vote_up_count + question.vote_down_count',
- 'favorited_myself' : 'SELECT count(*) FROM favorite_question f WHERE f.user_id = %s '+
- 'AND f.question_id = question.id',
- 'la_user_id' : 'auth_user.id',
- 'la_username' : 'auth_user.username',
- 'la_user_gold' : 'auth_user.gold',
- 'la_user_silver' : 'auth_user.silver',
- 'la_user_bronze' : 'auth_user.bronze',
- 'la_user_reputation' : 'auth_user.reputation'
- },
- select_params=[user_id],
- tables=['question', 'auth_user', 'favorite_question'],
- where=['question.deleted = 0 AND question.last_activity_by_id = auth_user.id '+
- 'AND favorite_question.question_id = question.id AND favorite_question.user_id = %s'],
- params=[user_id],
- order_by=['-vote_count', '-question.id']
- ).values('vote_count',
- 'favorited_myself',
- 'id',
- 'title',
- 'author_id',
- 'added_at',
- 'answer_accepted',
- 'answer_count',
- 'comment_count',
- 'view_count',
- 'favourite_count',
- 'summary',
- 'tagnames',
- 'vote_up_count',
- 'vote_down_count',
- 'last_activity_at',
- 'la_user_id',
- 'la_username',
- 'la_user_gold',
- 'la_user_silver',
- 'la_user_bronze',
- 'la_user_reputation')
- return render_to_response(user_view.template_file,{
- "tab_name" : user_view.id,
- "tab_description" : user_view.tab_description,
- "page_title" : user_view.page_title,
- "questions" : questions[:user_view.data_size],
- "view_user" : user
- }, context_instance=RequestContext(request))
+ select={
+ 'vote_count': 'question.vote_up_count + question.vote_down_count',
+ 'favorited_myself': 'SELECT count(*) FROM favorite_question f WHERE f.user_id = %s ' +
+ 'AND f.question_id = question.id',
+ 'la_user_id': 'auth_user.id',
+ 'la_username': 'auth_user.username',
+ 'la_user_gold': 'auth_user.gold',
+ 'la_user_silver': 'auth_user.silver',
+ 'la_user_bronze': 'auth_user.bronze',
+ 'la_user_reputation': 'auth_user.reputation'
+ },
+ select_params=[user_id],
+ tables=['question', 'auth_user', 'favorite_question'],
+ where=['question.deleted = 0 AND question.last_activity_by_id = auth_user.id ' +
+ 'AND favorite_question.question_id = question.id AND favorite_question.user_id = %s'],
+ params=[user_id],
+ order_by=['-vote_count', '-question.id']
+ ).values('vote_count',
+ 'favorited_myself',
+ 'id',
+ 'title',
+ 'author_id',
+ 'added_at',
+ 'answer_accepted',
+ 'answer_count',
+ 'comment_count',
+ 'view_count',
+ 'favourite_count',
+ 'summary',
+ 'tagnames',
+ 'vote_up_count',
+ 'vote_down_count',
+ 'last_activity_at',
+ 'la_user_id',
+ 'la_username',
+ 'la_user_gold',
+ 'la_user_silver',
+ 'la_user_bronze',
+ 'la_user_reputation')
+ return render_to_response(user_view.template_file, {
+ "tab_name": user_view.id,
+ "tab_description": user_view.tab_description,
+ "page_title": user_view.page_title,
+ "questions": questions[:user_view.data_size],
+ "view_user": user
+ }, context_instance=RequestContext(request))
def user_email_subscriptions(request, user_id, user_view):
user = get_object_or_404(User, id=user_id)
@@ -2084,21 +2080,21 @@ def badges(request):
def badge(request, id):
badge = get_object_or_404(Badge, id=id)
awards = Award.objects.extra(
- select={'id': 'auth_user.id',
- 'name': 'auth_user.username',
- 'rep':'auth_user.reputation',
- 'gold': 'auth_user.gold',
- 'silver': 'auth_user.silver',
- 'bronze': 'auth_user.bronze'},
- tables=['award', 'auth_user'],
- where=['badge_id=%s AND user_id=auth_user.id'],
- params=[id]
- ).values('id').distinct()
+ select={'id': 'auth_user.id',
+ 'name': 'auth_user.username',
+ 'rep':'auth_user.reputation',
+ 'gold': 'auth_user.gold',
+ 'silver': 'auth_user.silver',
+ 'bronze': 'auth_user.bronze'},
+ tables=['award', 'auth_user'],
+ where=['badge_id=%s AND user_id=auth_user.id'],
+ params=[id]
+ ).values('id').distinct()
return render_to_response('badge.html', {
- 'awards' : awards,
- 'badge' : badge,
- }, context_instance=RequestContext(request))
+ 'awards': awards,
+ 'badge': badge,
+ }, context_instance=RequestContext(request))
def read_message(request):
if request.method == "POST":
@@ -2189,7 +2185,7 @@ def book(request, short_name, unanswered=False):
page = 1
view_id = request.GET.get('sort', None)
- view_dic = {"latest":"-added_at", "active":"-last_activity_at", "hottest":"-answer_count", "mostvoted":"-score" }
+ view_dic = {"latest":"-added_at", "active":"-last_activity_at", "hottest":"-answer_count", "mostvoted":"-score"}
try:
orderby = view_dic[view_id]
except KeyError:
@@ -2210,22 +2206,22 @@ def book(request, short_name, unanswered=False):
questions = objects_list.page(page)
return render_to_response('book.html', {
- "book" : book,
- "author_info" : author_info,
- "author_rss" : author_rss,
- "questions" : questions,
- "context" : {
- 'is_paginated' : True,
- 'pages': objects_list.num_pages,
- 'page': page,
- 'has_previous': questions.has_previous(),
- 'has_next': questions.has_next(),
- 'previous': questions.previous_page_number(),
- 'next': questions.next_page_number(),
- 'base_url' : request.path + '?sort=%s&' % view_id,
- 'pagesize' : user_page_size
- }
- }, context_instance=RequestContext(request))
+ "book": book,
+ "author_info": author_info,
+ "author_rss": author_rss,
+ "questions": questions,
+ "context": {
+ 'is_paginated': True,
+ 'pages': objects_list.num_pages,
+ 'page': page,
+ 'has_previous': questions.has_previous(),
+ 'has_next': questions.has_next(),
+ 'previous': questions.previous_page_number(),
+ 'next': questions.next_page_number(),
+ 'base_url': request.path + '?sort=%s&' % view_id,
+ 'pagesize': user_page_size
+ }
+ }, context_instance=RequestContext(request))
@login_required
def ask_book(request, short_name):
@@ -2235,16 +2231,16 @@ def ask_book(request, short_name):
added_at = datetime.datetime.now()
html = sanitize_html(markdowner.convert(form.cleaned_data['text']))
question = Question(
- title = strip_tags(form.cleaned_data['title']),
- author = request.user,
- added_at = added_at,
- last_activity_at = added_at,
- last_activity_by = request.user,
- wiki = form.cleaned_data['wiki'],
- tagnames = form.cleaned_data['tags'].strip(),
- html = html,
- summary = strip_tags(html)[:120]
- )
+ title=strip_tags(form.cleaned_data['title']),
+ author=request.user,
+ added_at=added_at,
+ last_activity_at=added_at,
+ last_activity_by=request.user,
+ wiki=form.cleaned_data['wiki'],
+ tagnames=form.cleaned_data['tags'].strip(),
+ html=html,
+ summary=strip_tags(html)[:120]
+ )
if question.wiki:
question.last_edited_by = question.author
question.last_edited_at = added_at
@@ -2254,15 +2250,15 @@ def ask_book(request, short_name):
# create the first revision
QuestionRevision.objects.create(
- question = question,
- revision = 1,
- title = question.title,
- author = request.user,
- revised_at = added_at,
- tagnames = question.tagnames,
- summary = CONST['default_version'],
- text = form.cleaned_data['text']
- )
+ question=question,
+ revision=1,
+ title=question.title,
+ author=request.user,
+ revised_at=added_at,
+ tagnames=question.tagnames,
+ summary=CONST['default_version'],
+ text=form.cleaned_data['text']
+ )
books = Book.objects.extra(where=['short_name = %s'], params=[short_name])
match_count = len(books)
@@ -2331,7 +2327,7 @@ def search(request):
user.save()
view_id = request.GET.get('sort', None)
- view_dic = {"latest":"-added_at", "active":"-last_activity_at", "hottest":"-answer_count", "mostvoted":"-score" }
+ view_dic = {"latest":"-added_at", "active":"-last_activity_at", "hottest":"-answer_count", "mostvoted":"-score"}
try:
orderby = view_dic[view_id]
except KeyError:
@@ -2360,25 +2356,25 @@ def search(request):
related_tags.append(tag)
return render_to_response(template_file, {
- "questions" : questions,
- "tab_id" : view_id,
- "questions_count" : objects_list.count,
- "tags" : related_tags,
- "searchtag" : None,
- "searchtitle" : keywords,
- "keywords" : keywords,
- "is_unanswered" : False,
- "context" : {
- 'is_paginated' : True,
- 'pages': objects_list.num_pages,
- 'page': page,
- 'has_previous': questions.has_previous(),
- 'has_next': questions.has_next(),
- 'previous': questions.previous_page_number(),
- 'next': questions.next_page_number(),
- 'base_url' : request.path + '?t=question&q=%s&sort=%s&' % (keywords, view_id),
- 'pagesize' : pagesize
- }}, context_instance=RequestContext(request))
+ "questions": questions,
+ "tab_id": view_id,
+ "questions_count": objects_list.count,
+ "tags": related_tags,
+ "searchtag": None,
+ "searchtitle": keywords,
+ "keywords": keywords,
+ "is_unanswered": False,
+ "context": {
+ 'is_paginated': True,
+ 'pages': objects_list.num_pages,
+ 'page': page,
+ 'has_previous': questions.has_previous(),
+ 'has_next': questions.has_next(),
+ 'previous': questions.previous_page_number(),
+ 'next': questions.next_page_number(),
+ 'base_url': request.path + '?t=question&q=%s&sort=%s&' % (keywords, view_id),
+ 'pagesize': pagesize
+ }}, context_instance=RequestContext(request))
else:
raise Http404