1 from django.shortcuts import render_to_response, get_object_or_404
2 from django.http import HttpResponseRedirect, HttpResponse, HttpResponseForbidden, Http404
3 from django.template import RequestContext
4 from django.contrib.auth.decorators import login_required
5 from django.core.urlresolvers import reverse
6 from django.utils.html import *
10 from forum.forms import AskForm
11 from forum.views.readers import _get_tags_cache_json
12 from forum.models import *
13 from forum.utils.html import sanitize_html
16 return HttpResponseRedirect(reverse('books') + '/mysql-zhaoyang')
18 def book(request, short_name, unanswered=False):
22 3. author info and blog rss items
25 List of Questions, Tagged questions, and Unanswered questions.
27 books = Book.objects.extra(where=['short_name = %s'], params=[short_name])
28 match_count = len(books)
35 author_info = BookAuthorInfo.objects.get(book=book)
37 author_rss = BookAuthorRss.objects.filter(book=book)
39 # get pagesize from session, if failed then get default value
40 user_page_size = request.session.get("pagesize", QUESTIONS_PAGE_SIZE)
41 # set pagesize equal to logon user specified value in database
42 if request.user.is_authenticated() and request.user.questions_per_page > 0:
43 user_page_size = request.user.questions_per_page
46 page = int(request.GET.get('page', '1'))
50 view_id = request.GET.get('sort', None)
51 view_dic = {"latest":"-added_at", "active":"-last_activity_at", "hottest":"-answer_count", "mostvoted":"-score" }
53 orderby = view_dic[view_id]
58 # check if request is from tagged questions
60 # check if request is from unanswered questions
61 # Article.objects.filter(publications__id__exact=1)
62 objects = Question.objects.filter(book__id__exact=book.id, deleted=False, answer_count=0).order_by(orderby)
64 objects = Question.objects.filter(book__id__exact=book.id, deleted=False).order_by(orderby)
66 # RISK - inner join queries
67 objects = objects.select_related();
68 objects_list = Paginator(objects, user_page_size)
69 questions = objects_list.page(page)
71 return render_to_response('book.html', {
73 "author_info" : author_info,
74 "author_rss" : author_rss,
75 "questions" : questions,
77 'is_paginated' : True,
78 'pages': objects_list.num_pages,
80 'has_previous': questions.has_previous(),
81 'has_next': questions.has_next(),
82 'previous': questions.previous_page_number(),
83 'next': questions.next_page_number(),
84 'base_url' : request.path + '?sort=%s&' % view_id,
85 'pagesize' : user_page_size
87 }, context_instance=RequestContext(request))
90 def ask_book(request, short_name):
91 if request.method == "POST":
92 form = AskForm(request.POST)
94 added_at = datetime.datetime.now()
95 html = sanitize_html(markdowner.convert(form.cleaned_data['text']))
97 title = strip_tags(form.cleaned_data['title']),
98 author = request.user,
100 last_activity_at = added_at,
101 last_activity_by = request.user,
102 wiki = form.cleaned_data['wiki'],
103 tagnames = form.cleaned_data['tags'].strip(),
105 summary = strip_tags(html)[:120]
108 question.last_edited_by = question.author
109 question.last_edited_at = added_at
110 question.wikified_at = added_at
114 # create the first revision
115 QuestionRevision.objects.create(
118 title = question.title,
119 author = request.user,
120 revised_at = added_at,
121 tagnames = question.tagnames,
122 summary = CONST['default_version'],
123 text = form.cleaned_data['text']
126 books = Book.objects.extra(where=['short_name = %s'], params=[short_name])
127 match_count = len(books)
131 book.questions.add(question)
133 return HttpResponseRedirect(question.get_absolute_url())
137 tags = _get_tags_cache_json()
138 return render_to_response('ask.html', {
141 'email_validation_faq_url': reverse('faq') + '#validate',
142 }, context_instance=RequestContext(request))