Skip to content
This repository was archived by the owner on Sep 3, 2020. It is now read-only.

Commit 6801686

Browse files
author
Arjun
committed
beta 5.2
Several Bug Fixes
1 parent 9e04479 commit 6801686

File tree

8 files changed

+117
-5
lines changed

8 files changed

+117
-5
lines changed

db.sqlite3

16 KB
Binary file not shown.

qa/admin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@
77
admin.site.register(Comment)
88
admin.site.register(Tag)
99
admin.site.register(UserProfile)
10+
admin.site.register(Voter)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('qa', '0013_auto_20150210_1002'),
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='Voter',
16+
fields=[
17+
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
18+
('answer', models.ForeignKey(to='qa.Answer')),
19+
('user', models.ForeignKey(to='qa.UserProfile')),
20+
],
21+
options={
22+
},
23+
bases=(models.Model,),
24+
),
25+
migrations.RemoveField(
26+
model_name='answer',
27+
name='votes',
28+
),
29+
migrations.AlterField(
30+
model_name='userprofile',
31+
name='picture',
32+
field=models.ImageField(upload_to=b'profile_images', blank=True),
33+
preserve_default=True,
34+
),
35+
]

qa/migrations/0015_answer_votes.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('qa', '0014_auto_20150212_0636'),
11+
]
12+
13+
operations = [
14+
migrations.AddField(
15+
model_name='answer',
16+
name='votes',
17+
field=models.IntegerField(default=0),
18+
preserve_default=True,
19+
),
20+
]

qa/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class Answer(models.Model):
4444
def __str__(self):
4545
return self.answer_text
4646

47+
class Voter(models.Model):
48+
user = models.ForeignKey(UserProfile)
49+
answer = models.ForeignKey(Answer)
50+
4751
class Comment(models.Model):
4852
answer = models.ForeignKey(Answer)
4953
comment_text = models.CharField(max_length=200)

qa/templates/qa/detail.html

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
{% load bootstrap3 %}
22

33
{% bootstrap_css %}
4+
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
45
{% bootstrap_javascript %}
6+
<script>
7+
$(function () {
8+
$('[data-toggle="tooltip"]').tooltip()
9+
})
10+
</script>
11+
<style>
12+
big {
13+
font-size:20px;
14+
padding : 2px 0px 2px 0px;
15+
}
16+
</style>
517
{% load staticfiles %}
618
{% load django_markdown %}
719

@@ -18,6 +30,14 @@
1830
<h1><a href="/">Simple QA </a><small>Open Questions</small></h1>
1931
</div>
2032

33+
{% if message %}
34+
<div class="alert alert-danger" role="alert">{{ message }}
35+
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
36+
<span aria-hidden="true">&times;</span>
37+
</button>
38+
</div>
39+
{% endif %}
40+
2141
<div class="jumbotron">
2242
<p class="pull-right">{{ question.pub_date }}</p>
2343
<h3><cool>Q: </cool>{{ question.question_text }}</h3>
@@ -33,7 +53,12 @@ <h3 class="panel-title">Answers</h3>
3353
{% for answer in answers %}
3454
<div class="row">
3555
{% if user.is_authenticated %}
36-
<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>
56+
<div class ="col-md-1">
57+
<div class="row">
58+
<div class ="col-sm-1"><big class="pull-left"><b>{{ answer.votes }}</b></big></div>
59+
<div class ="col-sm-1"><a class="btn btn-sm btn-success" data-toggle="tooltip" data-placement="top" title="Vote Up this answer" href="/vote/{{ user.id }}/{{ answer.id }}/{{ question.id }}/0/"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span></a> <a class="btn btn-sm btn-danger" data-toggle="tooltip" data-placement="bottom" title="Vote Down this answer" href="/vote/{{ user.id }}/{{ answer.id }}/{{ question.id }}/1/"><span class="glyphicon glyphicon-minus" aria-hidden="true"></span></a></div>
60+
</div>
61+
</div>
3762
{% endif %}
3863
<div class ="col-md-9">
3964
<a href="/comment/{{ answer.id }}/" class="pull-right"><small><span class="glyphicon glyphicon-comment" aria-hidden="true"></span> Comment &nbsp;</small></a>

qa/views.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,37 @@ def add_answer(request):
182182

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

185-
def vote(request, answer_id, question_id, op_code):
185+
def vote(request, user_id, answer_id, question_id, op_code):
186+
187+
user_ob = User.objects.get(id=user_id)
188+
user = UserProfile.objects.get(user=user_ob)
186189
answer = Answer.objects.get(pk=answer_id)
190+
question = Question.objects.get(pk=question_id)
191+
192+
answer_list = question.answer_set.order_by('-votes')
193+
194+
paginator = Paginator(answer_list, 10)
195+
page = request.GET.get('page')
196+
try:
197+
answers = paginator.page(page)
198+
except PageNotAnInteger:
199+
# If page is not an integer, deliver first page.
200+
answers = paginator.page(1)
201+
except EmptyPage:
202+
# If page is out of range (e.g. 9999), deliver last page of results.
203+
answers = paginator.page(paginator.num_pages)
204+
205+
if Answer.objects.filter(id=answer_id, user_data=user).exists():
206+
return render(request, 'qa/detail.html', {'question': question, 'answers': answers, 'message':"You cannot vote on your answer!"})
207+
208+
if Voter.objects.filter(answer_id=answer_id, user=user).exists():
209+
return render(request, 'qa/detail.html', {'question': question, 'answers': answers, 'message':"You've already cast vote on this answer!"})
210+
211+
v = Voter()
212+
v.user = user
213+
v.answer = answer
214+
v.save()
215+
187216
if op_code == '0':
188217
answer.votes += 1
189218
u = answer.user_data
@@ -196,8 +225,6 @@ def vote(request, answer_id, question_id, op_code):
196225
u.save()
197226
answer.save()
198227

199-
question = Question.objects.get(pk=question_id)
200-
201228
answer_list = question.answer_set.order_by('-votes')
202229

203230
paginator = Paginator(answer_list, 10)

simpleqa/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
url(r'^admin/', include(admin.site.urls)),
1212
url(r'^add/$', views.add, name='add'),
1313
url(r'^answer/$', views.add_answer, name='add_answer'),
14-
url(r'^vote/(?P<answer_id>\d+)/(?P<question_id>\d+)/(?P<op_code>\d+)/$', views.vote, name='vote'),
14+
url(r'^vote/(?P<user_id>\d+)/(?P<answer_id>\d+)/(?P<question_id>\d+)/(?P<op_code>\d+)/$', views.vote, name='vote'),
1515
url(r'^comment/(?P<answer_id>\d+)/$', views.comment, name='comment'),
1616
url(r'^search/$', views.search, name='search'),
1717

0 commit comments

Comments
 (0)