1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from forum.models import BookAuthorInfo, BookAuthorRss, Book
from forum.models import Question, QuestionRevision
from django.http import HttpResponseRedirect
def books(request):
"""this view seems to redirect to a default book
maybe it should instead show some popular titles?
"""
return HttpResponseRedirect(reverse('books') + 'mysql-zhaoyang')
def book(request, short_name, unanswered=False):
"""
1. questions list
2. book info
3. author info and blog rss items
"""
"""
List of Questions, Tagged questions, and Unanswered questions.
"""
books = Book.objects.extra(where=['short_name = %s'], params=[short_name])
match_count = len(books)
if match_count == 0:
raise Http404
else:
# the book info
book = books[0]
# get author info
author_info = BookAuthorInfo.objects.get(book=book)
# get author rss info
author_rss = BookAuthorRss.objects.filter(book=book)
# get pagesize from session, if failed then get default value
user_page_size = request.session.get("pagesize", QUESTIONS_PAGE_SIZE)
# set pagesize equal to logon user specified value in database
if request.user.is_authenticated() and request.user.questions_per_page > 0:
user_page_size = request.user.questions_per_page
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
view_id = request.GET.get('sort', None)
view_dic = {"latest":"-added_at", "active":"-last_activity_at", "hottest":"-answer_count", "mostvoted":"-score" }
try:
orderby = view_dic[view_id]
except KeyError:
view_id = "latest"
orderby = "-added_at"
# check if request is from tagged questions
if unanswered:
# check if request is from unanswered questions
# Article.objects.filter(publications__id__exact=1)
objects = Question.objects.filter(book__id__exact=book.id, deleted=False, answer_count=0).order_by(orderby)
else:
objects = Question.objects.filter(book__id__exact=book.id, deleted=False).order_by(orderby)
# RISK - inner join queries
objects = objects.select_related();
objects_list = Paginator(objects, user_page_size)
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))
@login_required
def ask_book(request, short_name):
if request.method == "POST":
form = AskForm(request.POST)
if form.is_valid():
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]
)
if question.wiki:
question.last_edited_by = question.author
question.last_edited_at = added_at
question.wikified_at = added_at
question.save()
# 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']
)
books = Book.objects.extra(where=['short_name = %s'], params=[short_name])
match_count = len(books)
if match_count == 1:
# the book info
book = books[0]
book.questions.add(question)
return HttpResponseRedirect(question.get_absolute_url())
else:
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))
|