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