This repository has been archived by the owner on Jan 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
/
views.py
216 lines (168 loc) · 6.57 KB
/
views.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import settings
from django.http import Http404, HttpResponseRedirect
from django.shortcuts import render, redirect, get_object_or_404
from django.core.urlresolvers import reverse, NoReverseMatch
from django.db.models import Q
from knowledge.models import Question, Response, Category
from knowledge.forms import QuestionForm, ResponseForm
from knowledge.utils import paginate
ALLOWED_MODS = {
'question': [
'private', 'public',
'delete', 'lock',
'clear_accepted'
],
'response': [
'internal', 'inherit',
'private', 'public',
'delete', 'accept'
]
}
def get_my_questions(request):
if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)
if request.user.is_anonymous():
return None
else:
return Question.objects.can_view(request.user)\
.filter(user=request.user)
def knowledge_index(request,
template='django_knowledge/index.html'):
if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)
questions = Question.objects.can_view(request.user)\
.prefetch_related('responses__question')[0:20]
# this is for get_responses()
[setattr(q, '_requesting_user', request.user) for q in questions]
return render(request, template, {
'request': request,
'questions': questions,
'my_questions': get_my_questions(request),
'categories': Category.objects.all()
})
def knowledge_list(request,
category_slug=None,
template='django_knowledge/list.html',
Form=QuestionForm):
if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)
search = request.GET.get('title', None)
questions = Question.objects.can_view(request.user)\
.prefetch_related('responses__question')
if search:
questions = questions.filter(
Q(title__icontains=search) | Q(body__icontains=search)
)
category = None
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
questions = questions.filter(categories=category)
paginator, questions = paginate(questions,
50,
request.GET.get('page', '1'))
# this is for get_responses()
[setattr(q, '_requesting_user', request.user) for q in questions]
return render(request, template, {
'request': request,
'search': search,
'questions': questions,
'my_questions': get_my_questions(request),
'category': category,
'categories': Category.objects.all(),
'form': Form(request.user, initial={'title': search}) # prefill title
})
def knowledge_thread(request,
question_id,
slug=None,
template='django_knowledge/thread.html',
Form=ResponseForm):
if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)
try:
question = Question.objects.can_view(request.user)\
.get(id=question_id)
except Question.DoesNotExist:
if Question.objects.filter(id=question_id).exists() and \
hasattr(settings, 'LOGIN_REDIRECT_URL'):
return redirect(settings.LOGIN_REDIRECT_URL)
else:
raise Http404
responses = question.get_responses(request.user)
if request.path != question.get_absolute_url():
return redirect(question.get_absolute_url(), permanent=True)
if request.method == 'POST':
form = Form(request.user, question, request.POST)
if form and form.is_valid():
form.save()
return redirect(question.get_absolute_url())
else:
form = Form(request.user, question)
return render(request, template, {
'request': request,
'question': question,
'my_questions': get_my_questions(request),
'responses': responses,
'allowed_mods': ALLOWED_MODS,
'form': form,
'categories': Category.objects.all()
})
def knowledge_moderate(
request,
lookup_id,
model,
mod,
allowed_mods=ALLOWED_MODS):
"""
An easy to extend method to moderate questions
and responses in a vaguely RESTful way.
Usage:
/knowledge/moderate/question/1/inherit/ -> 404
/knowledge/moderate/question/1/public/ -> 200
/knowledge/moderate/response/3/notreal/ -> 404
/knowledge/moderate/response/3/inherit/ -> 200
"""
if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)
if request.method != 'POST':
raise Http404
if model == 'question':
Model, perm = Question, 'change_question'
elif model == 'response':
Model, perm = Response, 'change_response'
else:
raise Http404
if not request.user.has_perm(perm):
raise Http404
if mod not in allowed_mods[model]:
raise Http404
instance = get_object_or_404(
Model.objects.can_view(request.user),
id=lookup_id)
func = getattr(instance, mod)
if callable(func):
func()
try:
return redirect((
instance if instance.is_question else instance.question
).get_absolute_url())
except NoReverseMatch:
# if we delete an instance...
return redirect(reverse('knowledge_index'))
def knowledge_ask(request,
template='django_knowledge/ask.html',
Form=QuestionForm):
if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)
if request.method == 'POST':
form = Form(request.user, request.POST)
if form and form.is_valid():
question = form.save()
return redirect(question.get_absolute_url())
else:
form = Form(request.user)
return render(request, template, {
'request': request,
'my_questions': get_my_questions(request),
'form': form,
'categories': Category.objects.all()
})