Skip to content

Commit

Permalink
v0.2
Browse files Browse the repository at this point in the history
Vote Answers
  • Loading branch information
Arjun committed Jan 19, 2015
1 parent 00a7825 commit c44c900
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 9 deletions.
Binary file modified db.sqlite3
Binary file not shown.
20 changes: 20 additions & 0 deletions qa/migrations/0008_answer_votes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('qa', '0007_answer_pub_date'),
]

operations = [
migrations.AddField(
model_name='answer',
name='votes',
field=models.IntegerField(default=0),
preserve_default=True,
),
]
Binary file added qa/migrations/0008_answer_votes.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions qa/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __unicode__(self):
class Answer(models.Model):
question = models.ForeignKey(Question)
answer_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
pub_date = models.DateTimeField('date published')
user_data = models.ForeignKey(UserProfile)
def __str__(self):
Expand Down
Binary file modified qa/models.pyc
Binary file not shown.
42 changes: 37 additions & 5 deletions qa/templates/qa/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,59 @@
{% bootstrap_css %}
{% bootstrap_javascript %}

<link href='http://fonts.googleapis.com/css?family=Lobster' rel='stylesheet' type='text/css'>

<style>
cool {
font-family: 'Lobster', cursive;
}
</style>

<div class="container">
<div class="page-header">
<h1><a href="/">Simple QA </a><small>Open Questions</small></h1>
</div>

<div class="jumbotron">
<h2>Q: {{ question.question_text }}</h2>
<p class="pull-right">{{ question.pub_date }}</p>
<h2><cool>Q: {{ question.question_text }}</cool></h2>
<p><a class="btn btn-primary btn-sm pull-right" href="/answer/{{ question.id }}" role="button">Answer this Question!</a></p>
</div>

{% if question.answer_set.count %}
{% if answers %}
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title">There are {{ question.answer_set.count }} Answers</h3>
<h3 class="panel-title">There are {{ answers.count }} Answers</h3>
</div>
<div class="panel-body">
{% for answer in question.answer_set.all %}
<div class="alert alert-info" role="alert">{{ answer.answer_text }}<small class="pull-right">- <b>{{ answer.user_data.user.username }}</b> ({{ answer.pub_date }})</small></div>
{% for answer in answers %}
<div class="row">
{% if user.is_authenticated %}
<div class ="col-md-1"><h3><a href="/vote/{{ answer.id }}/{{ question.id }}/0/"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span></a> {{ answer.votes }} <a href="/vote/{{ answer.id }}/{{ question.id }}/1/"><span class="glyphicon glyphicon-minus" aria-hidden="true"></span></a></h3></div>
{% endif %}
<div class ="col-md-8"><div class="alert alert-info" role="alert">{{ answer.answer_text }}</div></div>
<div class ="col-md-3"><p class="pull-right">- <b>{{ answer.user_data.user.username }}</b> ({{ answer.pub_date }})</p></div>
</div>
{% endfor %}
</div>
</div>

<div class="pagination">
<span class="step-links">
{% if answers.has_previous %}
<a href="?page={{ answers.previous_page_number }}">previous</a>
{% endif %}

<span class="current">
Page {{ answers.number }} of {{ answers.paginator.num_pages }}.
</span>

{% if answers.has_next %}
<a href="?page={{ answers.next_page_number }}">next</a>
{% endif %}
</span>
</div>

{% else %}
<center><p>This question is still open, <a href="/answer/{{ question.id }}" role="button">Write answer!</a></p></center>
{% endif %}
Expand Down
11 changes: 10 additions & 1 deletion qa/templates/qa/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
{% bootstrap_css %}
{% bootstrap_javascript %}

<link href='http://fonts.googleapis.com/css?family=Roboto:300' rel='stylesheet' type='text/css'>

<style>
.ques {
font-family: 'Roboto', sans-serif;
font-size: 25px;
}
</style>

<div class="container">
<div class="page-header">
<a class="btn btn-danger pull-right" href="/add/">Ask Question</a><h1><a href="/">Simple QA </a><small>Open Questions</small></h1>
Expand All @@ -11,7 +20,7 @@
{% if questions %}
<ul>
{% for question in questions %}
<li><a href="/q/{{ question.id }}/">{{ question.question_text }}</a><small class="pull-right"> {{ question.pub_date}}</small></li>
<li><a class="ques" href="/q/{{ question.id }}/">{{ question.question_text }}</a><small class="pull-right"> {{ question.pub_date}}</small></li>
{% endfor %}
</ul>

Expand Down
59 changes: 56 additions & 3 deletions qa/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,22 @@ def add(request):
def detail(request, question_id):
try:
question = Question.objects.get(pk=question_id)
answer_list = question.answer_set.order_by('-votes')

paginator = Paginator(answer_list, 10)
page = request.GET.get('page')
try:
answers = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
answers = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
answers = paginator.page(paginator.num_pages)

except Question.DoesNotExist:
raise Http404("Question does not exist")
return render(request, 'qa/detail.html', {'question': question})
return render(request, 'qa/detail.html', {'answers': answers, 'question': question}, )

def answer(request, question_id):
try:
Expand All @@ -72,13 +85,53 @@ def add_answer(request):
return render(request, 'qa/answer.html', {'question': question, 'message': 'Empty'})

a = Answer()
pub_date = datetime.datetime.now()
a.answer_text = answer_text
a.question = question
a.user_data = user
a.pub_date = pub_date
a.save()
return render(request, 'qa/detail.html', {'question': question})

return render(request, 'qa/answer.html', {'question': question})
answer_list = question.answer_set.order_by('-votes')

paginator = Paginator(answer_list, 10)
page = request.GET.get('page')
try:
answers = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
answers = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
answers = paginator.page(paginator.num_pages)

return render(request, 'qa/detail.html', {'question': question, 'answers': answers})

return render(request, 'qa/detail.html', {'question': question})

def vote(request, answer_id, question_id, op_code):
answer = Answer.objects.get(pk=answer_id)
if op_code == '0':
answer.votes += 1
if op_code == '1':
answer.votes -= 1
answer.save()
question = Question.objects.get(pk=question_id)

answer_list = question.answer_set.order_by('-votes')

paginator = Paginator(answer_list, 10)
page = request.GET.get('page')
try:
answers = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
answers = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
answers = paginator.page(paginator.num_pages)

return render(request, 'qa/detail.html', {'question': question, 'answers': answers})

from qa.forms import UserForm, UserProfileForm

Expand Down
Binary file modified qa/views.pyc
Binary file not shown.
1 change: 1 addition & 0 deletions simpleqa/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
url(r'^admin/', include(admin.site.urls)),
url(r'^add/$', views.add, name='add'),
url(r'^answer/$', views.add_answer, name='add_answer'),
url(r'^vote/(?P<answer_id>\d+)/(?P<question_id>\d+)/(?P<op_code>\d+)/$', views.vote, name='vote'),

url(r'^register/$', views.register, name='register'),
url(r'^login/$', views.user_login, name='login'),
Expand Down
Binary file modified simpleqa/urls.pyc
Binary file not shown.

0 comments on commit c44c900

Please sign in to comment.