Merge branch 'python3-new' of ssh://expo.survex.com/home/expo/troggle into python3-new

This commit is contained in:
Martin Green
2022-06-23 19:02:10 +01:00
3 changed files with 36 additions and 15 deletions

View File

@@ -12,6 +12,7 @@ from django.urls import reverse
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from django.urls import NoReverseMatch
import troggle.settings as settings import troggle.settings as settings
from troggle.core.views import expo from troggle.core.views import expo
@@ -235,11 +236,13 @@ def rendercave(request, cave, slug, cave_id=''):
editable = False editable = False
if not cave_id: if not cave_id:
cave_id = slug # cave.unofficial_number cave_id = slug # cave.unofficial_number
try: context = {'cave_editable': editable, 'settings': settings, 'cave': cave, 'cavepage': True,
context = {'cave_editable': editable, 'settings': settings, 'cave': cave, 'cavepage': True,
'cave_id': cave_id, 'svxstem': str(svxstem), 'svx3d':svx3d} 'cave_id': cave_id, 'svxstem': str(svxstem), 'svx3d':svx3d}
r = render(request, templatefile, context) # crashes here if url not set up for 'edit_cave' in urls.py try:
r = render(request, templatefile, context) # crashes here with NoReverseMatch if url not set up for 'edit_cave' in urls.py
return r return r
except NoReverseMatch:
raise
except: except:
message = f'Failed to render cave: {slug}' message = f'Failed to render cave: {slug}'
return render(request,'errors/generic.html', {'message': message}) return render(request,'errors/generic.html', {'message': message})
@@ -261,23 +264,27 @@ def cavepage(request, karea, subpath):
try: try:
cave = Cave.objects.get(url = kpath) # ideally this will be unique cave = Cave.objects.get(url = kpath) # ideally this will be unique
# print(f" ! cavepage: url={kpath} -- {cave}")
r = rendercave(request, cave, cave.slug())
return r
except Cave.DoesNotExist: except Cave.DoesNotExist:
# probably a link to text or an image e.g. 1623/161/l/rl89a.htm i.e. an expoweb page # probably a link to text or an image e.g. 1623/161/l/rl89a.htm i.e. an expoweb page
# cannot assume that this is a simple cave page, for a cave we don't know. # cannot assume that this is a simple cave page, for a cave we don't know.
# print(f" ! cavepage: url={kpath} A cave of this name does not exist")
return expo.expopage(request, kpath) return expo.expopage(request, kpath)
except Cave.MultipleObjectsReturned: except Cave.MultipleObjectsReturned:
caves = Cave.objects.filter(url = kpath) caves = Cave.objects.filter(url = kpath)
# print(f" ! cavepage: url={kpath} multiple caves exist")
# we should have a -several variant for the cave pages, not just the svxcaves: # we should have a -several variant for the cave pages, not just the svxcaves:
return render(request, 'svxcaveseveral.html', {'settings': settings, "caves":caves }) return render(request, 'svxcaveseveral.html', {'settings': settings, "caves":caves })
try:
r = rendercave(request, cave, cave.slug())
return r
except NoReverseMatch:
raise
except: except:
message = f'Failed to find cave: {kpath}' message = f'Failed to render cave: {kpath} (it does exist and is unique) because of a Django URL resolution error. Check urls.py.'
return render(request,'errors/generic.html', {'message': message}) return render(request,'errors/generic.html', {'message': message})
return rendercave(request, cave, cave.slug(), cave_id=cave_id) # return rendercave(request, cave, cave.slug(), cave_id=cave_id)
def caveEntrance(request, slug): def caveEntrance(request, slug):
try: try:

View File

