Adding a simple answer workflow. · pythonDrive/bootcamp@3e66336 · GitHub
Skip to content

Commit 3e66336

Browse files
Adding a simple answer workflow.
1 parent 9be47ca commit 3e66336

6 files changed

Lines changed: 38 additions & 42 deletions

File tree

bootcamp/qa/urls.py

Lines changed: 1 addition & 1 deletion

bootcamp/qa/views.py

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
from django.conf import settings
2+
from django.contrib.auth.decorators import login_required
23
from django.contrib.auth.mixins import LoginRequiredMixin
34
from django.contrib import messages
45
from django.http import JsonResponse
56
from django.urls import reverse
67
from django.utils.translation import ugettext as _
78
from django.views.generic import CreateView, ListView, DetailView
89

10+
from bootcamp.helpers import ajax_required
911
from bootcamp.qa.models import Question, Answer
10-
from bootcamp.qa.forms import QuestionForm, AnswerForm
12+
from bootcamp.qa.forms import QuestionForm
1113

1214

1315
class QuestionListView(LoginRequiredMixin, ListView):
@@ -47,35 +49,15 @@ class CreateAnswerView(LoginRequiredMixin, CreateView):
4749
View to create new answers for a given question
4850
"""
4951
model = Answer
50-
fields = ["question", "content"]
52+
fields = ["content", ]
5153
message = _("Thank you! Your answer has been posted.")
5254

53-
def form_invalid(self, form):
54-
response = super().form_invalid(form)
55-
if self.request.is_ajax():
56-
return JsonResponse(form.errors, status=400)
57-
58-
else:
59-
return response
60-
6155
def form_valid(self, form):
62-
# We make sure to call the parent's form_valid() method because
63-
# it might do some processing (in the case of CreateView, it will
64-
# call form.save() for example).
65-
response = super().form_valid(form)
66-
if self.request.is_ajax():
67-
form.instance.user = self.request.user
68-
data = {
69-
"question": self.object.question,
70-
"content": self.object.content
71-
}
72-
return JsonResponse(data)
73-
74-
else:
75-
return response
76-
56+
form.instance.user = self.request.user
57+
form.instance.question_id = self.kwargs['question_id']
58+
return super().form_valid(form)
7759

7860
def get_success_url(self):
7961
messages.success(self.request, self.message)
8062
return reverse(
81-
"qa:detail", kwargs={"id": self.kwargs["question_id"]})
63+
"qa:question_detail", kwargs={"pk": self.kwargs["question_id"]})
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{% extends 'base.html' %}
2+
{% load static i18n %}
3+
{% load crispy_forms_tags %}
4+
5+
{% block title %}{% trans 'Answer a question' %}{% endblock %}
6+
7+
{% block content %}
8+
9+
<nav aria-label="breadcrumb">
10+
<ol class="breadcrumb">
11+
<li class="breadcrumb-item"><a href="{% url 'news:list' %}">{% trans 'Home' %}</a></li>
12+
<li class="breadcrumb-item"><a href="{% url 'qa:index' %}">{% trans 'Q&A' %}</a></li>
13+
<li class="breadcrumb-item active" aria-current="page">{% trans 'Answer a question' %}</li>
14+
</ol>
15+
</nav>
16+
<form action="{% url 'qa:propose_answer' view.kwargs.question_id %}" id="answerForm" method="post" role="form">
17+
{% csrf_token %}
18+
{{ form|crispy }}
19+
<div class="form-group">
20+
<button id="answer" type="submit" class="btn btn-primary">{% trans 'Answer' %}</button>
21+
<a href="{% url 'qa:index' %}" class="btn btn-default">{% trans 'Cancel' %}</a>
22+
</div>
23+
</form>
24+
25+
{% endblock content %}

bootcamp/templates/qa/question_detail.html

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ <h1>{{ question.title }}</h1>
2828
<h3>{{ question.count_answers }}</h3>
2929
<small class="text-secondary">{% trans 'Answers' %}</small>
3030
<h3>{{ question.count_likers }}</h3>
31-
<a href="#" style="text-decoration: none;"><h3><i class="text-danger fa fa-heart{% if request.user not in question.get_likers %}-o{% endif %}" aria-hidden="true"></i></h3></a>
31+
<a href="#"><h3><i class="text-danger fa fa-heart{% if request.user not in question.get_likers %}-o{% endif %}" aria-hidden="true"></i></h3></a>
3232
<small class="text-secondary">{% trans 'Likes' %}</small>
3333
<i class="fa fa-chevron-up vote up-vote answer-vote{% if request.user in question.get_upvoters %} voted{% endif %}" aria-hidden="true" title="{% trans 'Click to up vote; click again to toggle' %}"></i>
3434
<h3>{{ question.count_votes }}</h3>
@@ -58,16 +58,8 @@ <h3>{{ question.count_votes }}</h3>
5858
</p>
5959
{% endif %}
6060
</div>
61-
<form action="#" method="post" role="form">
62-
{% csrf_token %}
63-
<div class="form-group">
64-
<label for="answerFormTA">{% trans 'Submit an answer' %}</label>
65-
<input type="hidden" name="question" value="{{ question }}">
66-
<textarea class="form-control" id="answerFormTA" name="content" rows="4"></textarea>
67-
</div>
68-
<button type="submit" class="btn btn-primary pull-right">{% trans "Submit an answer"%}</button>
69-
</form>
7061
</div>
62+
<a href="{% url 'qa:propose_answer' question.id %}" class="btn btn-primary pull-right" role="button">{% trans "Submit an answer" %}</a>
7163
</div>
7264
<div class="page-header">
7365
<h1>{% trans 'Answers' %}</h1>
@@ -84,6 +76,4 @@ <h4>{% trans 'There are no answers yet.' %}</h4>
8476
{% endfor %}
8577
</ul>
8678
</div>
87-
88-
8979
{% endblock content %}

bootcamp/templates/qa/question_form.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
{% load static i18n %}
33
{% load crispy_forms_tags %}
44

5-
{% block head %}
6-
{% endblock head %}
5+
{% block title %}{% trans 'Ask a question' %}{% endblock %}
76

87
{% block content %}
98

bootcamp/templates/qa/question_sample.html

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)