2011-06-02 19:16:16 +01:00
|
|
|
import troggle.settings as settings
|
|
|
|
from troggle.helper import login_required_if_public
|
|
|
|
from utils import render_with_context
|
|
|
|
|
|
|
|
from django.http import HttpResponse, HttpResponseRedirect, Http404
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.template import Context, loader
|
|
|
|
import django.forms as forms
|
|
|
|
from tinymce.widgets import TinyMCE
|
2011-07-11 00:13:06 +01:00
|
|
|
from troggle.flatpages.models import Redirect, EntranceRedirect
|
|
|
|
from troggle.core.models import Cave
|
|
|
|
import troggle.core.views_caves
|
2011-06-02 19:16:16 +01:00
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
|
|
|
|
def flatpage(request, path):
|
2011-07-11 00:13:06 +01:00
|
|
|
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)
|
2012-06-10 14:59:21 +01:00
|
|
|
return troggle.core.views_caves.caveSlug(request, r.slug())
|
2011-07-11 00:13:06 +01:00
|
|
|
except Cave.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
r = EntranceRedirect.objects.get(originalURL = path)
|
2012-06-10 14:59:21 +01:00
|
|
|
return troggle.core.views_caves.entranceSlug(request, r.entrance.slug())
|
2011-07-11 00:13:06 +01:00
|
|
|
except EntranceRedirect.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2011-06-02 19:16:16 +01:00
|
|
|
if path.startswith("noinfo") and settings.PUBLIC_SITE and not request.user.is_authenticated():
|
2011-08-08 09:51:47 +01:00
|
|
|
print "flat path noinfo", path
|
2011-06-02 19:16:16 +01:00
|
|
|
return HttpResponseRedirect(reverse("auth_login") + '?next=%s' % request.path)
|
2011-07-11 22:36:48 +01:00
|
|
|
|
|
|
|
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")
|
2011-08-07 17:30:18 +01:00
|
|
|
path = path + "index.htm"
|
2011-07-11 22:36:48 +01:00
|
|
|
except IOError:
|
2011-08-08 12:18:47 +01:00
|
|
|
return render_with_context(request, 'pagenotfound.html', {'path': path})
|
2011-07-11 22:36:48 +01:00
|
|
|
else:
|
|
|
|
try:
|
2012-08-04 23:26:05 +01:00
|
|
|
filetobeopened = os.path.normpath(settings.EXPOWEB + path)
|
|
|
|
o = open(filetobeopened, "rb")
|
2011-07-11 22:36:48 +01:00
|
|
|
except IOError:
|
2011-08-08 12:18:47 +01:00
|
|
|
return render_with_context(request, 'pagenotfound.html', {'path': path})
|
2011-06-02 19:16:16 +01:00
|
|
|
if path.endswith(".htm") or path.endswith(".html"):
|
|
|
|
html = o.read()
|
2011-07-11 00:13:06 +01:00
|
|
|
|
2012-08-06 11:19:48 +01:00
|
|
|
m = re.search(r"(.*)<\s*head([^>]*)>(.*)<\s*/head\s*>(.*)<\s*body([^>]*)>(.*)<\s*/body\s*>(.*)", html, re.DOTALL + re.IGNORECASE)
|
2011-06-02 19:16:16 +01:00
|
|
|
if m:
|
2012-08-06 11:19:48 +01:00
|
|
|
preheader, headerattrs, head, postheader, bodyattrs, body, postbody = m.groups()
|
2011-06-02 19:16:16 +01:00
|
|
|
else:
|
|
|
|
return HttpResponse(html + "Page could not be split into header and body")
|
2011-08-08 12:58:02 +01:00
|
|
|
m = re.search(r"<title>(.*)</title>", head, re.DOTALL + re.IGNORECASE)
|
2011-08-08 09:51:47 +01:00
|
|
|
if m:
|
|
|
|
title, = m.groups()
|
|
|
|
else:
|
|
|
|
title = ""
|
2011-08-08 12:58:02 +01:00
|
|
|
linksmatch = re.match('(.*)<ul id="links">', body, re.DOTALL + re.IGNORECASE)
|
2011-08-08 09:51:47 +01:00
|
|
|
if linksmatch:
|
|
|
|
body, = linksmatch.groups()
|
2011-06-02 19:16:16 +01:00
|
|
|
if re.search(r"iso-8859-1", html):
|
|
|
|
body = unicode(body, "iso-8859-1")
|
2011-08-08 09:51:47 +01:00
|
|
|
body.strip
|
2011-08-08 10:58:50 +01:00
|
|
|
return render_with_context(request, 'flatpage.html', {'editable': True, 'path': path, 'title': title, 'body': body, 'homepage': (path == "index.htm")})
|
2011-06-02 19:16:16 +01:00
|
|
|
else:
|
2011-07-11 22:36:48 +01:00
|
|
|
return HttpResponse(o.read(), mimetype=getmimetype(path))
|
|
|
|
|
|
|
|
def getmimetype(path):
|
2012-08-14 14:05:15 +01:00
|
|
|
if path.lower().endswith(".png"): return "image/png"
|
|
|
|
if path.lower().endswith(".tif"): return "image/tif"
|
|
|
|
if path.lower().endswith(".gif"): return "image/gif"
|
|
|
|
if path.lower().endswith(".jpeg"): return "image/jpeg"
|
|
|
|
if path.lower().endswith(".jpg"): return "image/jpeg"
|
|
|
|
if path.lower().endswith("svg"): return "image/svg+xml"
|
|
|
|
if path.lower().endswith(".pdf"): return "application/pdf"
|
|
|
|
if path.lower().endswith(".ps"): return "application/postscript"
|
|
|
|
if path.lower().endswith(".svx"): return "application/x-survex-svx"
|
|
|
|
if path.lower().endswith(".3d"): return "application/x-survex-3d"
|
|
|
|
if path.lower().endswith(".pos"): return "application/x-survex-pos"
|
|
|
|
if path.lower().endswith(".err"): return "application/x-survex-err"
|
|
|
|
if path.lower().endswith(".odt"): return "application/vnd.oasis.opendocument.text"
|
|
|
|
if path.lower().endswith(".ods"): return "application/vnd.oasis.opendocument.spreadsheet"
|
2011-07-11 22:36:48 +01:00
|
|
|
return ""
|
2011-06-02 19:16:16 +01:00
|
|
|
|
|
|
|
@login_required_if_public
|
|
|
|
def editflatpage(request, path):
|
2011-07-11 00:13:06 +01:00
|
|
|
try:
|
2011-07-11 22:36:48 +01:00
|
|
|
r = Cave.objects.get(url = path)
|
2011-07-11 00:13:06 +01:00
|
|
|
return troggle.core.views_caves.editCave(request, r.cave.slug)
|
2011-07-11 22:36:48 +01:00
|
|
|
except Cave.DoesNotExist:
|
2011-07-11 00:13:06 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2011-06-02 19:16:16 +01:00
|
|
|
try:
|
|
|
|
filepath = os.path.normpath(settings.EXPOWEB + path)
|
|
|
|
o = open(filepath, "r")
|
2011-08-08 12:18:47 +01:00
|
|
|
html = o.read()
|
2012-08-06 11:56:20 +01:00
|
|
|
autogeneratedmatch = re.search(r"\<\!--\s*(.*?(Do not edit|auto-generated).*?)\s*--\>", html, re.DOTALL + re.IGNORECASE)
|
|
|
|
if autogeneratedmatch:
|
|
|
|
return HttpResponse(autogeneratedmatch.group(1))
|
2012-08-06 11:19:48 +01:00
|
|
|
m = re.search(r"(.*)<head([^>]*)>(.*)</head>(.*)<body([^>]*)>(.*)</body>(.*)", html, re.DOTALL + re.IGNORECASE)
|
2011-08-08 12:18:47 +01:00
|
|
|
if m:
|
|
|
|
filefound = True
|
2012-08-06 11:19:48 +01:00
|
|
|
preheader, headerargs, head, postheader, bodyargs, body, postbody = m.groups()
|
|
|
|
linksmatch = re.match('(.*)(<ul\s+id="links">.*)', body, re.DOTALL + re.IGNORECASE)
|
2011-08-08 13:11:57 +01:00
|
|
|
if linksmatch:
|
2012-08-06 11:19:48 +01:00
|
|
|
body, links = linksmatch.groups()
|
2011-08-08 12:18:47 +01:00
|
|
|
if re.search(r"iso-8859-1", html):
|
|
|
|
body = unicode(body, "iso-8859-1")
|
|
|
|
else:
|
|
|
|
return HttpResponse("Page could not be split into header and body")
|
2011-06-02 19:16:16 +01:00
|
|
|
except IOError:
|
2011-08-08 12:18:47 +01:00
|
|
|
filefound = False
|
|
|
|
|
|
|
|
|
2011-06-02 19:16:16 +01:00
|
|
|
if request.method == 'POST': # If the form has been submitted...
|
|
|
|
flatpageForm = FlatPageForm(request.POST) # A form bound to the POST data
|
|
|
|
if flatpageForm.is_valid():# Form valid therefore write file
|
2012-08-06 11:19:48 +01:00
|
|
|
if filefound:
|
|
|
|
headmatch = re.match(r"(.*)<title>.*</title>(.*)", head, re.DOTALL + re.IGNORECASE)
|
|
|
|
if headmatch:
|
|
|
|
head = headmatch.group(1) + "<title>" + flatpageForm.cleaned_data["title"] + "</title>" + headmatch.group(2)
|
|
|
|
else:
|
|
|
|
head = "<title>" + flatpageForm.cleaned_data["title"] + "</title>"
|
|
|
|
else:
|
|
|
|
head = "<title>" + flatpageForm.cleaned_data["title"] + "</title>"
|
|
|
|
preheader = "<html>"
|
|
|
|
headerargs = ""
|
|
|
|
postheader = ""
|
|
|
|
bodyargs = ""
|
|
|
|
postbody = "</html>"
|
|
|
|
body = flatpageForm.cleaned_data["html"]
|
|
|
|
body = body.replace("\r", "")
|
2012-08-12 18:10:23 +01:00
|
|
|
result = u"%s<head%s>%s</head>%s<body%s>\n%s</body>%s" % (preheader, headerargs, head, postheader, bodyargs, body, postbody)
|
|
|
|
f = open(filepath, "w")
|
|
|
|
f.write(result)
|
2011-06-02 19:16:16 +01:00
|
|
|
f.close()
|
|
|
|
return HttpResponseRedirect(reverse('flatpage', args=[path])) # Redirect after POST
|
|
|
|
else:
|
2011-08-08 12:18:47 +01:00
|
|
|
if filefound:
|
2011-08-08 12:58:02 +01:00
|
|
|
m = re.search(r"<title>(.*)</title>", head, re.DOTALL + re.IGNORECASE)
|
2011-08-08 12:18:47 +01:00
|
|
|
if m:
|
|
|
|
title, = m.groups()
|
|
|
|
else:
|
|
|
|
title = ""
|
|
|
|
flatpageForm = FlatPageForm({"html": body, "title": title})
|
2011-08-08 09:51:47 +01:00
|
|
|
else:
|
2011-08-08 12:18:47 +01:00
|
|
|
flatpageForm = FlatPageForm()
|
2011-06-02 19:16:16 +01:00
|
|
|
return render_with_context(request, 'editflatpage.html', {'path': path, 'form': flatpageForm, })
|
|
|
|
|
|
|
|
class FlatPageForm(forms.Form):
|
2011-08-08 09:51:47 +01:00
|
|
|
title = forms.CharField(widget=forms.TextInput(attrs={'size':'60'}))
|
|
|
|
|
2012-08-06 11:19:48 +01:00
|
|
|
html = forms.CharField(widget=forms.Textarea())
|