2017-12-28 10:39:02 +00:00
# Episodes.Community - Community-Driven TV Show Episode Link Sharing Site
# Copyright (C) 2017 Evert "Diamond" Prants <evert@lunasqu.ee>, Taizo "Tsa6" Simpson <taizo@tsa6.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-08-25 18:03:37 +00:00
from django . shortcuts import render
2017-09-22 20:00:24 +00:00
from django . views import View
2017-09-24 20:53:10 +00:00
from django . views . generic . base import TemplateView
2017-11-13 17:38:51 +00:00
from django . contrib . auth import login as auth_login , authenticate
2017-09-22 20:00:24 +00:00
from django . conf import settings
from django . http import HttpResponse
from django . http import HttpResponseRedirect
2017-09-24 20:53:10 +00:00
from django . db . models import Max
2017-11-14 13:32:26 +00:00
from django . contrib . auth . views import logout
2017-09-22 20:00:24 +00:00
import requests
import hashlib
import json
2017-09-22 21:48:39 +00:00
from . models import User
2017-09-24 20:53:10 +00:00
from . models import Show
2017-10-05 10:37:09 +00:00
from . models import Submission
from . models import DiscussionBoard
2017-08-25 18:03:37 +00:00
# Create your views here.
2017-09-22 20:00:24 +00:00
# Redirect url should point to this view
class LoginRedirect ( View ) :
def get ( self , req ) :
2017-09-22 20:13:45 +00:00
# Check request has correct arguments
request_valid = ' state ' in req . GET and ' code ' in req . GET
if not request_valid :
r = HttpResponse ( ' <h1>Error</h1><p>There was an error in your request. Please <a href=/login>try again</a></p> ' )
r . status = 400
return r
2017-09-22 20:00:24 +00:00
# Check state
userstate = generateState ( req )
if userstate == req . GET [ ' state ' ] :
code = req . GET [ ' code ' ]
2017-11-13 17:38:51 +00:00
user = authenticate ( code = code )
if user is not None and user . is_active :
auth_login ( req , user )
2017-09-22 20:00:24 +00:00
return HttpResponseRedirect ( ' / ' )
2017-11-13 17:38:51 +00:00
return HttpResponse ( ' <h1>Error</h1><br><p>It looks like something went wrong while trying to authenticate you. Please try again later.</p> ' , status = 500 )
return HttpResponse ( ' <h1>Unmatching state tokens</h1><br><p>It looks like the request to login wasn \' t started by you. Try going back to the home page and logging in again.</p> ' , status = 400 )
2017-09-22 20:00:24 +00:00
class Login ( View ) :
def get ( self , req ) :
2017-11-13 17:38:51 +00:00
url = ' %s authorize?response_type=code&client_id= %s &redirect_uri= %s &scope=email privilege&state= %s ' % ( settings . AUTH_TOKEN_ENDPOINT , settings . AUTH_CLIENT_ID , settings . AUTH_REDIRECT_URL , generateState ( req ) )
2017-09-22 20:00:24 +00:00
response = HttpResponse ( " Redirecting you to the IcyNet auth page... " )
response . status_code = 302
response [ ' Location ' ] = url
return response
2017-11-14 13:32:26 +00:00
def LogoutView ( request ) :
logout ( request )
return HttpResponseRedirect ( ' / ' )
2017-09-22 20:00:24 +00:00
def generateState ( request ) :
request . session . save ( )
m = hashlib . sha256 ( )
m . update ( bytearray ( request . session . session_key , ' utf-8 ' ) )
m . update ( bytearray ( settings . SECRET_KEY , ' utf-8 ' ) )
return m . hexdigest ( )
2017-09-24 20:53:10 +00:00
class LandingPage ( TemplateView ) :
template_name = " landing_page.html "
def get_context_data ( self , * * kwargs ) :
ctx = super ( ) . get_context_data ( )
2017-10-05 10:37:09 +00:00
ctx [ ' recent ' ] = Show . objects . annotate ( recency = Max ( ' episodes__airdate ' ) ) . order_by ( ' -recency ' ) [ : 8 ]
ctx [ ' stats ' ] = {
' shows ' : Show . objects . count ( ) ,
' episodes ' : Submission . objects . count ( ) ,
' boards ' : DiscussionBoard . objects . count ( )
}
2017-09-24 20:53:10 +00:00
return ctx