From 9b9f6720e079669fd884d83f6b69833e20d03ff7 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Mon, 3 May 2021 20:35:35 +0100 Subject: [PATCH] not found now does 404 & moved login --- core/TESTS/tests.py | 6 +++--- core/TESTS/tests_caves.py | 8 ++++---- core/views/auth.py | 21 ++++++++++++++++++++- core/views/caves.py | 2 +- core/views/expo.py | 14 +++++++------- core/views/logbooks.py | 2 +- core/views/login.py | 17 ----------------- core/views/other.py | 2 +- 8 files changed, 37 insertions(+), 35 deletions(-) delete mode 100644 core/views/login.py diff --git a/core/TESTS/tests.py b/core/TESTS/tests.py index 7a12637..b376840 100644 --- a/core/TESTS/tests.py +++ b/core/TESTS/tests.py @@ -75,7 +75,7 @@ class PageTests(TestCase): def test_expoweb_dir_no_index(self): response = self.client.get('/handbook/troggle') content = response.content.decode() - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, 404) ph = r'Page not found handbook/troggle/index.html' phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'") @@ -116,7 +116,7 @@ class PageTests(TestCase): def test_expoweb_notfound(self): response = self.client.get('/handbook/zyxxypqrqx.html') content = response.content.decode() - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, 404) ph = r'

