Authentication - Rewrite Show views to use django user model

This commit is contained in:
Evert Prants 2017-11-13 19:42:10 +02:00
parent 5fb6911960
commit 926ecbceb3
Signed by: evert
GPG Key ID: 1688DA83D222D0B5
2 changed files with 7 additions and 13 deletions

View File

@ -30,7 +30,7 @@
</section>
<section class="submissions">
<a href="/show/{{show.abbr}}" class="button"><i class="fa fa-fw fa-home"></i>&nbsp;Show Index</a>
{% if request.session.user_id %}
{% if user.is_authenticated %}
<a href="/show/{{show.abbr}}/episode/{{episode.season.number}}/{{episode.episode}}/submit" class="button"><i class="fa fa-fw fa-plus"></i>&nbsp;Submit New Link</a>
{% else %}
<span class="fillertext"><a href="/login">Log in</a> to submit a link</span>

View File

@ -2,11 +2,13 @@ from django.template import RequestContext
from django.shortcuts import render
from django.views import View
from django.views.generic.base import TemplateView
from django.contrib.auth.decorators import login_required
from django.conf import settings
from django.http import Http404
from django.http import HttpResponse
from django.http import HttpResponseRedirect
from django.db.models import Case, When, Value, IntegerField, Count, F
from django.contrib.auth.mixins import LoginRequiredMixin
from LandingPage.models import User
from LandingPage.models import Show
@ -85,15 +87,11 @@ class EpisodeView(TemplateView):
return ctx
# Submission form GET and POST
@login_required
def SubmissionForm(req, abbreviation, season, episode):
show = Show.objects.get(abbr=abbreviation)
episode = Episode.objects.filter(show=show,season__number=season,episode=episode).first()
# Check for login status
if not 'user_id' in req.session:
return HttpResponse('<h1>Error</h1><p>You need to be logged in to submit. Please <a href=/login>log in</a></p>', status=400)
user = User.objects.get(user_id=req.session['user_id'])
user = req.user
# 404's
if not show:
@ -144,16 +142,12 @@ def SubmissionForm(req, abbreviation, season, episode):
# Vote request
# /show/{{abbr}}/vote/{{submission id}}/{{positive == 1}}
class SubmissionVoteSubmit(View):
class SubmissionVoteSubmit(LoginRequiredMixin, View):
def post (self, req, abbreviation, subid, positive):
# Convert positive parameter into a boolean
pos_bool = int(positive) == 1
# Check for login status
if not 'user_id' in req.session:
return HttpResponse('<h1>Error</h1><p>You need to be logged in to vote. Please <a href=/login>log in</a></p>', status=400)
user = User.objects.get(user_id=req.session['user_id'])
user = req.user
# Get the submission from the database
submission = Submission.objects.filter(id=subid).first()