From 7808005498b06d11775e83a614657fd6cbb476b8 Mon Sep 17 00:00:00 2001 From: Philip Sargent Date: Mon, 30 Jan 2023 19:04:36 +0000 Subject: ran 'black' to reformat all the core files --- core/views/expo.py | 410 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 251 insertions(+), 159 deletions(-) (limited to 'core/views/expo.py') diff --git a/core/views/expo.py b/core/views/expo.py index 2a0a22a..ffd62a1 100644 --- a/core/views/expo.py +++ b/core/views/expo.py @@ -22,12 +22,12 @@ from troggle.core.views.editor_helpers import HTMLarea from .auth import login_required_if_public -'''Formerly a separate package called 'flatpages' written by Martin Green 2011. +"""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. -''' +""" -default_head = ''' +default_head = """ CUCC Expedition - index @@ -50,40 +50,42 @@ default_head = '''
  • -''' # this gets overwritten by templates/menu.html by django for most normal pages +""" # 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/ + """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)) + """ + return redirect(urljoin("http://expo.survex.com/expofiles/", filepath)) + def map(request): - '''Serves unadorned the expoweb/map/map.html file - ''' - fn = Path(settings.EXPOWEB, 'map', 'map.html') - return HttpResponse(content=open(fn, "r"),content_type='text/html') + """Serves unadorned the expoweb/map/map.html file""" + fn = Path(settings.EXPOWEB, "map", "map.html") + return HttpResponse(content=open(fn, "r"), content_type="text/html") + def mapfile(request, path): - '''Serves unadorned file - ''' - fn = Path(settings.EXPOWEB, 'map', path) - return HttpResponse(content=open(fn, "r"),content_type=getmimetype(fn)) + """Serves unadorned file""" + fn = Path(settings.EXPOWEB, "map", path) + 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 + """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 =="/": + """ + # print(f' - expofilessingle {filepath}') + if filepath == "" or filepath == "/": return expofilesdir(request, settings.EXPOFILES, "") - fn=urlunquote(filepath) - fn = Path(settings.EXPOFILES,filepath) + 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 + 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: @@ -91,133 +93,164 @@ def expofilessingle(request, filepath): 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. + """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}') + """ + # print(f' - expofilesdir {dirpath} settings.EXPOFILESREMOTE: {settings.EXPOFILESREMOTE}') if filepath: - urlpath = 'expofiles' / Path(filepath) + urlpath = "expofiles" / Path(filepath) else: - urlpath = Path('expofiles') + urlpath = Path("expofiles") try: for f in dirpath.iterdir(): pass except FileNotFoundError: - #print(f' - expofilesdir error {dirpath}') + # print(f' - expofilesdir error {dirpath}') return expofilesdir(request, dirpath.parent, filepath.parent) - + fileitems = [] - diritems = [] + diritems = [] for f in dirpath.iterdir(): if f.is_dir(): - diritems.append((urlpath / f.parts[-1], str(f.parts[-1]))) + 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]))) + # 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 }) + return render( + request, + "dirdisplay.html", + {"filepath": urlpath, "fileitems": fileitems, "diritems": diritems, "settings": settings}, + ) + def expowebpage(request, expowebpath, path): - '''Adds menus and serves an HTML page - ''' + """Adds menus and serves an HTML page""" 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" - 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. 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("\\'","\'") + with open(expowebpath / path, "rb") as o: + html = str(o.read()).replace("

    ", "

    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. &pound;;. ]*)>(.*)<\s*/head\s*>(.*)<\s*body([^>]*)>(.*)<\s*/body\s*>(.*)', html, re.DOTALL + re.IGNORECASE) + 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. &pound;;. ]*)>(.*)<\s*/head\s*>(.*)<\s*body([^>]*)>(.*)<\s*/body\s*>(.*)", + html, + re.DOTALL + re.IGNORECASE, + ) if m: preheader, headerattrs, head, postheader, bodyattrs, body, postbody = m.groups() else: - return HttpResponse(default_head + html + '

    HTML Parsing failure:
    Page could not be parsed into header and body:
    failure detected in expowebpage in views.expo.py

    Please edit this :expoweb: page to be in the expected full HTML format HTML Parsing failure:
    Page could not be parsed into header and body:
    failure detected in expowebpage in views.expo.py Please edit this :expoweb: page to be in the expected full HTML format (.*)", head, re.DOTALL + re.IGNORECASE) if m: - title, = m.groups() + (title,) = m.groups() else: title = "" m = re.search(r"^]*)noedit", head, re.DOTALL + re.IGNORECASE) if m: editable = False else: - editable = os.access(expowebpath / path, os.W_OK) # are file permissions writeable? - + editable = os.access(expowebpath / path, os.W_OK) # are file permissions writeable? + has_menu = False menumatch = re.match(r'(.*)