Skip to content

Commit 5d5dea3

Browse files
authored
Preserve answer input on unverified captcha (#64)
1 parent d09ef28 commit 5d5dea3

1 file changed

Lines changed: 45 additions & 20 deletions

File tree

website/views.py

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,22 @@ def question_answer(request):
193193
recaptcha_response = request.POST.get('g-recaptcha-response', '')
194194
if not recaptcha_response:
195195
messages.error(request, "Please complete the reCAPTCHA verification.")
196-
form = NewQuestionForm(request.POST)
197-
context['form'] = form
198-
context['recaptcha_site_key'] = settings.RECAPTCHA_SITE_KEY
199-
context['require_recaptcha'] = True
196+
question = get_object_or_404(Question, id=qid)
197+
answers = question.answer_set.all()
198+
if question.status in (0,2):
199+
label = "Show"
200+
else:
201+
label = "Hide"
202+
context = {
203+
'question': question,
204+
'answers': answers,
205+
'form': form,
206+
'label': label,
207+
'require_recaptcha': True,
208+
'recaptcha_site_key': settings.RECAPTCHA_SITE_KEY
209+
}
200210
context.update(csrf(request))
201-
# return render(request, 'website/templates/new-question.html', context)
202-
return HttpResponseRedirect('/question/' + str(qid))
211+
return render(request, 'website/templates/get-question.html', context)
203212
# verify with google
204213
recaptcha_verification_url = "https://www.google.com/recaptcha/api/siteverify"
205214
recaptcha_data = {
@@ -212,25 +221,41 @@ def question_answer(request):
212221
recaptcha_json = recaptcha_result.json()
213222
except requests.RequestException as e:
214223
messages.error(request, "Error verifying reCAPTCHA. Please try again.")
215-
form = NewQuestionForm(request.POST)
216-
context['form'] = form
217-
context['recaptcha_site_key'] = settings.RECAPTCHA_SITE_KEY
218-
context['require_recaptcha'] = True
224+
question = get_object_or_404(Question, id=qid)
225+
answers = question.answer_set.all()
226+
if question.status in (0,2):
227+
label = "Show"
228+
else:
229+
label = "Hide"
230+
context = {
231+
'question': question,
232+
'answers': answers,
233+
'form': form,
234+
'label': label,
235+
'require_recaptcha': True,
236+
'recaptcha_site_key': settings.RECAPTCHA_SITE_KEY
237+
}
219238
context.update(csrf(request))
220-
messages.error(request, "Error")
221-
# return render(request, 'website/templates/new-question.html', context)
222-
return HttpResponseRedirect('/question/' + str(qid))
239+
return render(request, 'website/templates/get-question.html', context)
223240
# check if verification was successful
224241
if not recaptcha_json.get('success', False):
225242
messages.error(request, "reCAPTCHA verification failed. Please try again.")
226-
form = NewQuestionForm(request.POST)
227-
context['form'] = form
228-
context['recaptcha_site_key'] = settings.RECAPTCHA_SITE_KEY
229-
context['require_recaptcha'] = True
243+
question = get_object_or_404(Question, id=qid)
244+
answers = question.answer_set.all()
245+
if question.status in (0,2):
246+
label = "Show"
247+
else:
248+
label = "Hide"
249+
context = {
250+
'question': question,
251+
'answers': answers,
252+
'form': form,
253+
'label': label,
254+
'require_recaptcha': True,
255+
'recaptcha_site_key': settings.RECAPTCHA_SITE_KEY
256+
}
230257
context.update(csrf(request))
231-
messages.error(request, "Error")
232-
# return render(request, 'website/templates/new-question.html', context)
233-
return HttpResponseRedirect('/question/' + str(qid))
258+
return render(request, 'website/templates/get-question.html', context)
234259

235260
qid = cleaned_data['question']
236261
body = cleaned_data['body']

0 commit comments

Comments
 (0)