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): #print(" - FLATPAGES delivering the file: {} as MIME type: {}".format(path,getmimetype(path))) try: r = Redirect.objects.get(originalURL = path) #print(" - FLATPAGES REDIRECT the file: {} as: {}".format(path,r)) 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 == "": #print(" - FLATPAGES the file: {} ENDSWITH ...".format(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: #print(" - FLATPAGES the file: {} ...".format(path)) if path.startswith("site_media"): #print(" - MEDIA_ROOT: {} ...".format(settings.MEDIA_ROOT)) path = path.replace("site_media", settings.MEDIA_ROOT) filetobeopened = os.path.normpath(path) elif path.startswith("static"): #print(" - STATIC_ROOT: {} ...".format(settings.MEDIA_ROOT)) path = path.replace("static", settings.MEDIA_ROOT) filetobeopened = os.path.normpath(path) else: filetobeopened = os.path.normpath(settings.EXPOWEB + path) #print(" - FLATPAGES full path : {} ...".format(filetobeopened)) o = open(filetobeopened, "rb") #print(" - FLATPAGES full path no error: {} ...".format(filetobeopened)) except IOError: #print(" - FLATPAGES ERROR: {} ...".format(filetobeopened)) 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"(.*)", head, re.DOTALL + re.IGNORECASE) if m: title, = m.groups() else: title = "" m = re.search(rb"]*)noedit", head, re.DOTALL + re.IGNORECASE) if m: editable = False else: editable = True has_menu = False menumatch = re.match(rb'(.*)