From 528e95fc7beff7243746ad45f2aad177104803a3 Mon Sep 17 00:00:00 2001 From: Evert Date: Sat, 11 Nov 2017 11:24:39 +0200 Subject: [PATCH] Show - Allow submissions to be voted for --- LandingPage/static/css/style.css | 3 +- Show/templates/episode.html | 12 +++++--- Show/urls.py | 3 +- Show/views.py | 47 +++++++++++++++++++++++++++++++- 4 files changed, 58 insertions(+), 7 deletions(-) diff --git a/LandingPage/static/css/style.css b/LandingPage/static/css/style.css index f8c6dba..5c3ff2b 100644 --- a/LandingPage/static/css/style.css +++ b/LandingPage/static/css/style.css @@ -139,7 +139,7 @@ a.episode .submission_cnt { .submission { padding: 10px; } -.submission a { +.submission a.link { font-size: 180%; text-decoration: none; color: #191919; @@ -156,6 +156,7 @@ a.episode .submission_cnt { border-radius: 5px; font-weight: bold; cursor: pointer; + text-decoration: none; } .vote-positive { background-color: #a4ffa7; diff --git a/Show/templates/episode.html b/Show/templates/episode.html index b50f1cf..3e475a4 100644 --- a/Show/templates/episode.html +++ b/Show/templates/episode.html @@ -20,10 +20,14 @@
{% for sbm in submissions %}
-  {{sbm.url}} -
-
 {{sbm.positives}}
-
 {{sbm.negatives}}
+  {{sbm.url}} +
{% empty %} diff --git a/Show/urls.py b/Show/urls.py index 504224c..2bfc217 100644 --- a/Show/urls.py +++ b/Show/urls.py @@ -20,5 +20,6 @@ from . import views urlpatterns = [ url(r'^$', views.IndexView.as_view()), - url(r'^episode/(?P\d{1,4})/(?P\d{1,4})/?$', views.EpisodeView.as_view()) + url(r'^episode/(?P\d{1,4})/(?P\d{1,4})/?$', views.EpisodeView.as_view()), + url(r'^vote/(?P\d+)/(?P[0-1])/?$', views.SubmissionVoteSubmit.as_view()) ] diff --git a/Show/views.py b/Show/views.py index 1646a0c..3512549 100644 --- a/Show/views.py +++ b/Show/views.py @@ -8,10 +8,11 @@ from django.http import HttpResponse from django.http import HttpResponseRedirect 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 Season from LandingPage.models import Episode -from LandingPage.models import Submission +from LandingPage.models import Submission, SubmissionVote # Create your views here. @@ -78,3 +79,47 @@ class EpisodeView(TemplateView): ctx['submissions'] = submissions 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('

Error

You need to be logged in to vote. Please log in

') + 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('

Error

You cannot vote for your own submission.

') + 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)) +