@@ -1,5 +1,8 @@
import os import os
import re import re
from sys import getfilesystemencoding as sys_getfilesystemencoding
from pathlib import Path from pathlib import Path
from urllib.parse import urljoin, unquote as urlunquote from urllib.parse import urljoin, unquote as urlunquote
from urllib.request import urlopen from urllib.request import urlopen
@@ -126,7 +129,12 @@ def expowebpage(request, expowebpath, path):
# Should not get here if the path has suffix "_edit" # Should not get here if the path has suffix "_edit"
print(f' - 404 error in expowebpage() {path}') print(f' - 404 error in expowebpage() {path}')
return render(request, 'pagenotfound.html', {'path': path}, status="404") return render(request, 'pagenotfound.html', {'path': path}, status="404")
# print(f' - {sys_getfilesystemencoding()=}')
if (sys_getfilesystemencoding() != "utf-8"):
return HttpResponse(default_head + '<h3>UTF-8 Parsing Failure:<br>Default file encoding on this Troggle installation is not UTF-8:<br>failure detected in expowebpage in views.expo.py</h3> Please Please reconfigure Debian/Apache/Django to fix this, i.e. contact Wookey. </body' )
# This next bit can be drastically simplified now that we know that the system encoding actually is utf-8
try: try:
with open(expowebpath / path, "r", encoding='utf-8') as o: with open(expowebpath / path, "r", encoding='utf-8') as o:
html = o.read() html = o.read()
@@ -221,9 +229,13 @@ def expopage(request, path):
# do not redirect to a file path without the slash as we may get in a loop. Let the user fix it: # do not redirect to a file path without the slash as we may get in a loop. Let the user fix it:
return render(request, 'dirnotfound.html', {'path': path, 'subpath': path[0:-1]}) return render(request, 'dirnotfound.html', {'path': path, 'subpath': path[0:-1]})
# So it must be a file in /expoweb/ but not .htm or .html probably an image # So it must be a file in /expoweb/ but not .htm or .html probably an image, maybe a txt file
filetobeopened = expowebpath / path filetobeopened = expowebpath / path
# print(f' - {sys_getfilesystemencoding()=}')
if (sys_getfilesystemencoding() != "utf-8"):
return HttpResponse(default_head + '<h3>UTF-8 Parsing Failure:<br>Default file encoding on this Troggle installation is not UTF-8:<br>failure detected in expowebpage in views.expo.py</h3> Please Please reconfigure Debian/Apache/Django to fix this, i.e. contact Wookey. </body' )
try: try:
content = open(filetobeopened, "rb") content = open(filetobeopened, "rb")
content_type=getmimetype(path) content_type=getmimetype(path)
@@ -279,6 +291,10 @@ def editexpopage(request, path):
except Cave.DoesNotExist: except Cave.DoesNotExist:
pass pass
print(f' - {sys_getfilesystemencoding()=}')
if (sys_getfilesystemencoding() != "utf-8"):
return HttpResponse(default_head + '<h3>UTF-8 Parsing Failure:<br>Default file encoding on this Troggle installation is not UTF-8:<br>failure detected in expowebpage in views.expo.py</h3> Please Please reconfigure Debian/Apache/Django to fix this, i.e. contact Wookey. </body' )
try: try:
filepath = Path(settings.EXPOWEB) / path filepath = Path(settings.EXPOWEB) / path
o = open(filepath, "r", encoding="utf8") o = open(filepath, "r", encoding="utf8")
@@ -350,8 +366,6 @@ def editexpopage(request, path):
pageform = ExpoPageForm({"html": body, "title": "Missing"}) pageform = ExpoPageForm({"html": body, "title": "Missing"})
return render(request, 'editexpopage.html', {'path': path, 'form': pageform, }) return render(request, 'editexpopage.html', {'path': path, 'form': pageform, })
class ExpoPageForm(forms.Form): class ExpoPageForm(forms.Form):
'''The form used by the editexpopage function '''The form used by the editexpopage function
''' '''

View File

@@ -527,7 +527,7 @@ div#scene {
<p><a href="https://aardgoose.github.io/CaveView.js/">CaveView</a> display of the .3d file is temporarily disabled while we fix things (Nov.2021). See <a href="/handbook/computing/todo.html">/handbook/computing/todo.html</a>. <p><a href="https://aardgoose.github.io/CaveView.js/">CaveView</a> display of the .3d file is temporarily disabled while we fix things (Nov.2021). See <a href="/handbook/computing/todo.html">/handbook/computing/todo.html</a>.
<a href="/survexfile">All survex files</a> &nbsp;&nbsp;&nbsp; <a href="/survexfile">All survex files</a> &nbsp;&nbsp;&nbsp;
<a href="{% if cave.kataster_number %}{% url "cave3d" cave.kataster_number %}{% else %}{% url "cave3d" cave.unofficial_number %}{% endif %}">3d file download</a>&nbsp;&nbsp;&nbsp; <a href="{% if cave.kataster_number %}{% url "cave3d" cave.kataster_number %}{% else %}{% url "cave3d" cave.unofficial_number %}{% endif %}">3d file download</a>&nbsp;&nbsp;&nbsp;
<a href="{% url "survexcavessingle" cave.kataster_number %}">This survex file</a> &nbsp;&nbsp;&nbsp; <a href="{% if cave.kataster_number %}{% url "survexcavessingle" cave.kataster_number %}">This survex file</a> &nbsp;&nbsp;&nbsp;{% endif %}">
<div id='scene'></div> <div id='scene'></div>
{% endif %} {% endif %}