mirror of
https://expo.survex.com/repositories/troggle/.git
synced 2024-11-26 01:01:53 +00:00
149 lines
5.8 KiB
Python
149 lines
5.8 KiB
Python
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
|
|
from troggle.flatpages.models import Redirect, EntranceRedirect
|
|
from troggle.core.models import Cave
|
|
import troggle.core.views_caves
|
|
|
|
import os
|
|
import re
|
|
|
|
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
|
|
|
|
try:
|
|
r = EntranceRedirect.objects.get(originalURL = path)
|
|
return troggle.core.views_caves.enranceSlug(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_with_context(request, 'pagenotfound.html', {'path': path})
|
|
else:
|
|
try:
|
|
o = open(os.path.normpath(settings.EXPOWEB + path), "rb")
|
|
except IOError:
|
|
return render_with_context(request, 'pagenotfound.html', {'path': path})
|
|
if path.endswith(".htm") or path.endswith(".html"):
|
|
html = o.read()
|
|
|
|
m = re.search(r"<head>(.*)</head>.*<body[^>]*>(.*)</body>", html, re.DOTALL + re.IGNORECASE)
|
|
if m:
|
|
head, body = m.groups()
|
|
else:
|
|
return HttpResponse(html + "Page could not be split into header and body")
|
|
m = re.search(r"<title>(.*)</title>", head, re.DOTALL + re.IGNORECASE)
|
|
if m:
|
|
title, = m.groups()
|
|
else:
|
|
title = ""
|
|
linksmatch = re.match('(.*)<ul id="links">', body, re.DOTALL + re.IGNORECASE)
|
|
if linksmatch:
|
|
body, = linksmatch.groups()
|
|
if re.search(r"iso-8859-1", html):
|
|
body = unicode(body, "iso-8859-1")
|
|
body.strip
|
|
return render_with_context(request, 'flatpage.html', {'editable': True, 'path': path, 'title': title, 'body': body, 'homepage': (path == "index.htm")})
|
|
else:
|
|
return HttpResponse(o.read(), mimetype=getmimetype(path))
|
|
|
|
def getmimetype(path):
|
|
if path.endswith(".png"): return "image/png"
|
|
if path.endswith(".tif"): return "image/tif"
|
|
if path.endswith(".gif"): return "image/gif"
|
|
if path.endswith(".jpeg"): return "image/jpeg"
|
|
if path.endswith(".jpg"): return "image/jpeg"
|
|
if path.endswith("svg"): return "image/svg+xml"
|
|
if path.endswith(".pdf"): return "application/pdf"
|
|
if path.endswith(".ps"): return "application/postscript"
|
|
if path.endswith(".svx"): return "application/x-survex-svx"
|
|
if path.endswith(".3d"): return "application/x-survex-3d"
|
|
if path.endswith(".pos"): return "application/x-survex-pos"
|
|
if path.endswith(".err"): return "application/x-survex-err"
|
|
if path.endswith(".odt"): return "application/vnd.oasis.opendocument.text"
|
|
if path.endswith(".ods"): return "application/vnd.oasis.opendocument.spreadsheet"
|
|
return ""
|
|
|
|
@login_required_if_public
|
|
def editflatpage(request, path):
|
|
try:
|
|
r = Cave.objects.get(url = path)
|
|
return troggle.core.views_caves.editCave(request, r.cave.slug)
|
|
except Cave.DoesNotExist:
|
|
pass
|
|
|
|
|
|
try:
|
|
filepath = os.path.normpath(settings.EXPOWEB + path)
|
|
o = open(filepath, "r")
|
|
html = o.read()
|
|
m = re.search(r"<head>(.*)</head>.*<body[^>]*>(.*)</body>", html, re.DOTALL + re.IGNORECASE)
|
|
if m:
|
|
filefound = True
|
|
head, body = m.groups()
|
|
linksmatch = re.match('(.*)<ul\s+id="links">', body, re.DOTALL + re.IGNORECASE)
|
|
if linksmatch:
|
|
body, = linksmatch.groups()
|
|
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")
|
|
except IOError:
|
|
filefound = False
|
|
|
|
|
|
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
|
|
f = open(filepath, "w")
|
|
template = loader.get_template('dataformat/flatfile.html')
|
|
context = Context({'form': flatpageForm.cleaned_data})
|
|
f.write(template.render(context))
|
|
f.close()
|
|
return HttpResponseRedirect(reverse('flatpage', args=[path])) # Redirect after POST
|
|
else:
|
|
if filefound:
|
|
m = re.search(r"<title>(.*)</title>", head, re.DOTALL + re.IGNORECASE)
|
|
if m:
|
|
title, = m.groups()
|
|
else:
|
|
title = ""
|
|
flatpageForm = FlatPageForm({"html": body, "title": title})
|
|
else:
|
|
flatpageForm = FlatPageForm()
|
|
return render_with_context(request, 'editflatpage.html', {'path': path, 'form': flatpageForm, })
|
|
|
|
class FlatPageForm(forms.Form):
|
|
title = forms.CharField(widget=forms.TextInput(attrs={'size':'60'}))
|
|
|
|
html = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 20}))
|