import os
import re
from pathlib import Path
from sys import getfilesystemencoding as sys_getfilesystemencoding
from urllib.parse import unquote as urlunquote
from urllib.parse import urljoin
import django.forms as forms
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import redirect, render
from django.urls import reverse
from django.views.decorators.csrf import ensure_csrf_cookie
import troggle.core.views.caves
import troggle.settings as settings
from troggle.core.models.caves import Cave
from troggle.core.utils import WriteAndCommitError, write_and_commit, current_expo
from troggle.core.views.editor_helpers import HTMLarea
from troggle.core.views.uploads import edittxtpage
from .auth import login_required_if_public
"""Formerly a separate package called 'flatpages' written by Martin Green 2011.
This was NOT django.contrib.flatpages which stores HTML in the database, so the name was changed to expopages.
Then it was incorporated into troggle directly, rather than being an unnecessary external package.
This is a succession of hacks and needs to be redisgned and refactored.
"""
default_head = """
""" # this gets overwritten by templates/menu.html by django for most normal pages
def expofiles_redirect(request, filepath):
"""This is used only when running as a test system without a local copy of /expofiles/
when settings.EXPOFILESREMOTE is True
"""
return redirect(urljoin("http://expo.survex.com/expofiles/", filepath))
def spider(request, _):
# urls ending in "_edit_edit"
return render(request, "pagenotfound.html", {"path": path}, status=404)
# return redirect("/?#") # so that suffixes applied by spider are no longer part of the url
def map(request):
"""Serves unadorned the expoweb/map/slippy/map.html file"""
fn = Path(settings.EXPOWEB, "map", "slippy", "map.html")
return HttpResponse(content=open(fn, "r"), content_type="text/html")
def mapfile(request, path):
"""Serves unadorned file: everything in the /map/... folder tree"""
fn = Path(settings.EXPOWEB, "map", path)
print(f"MAP cuttout. \n{path=}\n{fn=} mime:{getmimetype(fn)}")
return HttpResponse(content=open(fn, "r"), content_type=getmimetype(fn))
def expofilessingle(request, filepath):
"""sends a single binary file to the user, if not found, show the parent directory
If the path actually is a directory, then show that.
"""
# print(f' - expofilessingle {filepath}')
if filepath == "" or filepath == "/":
return expofilesdir(request, settings.EXPOFILES, "")
fn = urlunquote(filepath)
fn = Path(settings.EXPOFILES, filepath)
if fn.is_dir():
return expofilesdir(request, Path(fn), Path(filepath))
if fn.is_file():
return HttpResponse(content=open(fn, "rb"), content_type=getmimetype(filepath)) # any file
else:
# not a file, so show parent directory - DANGER need to check this is limited to below expofiles
if Path(fn).parent == Path(settings.EXPOFILES).parent:
return expofilesdir(request, Path(settings.EXPOFILES), Path(filepath).parent)
else:
return expofilesdir(request, Path(fn).parent, Path(filepath).parent)
def expofilesdir(request, dirpath, filepath):
"""does a directory display. If there is an index.html file we should display that.
- dirpath is a full Path() resolved including local machine /expofiles/
- filepath is a Path() and it does not have /expofiles/ in it
"""
# print(f' - expofilesdir {dirpath} settings.EXPOFILESREMOTE: {settings.EXPOFILESREMOTE}')
if filepath:
urlpath = "expofiles" / Path(filepath)
else:
urlpath = Path("expofiles")
try:
for f in dirpath.iterdir():
pass
except FileNotFoundError:
# print(f' - expofilesdir error {dirpath}')
return expofilesdir(request, dirpath.parent, filepath.parent)
fileitems = []
diritems = []
for f in dirpath.iterdir():
if f.is_dir():
diritems.append((urlpath / f.parts[-1], str(f.parts[-1])))
else:
# if f.parts[-1].lower() == 'index.htm' or f.parts[-1].lower() == 'index.html': # css cwd problem
# return HttpResponse(content=open(f, "rb"),content_type=getmimetype(filepath)) # any file
# return expofilessingle(request, str(Path(filepath / f.parts[-1])))
fileitems.append((Path(urlpath) / f.parts[-1], str(f.parts[-1]), getmimetype(f)))
return render(
request,
"dirdisplay.html",
{"filepath": urlpath, "fileitems": fileitems, "diritems": diritems, "settings": settings},
)
def pubspage(request):
"""This is a single-page function, specifically to cope with the updated "current year"
which is hard-coded into the HTML of the page.
This is a placeholder, we need to do something like what is done in indexpage() below...
"""
expowebpath = Path(settings.EXPOWEB)
print("--redirecting from pubspage()")
return expowebpage(request, expowebpath, "pubs.htm")
def indexpage(request):
"""This is a single-page function, specifically to cope with the updated "current year"
which is hard-coded into the HTML of the page.
This edits the most recent year on the fly..
This now works.
"""
print("--redirecting from indexpage()")
subpath = Path(settings.EXPOWEB) / "index00.htm"
with open(subpath, "r") as o:
html = o.read()
html = html.replace('2xxx', f"{current_expo()}")
content_type = "text/html"
return HttpResponse(html, content_type=content_type)
def expowebpage(request, expowebpath, path):
"""Adds menus and serves an HTML page"""
if path == "" or path == "index.htm":
return indexpage(request)
if not os.path.isfile(expowebpath / path):
# Should not get here if the path has suffix "_edit"
print(f" - 404 error in expowebpage() {path}")
return render(request, "pagenotfound.html", {"path": path}, status=404)
# print(f' - {sys_getfilesystemencoding()=}')
if sys_getfilesystemencoding() != "utf-8":
return HttpResponse(
default_head
+ "
UTF-8 Parsing Failure: Default file encoding on this Troggle installation is not UTF-8: failure detected in expowebpage in views.expo.py
Please Please reconfigure Debian/Apache/Django to fix this, i.e. contact Wookey. ", "
BAD NON-UTF-8 characters here - ")
html = html.replace("\\n", "\n")
html = html.replace("\\r", "")
html = html.replace("\\t", "\t")
html = html.replace("\\'", "'")
except:
return HttpResponse(
default_head
+ "
UTF-8 Parsing Failure: Page could not be parsed using UTF-8: failure detected in expowebpage in views.expo.py
Please edit this :expoweb: page to replace dubious umlauts and £ symbols with correct HTML entities e.g. £;.