import os import re from django.shortcuts import render from django.http import HttpResponse, HttpResponseRedirect, Http404 from django.urls import reverse, resolve from django.template import Context, loader import django.forms as forms from troggle.helper import login_required_if_public from troggle.flatpages.models import Redirect, EntranceRedirect from troggle.core.models_caves import Cave import troggle.core.views_caves import troggle.settings as settings def flatpage(request, path): try: r = Redirect.objects.get(originalURL = path) return HttpResponseRedirect(r.newURL) # Redirect after POST except Redirect.DoesNotExist: pass try: r = Cave.objects.get(url = path) return troggle.core.views_caves.caveSlug(request, r.slug()) except Cave.DoesNotExist: pass except: print(" ! FAILED to get only one cave per slug for: "+path) caves = Cave.objects.all().filter(url = path) for c in caves: print(path, c.slug()) if c.slug() != None: return troggle.core.views_caves.caveSlug(request, c.slug()) pass try: r = EntranceRedirect.objects.get(originalURL = path) return troggle.core.views_caves.entranceSlug(request, r.entrance.slug()) except EntranceRedirect.DoesNotExist: pass if path.startswith("noinfo") and settings.PUBLIC_SITE and not request.user.is_authenticated(): print(("flat path noinfo", path)) return HttpResponseRedirect(reverse("auth_login") + '?next=%s' % request.path) if path.endswith("/") or path == "": try: o = open(os.path.normpath(settings.EXPOWEB + path + "index.html"), "rb") path = path + "index.html" except IOError: try: o = open(os.path.normpath(settings.EXPOWEB + path + "index.htm"), "rb") path = path + "index.htm" except IOError: return render(request, 'pagenotfound.html', {'path': path}) else: try: filetobeopened = os.path.normpath(settings.EXPOWEB + path) o = open(filetobeopened, "rb") except IOError: return render(request, 'pagenotfound.html', {'path': path}) if path.endswith(".htm") or path.endswith(".html"): html = o.read() m = re.search(rb'(.*)<\s*head([^>]*)>(.*)<\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(html + "Page could not be split into header and body: parsing failed in flatpages.views.py") m = re.search(rb"