Page not found' phmatch = re.search(ph, content) self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'") @@ -367,7 +367,7 @@ class PageTests(TestCase): def test_page_photos_not_ok(self): # Flat file tests. response = self.client.get('/photos/2018/PhilipSargent/corin.jpeg') - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, 404) content = response.content.decode() ph = r'Page not found 2018/PhilipSargent/corin.jpeg' phmatch = re.search(ph, content) diff --git a/core/TESTS/tests_caves.py b/core/TESTS/tests_caves.py index 8a08c71..13152f7 100644 --- a/core/TESTS/tests_caves.py +++ b/core/TESTS/tests_caves.py @@ -156,11 +156,11 @@ class FixturePageTests(TestCase): self.assertIsNotNone(phmatch, "Failed to find expected text: '" + ph +"'") def test_fix_cave_bare_url115(self): - '''Expect to get Page Not Found and status 200''' + '''Expect to get Page Not Found and status 404''' ph = self.ph ph = 'Probably a mistake.' response = self.client.get('/1623/115') - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, 404) content = response.content.decode() phmatch = re.search(ph, content) @@ -168,11 +168,11 @@ class FixturePageTests(TestCase): def test_fix_cave_slug115(self): - '''Expect to get Page Not Found and status 200''' + '''Expect to get Page Not Found and status 404''' ph = self.ph ph = 'Probably a mistake.' response = self.client.get('/1623-115') - self.assertEqual(response.status_code, 200) + self.assertEqual(response.status_code, 404) content = response.content.decode() phmatch = re.search(ph, content) diff --git a/core/views/auth.py b/core/views/auth.py index 19b80a4..a7b2ca1 100644 --- a/core/views/auth.py +++ b/core/views/auth.py @@ -1,8 +1,27 @@ from builtins import str + +from django.conf import settings 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 +from django.contrib.auth.decorators import login_required + +"""This enforces the login requirement for non-public pages using +the decorator mechanism. +https://www.fullstackpython.com/django-contrib-auth-decorators-login-required-examples.html +""" + +class login_required_if_public(object): + + def __init__(self, f): + if settings.PUBLIC_SITE: + self.f = login_required(f) + else: + self.f = f + + def __call__(self, *args, **kwargs): + return self.f(*args, **kwargs) + # 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 diff --git a/core/views/caves.py b/core/views/caves.py index 229aa57..4a8919e 100644 --- a/core/views/caves.py +++ b/core/views/caves.py @@ -20,7 +20,7 @@ from troggle.core.models.troggle import Expedition, DataIssue from troggle.core.models.caves import CaveSlug, Cave, CaveAndEntrance, QM, EntranceSlug, Entrance, Area, SurvexStation, GetCaveLookup from troggle.core.forms import CaveForm, CaveAndEntranceFormSet, EntranceForm, EntranceLetterForm #from troggle.core.forms import VersionControlCommentForm -from .login import login_required_if_public +from .auth import login_required_if_public '''Manages the complex procedures to assemble a cave description out of the compnoents Manages the use of cavern to parse survex files to produce 3d and pos files diff --git a/core/views/expo.py b/core/views/expo.py index 9ff533b..57d1a7d 100644 --- a/core/views/expo.py +++ b/core/views/expo.py @@ -14,7 +14,7 @@ from django.contrib import admin import django.forms as forms -from .login import login_required_if_public +from .auth import login_required_if_public from troggle.core.models.caves import Cave import troggle.core.views.caves import troggle.settings as settings @@ -121,7 +121,7 @@ def expowebpage(request, expowebpath, path): '''Adds menus and serves an HTML page ''' if not Path(expowebpath / path).is_file(): - return render(request, 'pagenotfound.html', {'path': path}) + return render(request, 'pagenotfound.html', {'path': path}, status="404") with open(os.path.normpath(expowebpath / path), "r") as o: html = o.read() @@ -164,9 +164,9 @@ def mediapage(request, subpath=None, doc_root=None): try: return HttpResponse(content=open(filetobeopened, "rb"), content_type=getmimetype(subpath)) except IOError: - return render(request, 'pagenotfound.html', {'path': subpath}) + return render(request, 'pagenotfound.html', {'path': subpath}, status="404") else: - return render(request, 'pagenotfound.html', {'path': subpath}) + return render(request, 'pagenotfound.html', {'path': subpath}, status="404") @@ -196,7 +196,7 @@ def expopage(request, path): if (expowebpath / path / p).is_file(): # This needs to reset the path to the new subdirectory return HttpResponseRedirect('/'+str(Path(path) / p)) - return render(request, 'pagenotfound.html', {'path': Path(path) / "index.html"}) + return render(request, 'pagenotfound.html', {'path': Path(path) / "index.html"}, status="404") if path.endswith("/"): # we already know it is not a directory. @@ -210,7 +210,7 @@ def expopage(request, path): try: return HttpResponse(content=open(filetobeopened, "rb"), content_type=getmimetype(path)) except IOError: - return render(request, 'pagenotfound.html', {'path': path}) + return render(request, 'pagenotfound.html', {'path': path}, status="404") @@ -310,7 +310,7 @@ def editexpopage(request, path): git = settings.GIT with open(filepath, "w") as f: f.write(result) - print(f'WROTE {cwd}---{filename} ') + #print(f'WROTE {cwd}---{filename} ') subprocess.call([git, "add", filename], cwd=cwd) subprocess.call([git, "commit", "-m", 'Edit this page'], cwd=cwd) diff --git a/core/views/logbooks.py b/core/views/logbooks.py index 2ec6991..461ff91 100644 --- a/core/views/logbooks.py +++ b/core/views/logbooks.py @@ -16,7 +16,7 @@ from troggle.core.models.troggle import Expedition, Person, PersonExpedition from troggle.core.utils import TROG from troggle.core.models.caves import LogbookEntry, PersonTrip from troggle.core.models.survex import SurvexBlock -from .login import login_required_if_public +from .auth import login_required_if_public from troggle.parsers.logbooks import LoadLogbookForExpedition from troggle.parsers.people import GetPersonExpeditionNameLookup diff --git a/core/views/login.py b/core/views/login.py deleted file mode 100644 index 687b370..0000000 --- a/core/views/login.py +++ /dev/null @@ -1,17 +0,0 @@ -from django.contrib.auth.decorators import login_required -from django.conf import settings -"""This enforces the login requirement for non-public pages using -the decorator mechanism. -https://www.fullstackpython.com/django-contrib-auth-decorators-login-required-examples.html -""" - -class login_required_if_public(object): - - def __init__(self, f): - if settings.PUBLIC_SITE: - self.f = login_required(f) - else: - self.f = f - - def __call__(self, *args, **kwargs): - return self.f(*args, **kwargs) diff --git a/core/views/other.py b/core/views/other.py index 83d6113..6f06ecf 100644 --- a/core/views/other.py +++ b/core/views/other.py @@ -16,7 +16,7 @@ from troggle.parsers.imports import import_logbooks, import_QMs, import_drawings # from databaseReset import reinit_db # don't do this. databaseRest runs code *at import time* from troggle.core.models.troggle import Expedition, Person, PersonExpedition from troggle.core.models.caves import LogbookEntry, QM, Cave, PersonTrip -from .login import login_required_if_public +from .auth import login_required_if_public '''Utility functions and code to serve the control panel and individual user's progress and task list (deprecated as we do not have individual user login).