Show - Allow submissions to be voted for

This commit is contained in:
Evert Prants 2017-11-11 11:24:39 +02:00
parent 1e868efb72
commit 528e95fc7b
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
4 changed files with 58 additions and 7 deletions

View File

@ -139,7 +139,7 @@ a.episode .submission_cnt {
.submission { .submission {
padding: 10px; padding: 10px;
} }
.submission a { .submission a.link {
font-size: 180%; font-size: 180%;
text-decoration: none; text-decoration: none;
color: #191919; color: #191919;
@ -156,6 +156,7 @@ a.episode .submission_cnt {
border-radius: 5px; border-radius: 5px;
font-weight: bold; font-weight: bold;
cursor: pointer; cursor: pointer;
text-decoration: none;
} }
.vote-positive { .vote-positive {
background-color: #a4ffa7; background-color: #a4ffa7;

View File

@ -20,10 +20,14 @@
<div class="submission-list"> <div class="submission-list">
{% for sbm in submissions %} {% for sbm in submissions %}
<div class="submission{% if sbm.positives < sbm.negatives %} buried{% endif %}"> <div class="submission{% if sbm.positives < sbm.negatives %} buried{% endif %}">
<a href="{{sbm.url}}"><i class="fa fa-fw fa-globe"></i>&nbsp;{{sbm.url}}</a> <a href="{{sbm.url}}" class="link"><i class="fa fa-fw fa-globe"></i>&nbsp;{{sbm.url}}</a>
<div class="vote-btns"> <div class="vote-btns" data-vote-id="{{sbm.id}}">
<div class="vote-positive"><i class="fa fa-fw fa-thumbs-up"></i>&nbsp;{{sbm.positives}}</div> <a href="/show/{{show.abbr}}/vote/{{sbm.id}}/1" class="vote-positive">
<div class="vote-negative"><i class="fa fa-fw fa-thumbs-down"></i>&nbsp;{{sbm.negatives}}</div> <i class="fa fa-fw fa-thumbs-up"></i>&nbsp;{{sbm.positives}}
</a>
<a href="/show/{{show.abbr}}/vote/{{sbm.id}}/0" class="vote-negative">
<i class="fa fa-fw fa-thumbs-down"></i>&nbsp;{{sbm.negatives}}
</a>
</div> </div>
</div> </div>
{% empty %} {% empty %}

View File

@ -20,5 +20,6 @@ from . import views
urlpatterns = [ urlpatterns = [
url(r'^$', views.IndexView.as_view()), url(r'^$', views.IndexView.as_view()),
url(r'^episode/(?P<season>\d{1,4})/(?P<episode>\d{1,4})/?$', views.EpisodeView.as_view()) url(r'^episode/(?P<season>\d{1,4})/(?P<episode>\d{1,4})/?$', views.EpisodeView.as_view()),
url(r'^vote/(?P<subid>\d+)/(?P<positive>[0-1])/?$', views.SubmissionVoteSubmit.as_view())
] ]

View File

@ -8,10 +8,11 @@ from django.http import HttpResponse
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.db.models import Case, When, Value, IntegerField, Count, F from django.db.models import Case, When, Value, IntegerField, Count, F
from LandingPage.models import User
from LandingPage.models import Show from LandingPage.models import Show
from LandingPage.models import Season from LandingPage.models import Season
from LandingPage.models import Episode from LandingPage.models import Episode
from LandingPage.models import Submission from LandingPage.models import Submission, SubmissionVote
# Create your views here. # Create your views here.
@ -78,3 +79,47 @@ class EpisodeView(TemplateView):
ctx['submissions'] = submissions ctx['submissions'] = submissions
return ctx return ctx
# Vote request
# /show/{{abbr}}/vote/{{submission id}}/{{positive == 1}}
class SubmissionVoteSubmit(View):
def get (self, req, abbreviation, subid, positive):
# Check for login status
if not req.session['user_id']:
r = HttpResponse('<h1>Error</h1><p>You need to be logged in to vote. Please <a href=/login>log in</a></p>')
r.status = 400
return r
user = User.objects.get(user_id=req.session['user_id'])
# Get the submission from the database
submission = Submission.objects.filter(id=subid).first()
# 404
if not submission:
raise Http404("Submission does not exist")
# Prevent voting for own submission
if submission.user == user:
r = HttpResponse('<h1>Error</h1><p>You cannot vote for your own submission.</p>')
r.status = 400
return r
# Allow changing a vote from positive to negative or vice-versa. Delete vote if its a re-vote
vote = submission.votes.filter(user=user,submission__id=submission.id).first()
if vote:
if not vote.positive == (int(positive) == 1):
vote.positive = int(positive) == 1
vote.save()
else:
vote.delete()
else:
new_vote = SubmissionVote(
user=user,
submission=submission,
positive=int(positive) == 1
)
new_vote.save()
return HttpResponseRedirect('/show/%s/episode/%d/%d'%(abbreviation, submission.episode.season.number, submission.episode.episode))