New user login/logoff system using standard Dj

This commit is contained in:
Philip Sargent
2021-04-06 00:49:09 +01:00
parent 6d6bec35f2
commit d1cd72c5f8
25 changed files with 348 additions and 197 deletions

View File

@@ -126,18 +126,12 @@ class PageTests(TestCase):
def test_expoweb_dir(self):
response = self.client.get('/handbook')
content = response.content.decode()
self.assertEqual(response.status_code, 200)
ph = r'Introduction to expo'
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'")
self.assertEqual(response.status_code, 302) # directory, so redirects to /index.htm
def test_expoweb_dirslash(self):
response = self.client.get('/handbook/')
content = response.content.decode()
self.assertEqual(response.status_code, 200)
ph = r'Introduction to expo'
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'")
self.assertEqual(response.status_code, 302) # directory, so redirects to /index.htm
def test_expoweb_dir_no_index(self):
response = self.client.get('/handbook/troggle')
@@ -147,6 +141,31 @@ class PageTests(TestCase):
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'")
def test_expoweb_dir_with_index_htm(self):
response = self.client.get('/years/1999/index.htm')
content = response.content.decode()
self.assertEqual(response.status_code, 200) # directory, so redirects to /index.htm
ph = r'Passage descriptions for 1999'
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'")
def test_expoweb_dir_with_index_html(self):
response = self.client.get('/years/2015/index.html')
content = response.content.decode()
self.assertEqual(response.status_code, 200) # directory, so redirects to /index.htm
ph = r'Things left at top camp 2014'
phmatch = re.search(ph, content)
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'")
def test_expoweb_dir_with_index2(self):
response = self.client.get('/handbook/index.htm')
content = response.content.decode()
self.assertEqual(response.status_code, 200)
ph = r'Introduction to expo'
phmatch = re.search(ph, content)
#print("\n ! - test_expoweb_dir_with_index2\n{}\n{}".format(response.reason_phrase, content))
self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'")
def test_expoweb_htm(self):
response = self.client.get('/handbook/index.htm')
content = response.content.decode()
@@ -234,9 +253,9 @@ class PageTests(TestCase):
def test_page_folk(self):
# This page is separately generated, so it has the full data content
response = self.client.get('/folk/')
response = self.client.get('/folk/index.htm')
content = response.content.decode()
self.assertEqual(response.status_code, 200)
self.assertEqual(response.status_code, 200)
for ph in [ r'involves some active contribution',
r'Naomi Griffiths',
r'Gail Smith',

58
core/views/auth.py Normal file
View File

@@ -0,0 +1,58 @@
from builtins import str
from django.shortcuts import render
from django.http import Http404, HttpResponseRedirect
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth import forms as auth_forms
# This is copied from CUYC.cuy.website.view.auth
# If we want to do the whole online-email thing, we would also need to copy across the code in these
# imported files and delete what is superfluous.
# Or we could just load the latest version of django-registration app.
#from cuy.club.models import Member, Message
#from ..forms import WebsiteLoginForm, WebsiteRegisterForm
#from ...common import mail_site_error
#from .generic import user_is_active
'''The login and logout functions.
This is also where we would manage registration: for people wanting to create and validate their individual
logon accounts/forgottenpassword'''
############################
# Authentication Functions #
############################
def expologout(request):
login_form = auth_forms.AuthenticationForm()
logout(request)
return render(request, 'login/logout.html', {'form':login_form})
def expologin(request):
# GET
if not request.method == 'POST':
if (not request.user.is_authenticated) or (not request.user.is_active):
return render(request, 'login/index.html', {})
else:
# going to login page when you are already logged in
return render(request, 'tasks.html', {})
# POST
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is None:
return render(request, 'login/index.html',
{'invalid': True, 'username':username})
if not user.is_active:
return render(request, 'login/enable.html',
{'login_state':'notenabled'})
try:
login(request, user)
# Should do the ?next= stuff here..
return render(request, 'tasks.html', {})
except:
return render(request, 'errors/generic.html', {})

View File

@@ -35,8 +35,8 @@ def showrequest(request):
return HttpResponse(request.GET)
def frontpage(request):
'''never seen in practice'''
# bthe messages system does a popup on this page if there is a recent message, e.g. from the admin site actions.
'''never seen in common practice. Logon should redirect here when this is more useful'''
# the messages system does a popup on this page if there is a recent message, e.g. from the admin site actions.
# via django.contrib.messages.middleware.MessageMiddleware
# this is set in the templates.
if request.user.is_authenticated():