diff options
Diffstat (limited to 'forum/views.py')
-rw-r--r-- | forum/views.py | 1253 |
1 files changed, 630 insertions, 623 deletions
diff --git a/forum/views.py b/forum/views.py index 53338dd3..8ff44f4b 100644 --- a/forum/views.py +++ b/forum/views.py @@ -1,33 +1,33 @@ # 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 from django.template import RequestContext, loader -from django.utils.html import * -from django.utils import simplejson -from django.core import serializers from django.core.mail import mail_admins -from django.db import transaction from django.contrib.contenttypes.models import ContentType -from django.utils.translation import ugettext as _ from django.template.defaultfilters import slugify from django.core.exceptions import PermissionDenied - from utils.html import sanitize_html +from django.core.files.storage import default_storage +from django.shortcuts import get_object_or_404 +from django.shortcuts import render_to_response +from django.utils import simplejson +from django.utils.html import * +from django.utils.translation import ugettext as _ 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 @@ -50,7 +50,7 @@ answer_type = ContentType.objects.get_for_model(Answer) comment_type = ContentType.objects.get_for_model(Comment) question_revision_type = ContentType.objects.get_for_model(QuestionRevision) answer_revision_type = ContentType.objects.get_for_model(AnswerRevision) -repute_type =ContentType.objects.get_for_model(Repute) +repute_type = ContentType.objects.get_for_model(Repute) question_type_id = question_type.id answer_type_id = answer_type.id comment_type_id = comment_type.id @@ -61,7 +61,7 @@ def _get_tags_cache_json(): tags = Tag.objects.filter(deleted=False).all() tags_list = [] for tag in tags: - dic = {'n': tag.name, 'c': tag.used_count } + dic = {'n': tag.name, 'c': tag.used_count} tags_list.append(dic) tags = simplejson.dumps(tags_list) return tags @@ -95,11 +95,11 @@ def index(request): awards = Award.objects.get_recent_awards() return render_to_response('index.html', { - "questions" : questions, - "tab_id" : view_id, - "tags" : tags, - "awards" : awards[:INDEX_AWARD_SIZE], - }, context_instance=RequestContext(request)) + "questions": questions, + "tab_id": view_id, + "tags": tags, + "awards": awards[:INDEX_AWARD_SIZE], + }, context_instance=RequestContext(request)) def about(request): return render_to_response('about.html', context_instance=RequestContext(request)) @@ -147,10 +147,8 @@ def questions(request, tagname=None, unanswered=False): # template file # "questions.html" or "unanswered.html" 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: @@ -189,39 +187,39 @@ def questions(request, tagname=None, unanswered=False): else: related_tags = None return render_to_response(template_file, { - "questions" : questions, - "author_name" : author_name, - "tab_id" : view_id, - "questions_count" : objects_list.count, - "tags" : related_tags, - "searchtag" : tagname, - "is_unanswered" : unanswered, - "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' : pagesize - }}, context_instance=RequestContext(request)) - -def create_new_answer( question=None, author=None,\ - added_at=None, wiki=False,\ - text='', email_notify=False): + "questions": questions, + "author_name": author_name, + "tab_id": view_id, + "questions_count": objects_list.count, + "tags": related_tags, + "searchtag": tagname, + "is_unanswered": unanswered, + "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': pagesize + }}, context_instance=RequestContext(request)) + +def create_new_answer(question=None, author=None, \ + added_at=None, wiki=False, \ + text='', email_notify=False): html = sanitize_html(markdowner.convert(text)) #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 @@ -237,13 +235,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: @@ -256,24 +254,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 @@ -283,15 +281,15 @@ 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. @@ -302,7 +300,7 @@ def ask(request): if form.is_valid(): added_at = datetime.datetime.now() - title = strip_tags(form.cleaned_data['title'].strip()) + title = strip_tags(form.cleaned_data['title']).strip() wiki = form.cleaned_data['wiki'] tagnames = form.cleaned_data['tags'].strip() text = form.cleaned_data['text'] @@ -313,29 +311,29 @@ 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: @@ -377,8 +375,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) @@ -390,14 +388,14 @@ 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 - user_answer_votes[answer.id] = vote_value + user_answer_votes[vote.object_id] = vote_value + #do we need this? if answers is not None: answers = answers.order_by("-accepted", orderby) @@ -444,28 +442,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): @@ -485,9 +483,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): @@ -495,14 +493,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): @@ -525,25 +523,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) @@ -551,10 +549,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): @@ -567,8 +565,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: @@ -602,20 +600,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: @@ -628,11 +626,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 @@ -651,8 +649,8 @@ def edit_answer(request, id): if revision_form.is_valid(): # Replace with those from the selected revision form = EditAnswerForm(answer, - AnswerRevision.objects.get(answer=answer, - revision=revision_form.cleaned_data['revision'])) + AnswerRevision.objects.get(answer=answer, + revision=revision_form.cleaned_data['revision'])) else: form = EditAnswerForm(answer, latest_revision, request.POST) else: @@ -666,14 +664,14 @@ 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, - author = request.user, - revised_at = edited_at, - text = form.cleaned_data['text'] - ) + answer=answer, + author=request.user, + revised_at=edited_at, + text=form.cleaned_data['text'] + ) if form.cleaned_data['summary']: revision.summary = form.cleaned_data['summary'] @@ -690,14 +688,14 @@ def edit_answer(request, id): revision_form = RevisionForm(answer, latest_revision) form = EditAnswerForm(answer, latest_revision) return render_to_response('answer_edit.html', { - 'answer': answer, - 'revision_form': revision_form, - 'form' : form, - }, context_instance=RequestContext(request)) + 'answer': answer, + 'revision_form': revision_form, + 'form': form, + }, context_instance=RequestContext(request)) QUESTION_REVISION_TEMPLATE = ('<h1>%(title)s</h1>\n' - '<div class="text">%(html)s</div>\n' - '<div class="tags">%(tags)s</div>') + '<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()) @@ -707,7 +705,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) @@ -716,13 +714,13 @@ def question_revisions(request, id): 'title': revisions[0].title, 'html': sanitize_html(markdowner.convert(revisions[0].text)), 'tags': ' '.join(['<a class="post-tag">%s</a>' % tag - for tag in revisions[0].tagnames.split(' ')]), + for tag in revisions[0].tagnames.split(' ')]), } revisions[i].summary = _('initial version') return render_to_response('revisions_question.html', { - 'post': post, - 'revisions': revisions, - }, context_instance=RequestContext(request)) + 'post': post, + 'revisions': revisions, + }, context_instance=RequestContext(request)) ANSWER_REVISION_TEMPLATE = ('<div class="text">%(html)s</div>') def answer_revisions(request, id): @@ -739,9 +737,9 @@ def answer_revisions(request, id): revisions[i].diff = revisions[i].text revisions[i].summary = _('initial version') return render_to_response('revisions_answer.html', { - 'post': post, - 'revisions': revisions, - }, context_instance=RequestContext(request)) + 'post': post, + 'revisions': revisions, + }, context_instance=RequestContext(request)) def answer(request, id): question = get_object_or_404(Question, id=id) @@ -754,25 +752,25 @@ def answer(request, id): if request.user.is_authenticated(): create_new_answer( - question=question, - author=request.user, - added_at=update_time, - wiki=wiki, - text=text, - email_notify=form.cleaned_data['email_notify'] - ) + question=question, + author=request.user, + added_at=update_time, + wiki=wiki, + text=text, + email_notify=form.cleaned_data['email_notify'] + ) else: request.session.flush() html = sanitize_html(markdowner.convert(text)) summary = strip_tags(html)[:120] anon = AnonymousAnswer( - question = question, - wiki = wiki, - text = text, - summary = summary, - session_key = request.session.session_key, - ip_addr = request.META['REMOTE_ADDR'], - ) + question=question, + wiki=wiki, + text=text, + summary=summary, + session_key=request.session.session_key, + ip_addr=request.META['REMOTE_ADDR'], + ) anon.save() return HttpResponseRedirect(reverse('user_signin_new_answer')) @@ -789,35 +787,34 @@ def tags(request): if request.method == "GET": stag = request.GET.get("q", "").strip() - if stag != '': + if len(stag) > 0: 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): tags = objects_list.page(objects_list.num_pages) return render_to_response('tags.html', { - "tags" : tags, - "stag" : stag, - "tab_id" : sortby, - "keywords" : stag, - "context" : { - 'is_paginated' : is_paginated, - 'pages': objects_list.num_pages, - 'page': page, - 'has_previous': tags.has_previous(), - 'has_next': tags.has_next(), - 'previous': tags.previous_page_number(), - 'next': tags.next_page_number(), - 'base_url' : reverse('tags') + '?sort=%s&' % sortby - } - + "tags" : tags, + "stag" : stag, + "tab_id" : sortby, + "keywords" : stag, + "context" : { + 'is_paginated' : is_paginated, + 'pages': objects_list.num_pages, + 'page': page, + 'has_previous': tags.has_previous(), + 'has_next': tags.has_next(), + 'previous': tags.previous_page_number(), + 'next': tags.next_page_number(), + 'base_url' : reverse('tags') + '?sort=%s&' % sortby + } }, context_instance=RequestContext(request)) def tag(request, tag): @@ -864,9 +861,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): @@ -1055,7 +1052,7 @@ def vote(request, id): 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: @@ -1083,22 +1080,22 @@ def users(request): users = objects_list.page(objects_list.num_pages) return render_to_response('users.html', { - "users" : users, - "suser" : suser, - "keywords" : suser, - "tab_id" : sortby, - "context" : { - 'is_paginated' : is_paginated, - 'pages': objects_list.num_pages, - 'page': page, - 'has_previous': users.has_previous(), - 'has_next': users.has_next(), - 'previous': users.previous_page_number(), - 'next': users.next_page_number(), - 'base_url' : base_url - } + "users": users, + "suser": suser, + "keywords": suser, + "tab_id": sortby, + "context": { + 'is_paginated': is_paginated, + 'pages': objects_list.num_pages, + 'page': page, + 'has_previous': users.has_previous(), + 'has_next': users.has_next(), + 'previous': users.previous_page_number(), + 'next': users.next_page_number(), + 'base_url': base_url + } - }, context_instance=RequestContext(request)) + }, context_instance=RequestContext(request)) def user(request, id): sort = request.GET.get('sort', 'stats') @@ -1158,50 +1155,50 @@ def edit_user(request, id): else: form = EditUserForm(user) return render_to_response('user_edit.html', { - 'form' : form, - 'gravatar_faq_url' : reverse('faq') + '#gravatar', - }, context_instance=RequestContext(request)) + 'form' : form, + 'gravatar_faq_url' : reverse('faq') + '#gravatar', + }, context_instance=RequestContext(request)) 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={ @@ -1227,6 +1224,7 @@ def user_stats(request, user_id, user_view): 'answer_count', 'vote_up_count', 'vote_down_count')[:100] + up_votes = Vote.objects.get_up_vote_count_from_user(user) down_votes = Vote.objects.get_down_vote_count_from_user(user) votes_today = Vote.objects.get_votes_count_today_from_user(user) @@ -1236,30 +1234,37 @@ def user_stats(request, user_id, user_view): | set(map(lambda v: v['id'], list(answered_questions))) user_tags = Tag.objects.filter(questions__id__in = question_id_set) - try: from django.db.models import Count awards = Award.objects.extra( - select={'id': 'badge.id', 'name':'badge.name', 'description': 'badge.description', 'type': 'badge.type'}, - tables=['award', 'badge'], - order_by=['-awarded_at'], - where=['user_id=%s AND badge_id=badge.id'], - params=[user.id] - ).values('id', 'name', 'description', 'type') + select={'id': 'badge.id', + 'name':'badge.name', + 'description': 'badge.description', + 'type': 'badge.type'}, + tables=['award', 'badge'], + order_by=['-awarded_at'], + where=['user_id=%s AND badge_id=badge.id'], + params=[user.id] + ).values('id', 'name', 'description', 'type') total_awards = awards.count() awards = awards.annotate(count = Count('badge__id')) user_tags = user_tags.annotate(user_tag_usage_count=Count('name')) except ImportError: awards = Award.objects.extra( - select={'id': 'badge.id', 'count': 'count(badge_id)', 'name':'badge.name', 'description': 'badge.description', 'type': 'badge.type'}, - tables=['award', 'badge'], - order_by=['-awarded_at'], - where=['user_id=%s AND badge_id=badge.id'], - params=[user.id] - ).values('id', 'count', 'name', 'description', 'type') + select={'id': 'badge.id', + 'count': 'count(badge_id)', + 'name':'badge.name', + 'description': 'badge.description', + 'type': 'badge.type'}, + tables=['award', 'badge'], + order_by=['-awarded_at'], + where=['user_id=%s AND badge_id=badge.id'], + params=[user.id] + ).values('id', 'count', 'name', 'description', 'type') total_awards = awards.count() awards.query.group_by = ['badge_id'] + user_tags = user_tags.extra( select={'user_tag_usage_count': 'COUNT(1)',}, order_by=['-user_tag_usage_count'], @@ -1271,24 +1276,24 @@ def user_stats(request, user_id, user_view): else: moderate_user_form = None - return render_to_response(user_view.template_file,{ - 'moderate_user_form': moderate_user_form, - "tab_name" : user_view.id, - "tab_description" : user_view.tab_description, - "page_title" : user_view.page_title, - "view_user" : user, - "questions" : questions, - "answered_questions" : answered_questions, - "up_votes" : up_votes, - "down_votes" : down_votes, - "total_votes": up_votes + down_votes, - "votes_today_left": votes_total-votes_today, - "votes_total_per_day": votes_total, - "user_tags" : user_tags[:50], - "tags" : tags, - "awards": awards, - "total_awards" : total_awards, - }, context_instance=RequestContext(request)) + return render_to_response(user_view.template_file, + { + 'moderate_user_form': moderate_user_form, + "tab_name" : user_view.id, + "tab_description" : user_view.tab_description, + "page_title" : user_view.page_title, + "view_user" : user, + "questions" : questions, + "answered_questions" : answered_questions, + "up_votes" : up_votes, + "down_votes" : down_votes, + "total_votes": up_votes + down_votes, + "votes_today_left": votes_total-votes_today, + "votes_total_per_day": votes_total, + "user_tags" : user_tags[:50], + "awards": awards, + "total_awards" : total_awards, + }, context_instance=RequestContext(request)) def user_recent(request, user_id, user_view): user = get_object_or_404(User, id=user_id) @@ -1308,7 +1313,6 @@ def user_recent(request, user_id, user_view): self.title_link = reverse('question', kwargs={'id':question_id}) + u'%s' % slug_title if int(answer_id) > 0: self.title_link += '#%s' % answer_id - class AwardEvent: def __init__(self, time, type, id): self.time = time @@ -1338,7 +1342,7 @@ def user_recent(request, user_id, user_view): ) if len(questions) > 0: questions = [(Event(q['active_at'], q['activity_type'], q['title'], '', '0', \ - q['question_id'])) for q in questions] + q['question_id'])) for q in questions] activities.extend(questions) # answers @@ -1365,7 +1369,7 @@ def user_recent(request, user_id, user_view): ) if len(answers) > 0: answers = [(Event(q['active_at'], q['activity_type'], q['title'], '', q['answer_id'], \ - q['question_id'])) for q in answers] + q['question_id'])) for q in answers] activities.extend(answers) # question comments @@ -1393,7 +1397,7 @@ def user_recent(request, user_id, user_view): if len(comments) > 0: comments = [(Event(q['added_at'], q['activity_type'], q['title'], '', '0', \ - q['question_id'])) for q in comments] + q['question_id'])) for q in comments] activities.extend(comments) # answer comments @@ -1424,7 +1428,7 @@ def user_recent(request, user_id, user_view): if len(comments) > 0: comments = [(Event(q['added_at'], q['activity_type'], q['title'], '', q['answer_id'], \ - q['question_id'])) for q in comments] + q['question_id'])) for q in comments] activities.extend(comments) # question revisions @@ -1453,7 +1457,7 @@ def user_recent(request, user_id, user_view): if len(revisions) > 0: revisions = [(Event(q['added_at'], q['activity_type'], q['title'], q['summary'], '0', \ - q['question_id'])) for q in revisions] + q['question_id'])) for q in revisions] activities.extend(revisions) # answer revisions @@ -1486,7 +1490,7 @@ def user_recent(request, user_id, user_view): if len(revisions) > 0: revisions = [(Event(q['added_at'], q['activity_type'], q['title'], q['summary'], \ - q['answer_id'], q['question_id'])) for q in revisions] + q['answer_id'], q['question_id'])) for q in revisions] activities.extend(revisions) # accepted answers @@ -1510,40 +1514,41 @@ def user_recent(request, user_id, user_view): 'added_at', 'activity_type', ) + 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, - "tab_description" : user_view.tab_description, - "page_title" : user_view.page_title, - "view_user" : user, - "activities" : activities[:user_view.data_size] - }, context_instance=RequestContext(request)) + 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, + "activities": activities[:user_view.data_size] + }, context_instance=RequestContext(request)) def user_responses(request, user_id, user_view): """ @@ -1565,139 +1570,139 @@ def user_responses(request, user_id, user_view): user = get_object_or_404(User, id=user_id) responses = [] answers = Answer.objects.extra( - select={ - 'title' : 'question.title', - 'question_id' : 'question.id', - 'answer_id' : 'answer.id', - 'added_at' : 'answer.added_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 '+ - 'question.author_id = %s AND answer.author_id <> %s AND answer.author_id=auth_user.id'], - params=[user_id, 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.added_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 ' + + 'question.author_id = %s AND answer.author_id <> %s AND answer.author_id=auth_user.id'], + params=[user_id, 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['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) # question comments comments = Comment.objects.extra( - select={ - 'title' : 'question.title', - 'question_id' : 'comment.object_id', - 'added_at' : 'comment.added_at', - 'comment' : 'comment.comment', - 'username' : 'auth_user.username', - 'user_id' : 'auth_user.id' - }, - tables=['question', 'auth_user', 'comment'], - where=['question.deleted = 0 AND question.author_id = %s AND comment.object_id=question.id AND '+ - 'comment.content_type_id=%s AND comment.user_id <> %s AND comment.user_id = auth_user.id'], - params=[user_id, question_type_id, user_id], - order_by=['-comment.added_at'] - ).values( - 'title', - 'question_id', - 'added_at', - 'comment', - 'username', - 'user_id' - ) + select={ + 'title': 'question.title', + 'question_id': 'comment.object_id', + 'added_at': 'comment.added_at', + 'comment': 'comment.comment', + 'username': 'auth_user.username', + 'user_id': 'auth_user.id' + }, + tables=['question', 'auth_user', 'comment'], + where=['question.deleted = 0 AND question.author_id = %s AND comment.object_id=question.id AND ' + + 'comment.content_type_id=%s AND comment.user_id <> %s AND comment.user_id = auth_user.id'], + params=[user_id, question_type_id, user_id], + order_by=['-comment.added_at'] + ).values( + 'title', + 'question_id', + 'added_at', + 'comment', + 'username', + 'user_id' + ) 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) @@ -1705,61 +1710,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) @@ -1784,14 +1789,14 @@ def user_reputation(request, user_id, user_view): params=[user.id] ).values('positive', 'negative', 'question_id', 'title', 'reputed_at', 'reputation') reputation.query.group_by = ['question_id'] - + rep_list = [] for rep in Repute.objects.filter(user=user).order_by('reputed_at'): dic = '[%s,%s]' % (calendar.timegm(rep.reputed_at.timetuple()) * 1000, rep.reputation) rep_list.append(dic) reps = ','.join(rep_list) reps = '[%s]' % reps - + return render_to_response(user_view.template_file, { "tab_name": user_view.id, "tab_description": user_view.tab_description, @@ -1804,52 +1809,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): @@ -1974,21 +1979,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": @@ -2021,7 +2026,7 @@ def upload(request): raise FileTypeNotAllow # genetate new file name - new_file_name = str(time.time()).replace('.', str(random.randint(0,100000))) + file_name_suffix + new_file_name = str(time.time()).replace('.', str(random.randint(0, 100000))) + file_name_suffix # use default storage to store file default_storage.save(new_file_name, f) # check file size @@ -2057,7 +2062,7 @@ def book(request, short_name, unanswered=False): """ books = Book.objects.extra(where=['short_name = %s'], params=[short_name]) match_count = len(books) - if match_count == 0 : + if match_count == 0: raise Http404 else: # the book info @@ -2079,7 +2084,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: @@ -2100,22 +2105,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): @@ -2125,16 +2130,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 @@ -2144,15 +2149,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) @@ -2166,11 +2171,13 @@ def ask_book(request, short_name): form = AskForm() tags = _get_tags_cache_json() - return render_to_response('ask.html', { - 'form' : form, - 'tags' : tags, - 'email_validation_faq_url': reverse('faq') + '#validate', - }, context_instance=RequestContext(request)) + return render_to_response('ask.html', + { + 'form' : form, + 'tags' : tags, + 'email_validation_faq_url': reverse('faq') + '#validate', + }, context_instance=RequestContext(request) + ) def search(request): """ @@ -2221,7 +2228,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: @@ -2250,25 +2257,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 |