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)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2011-06-02 19:16:16 +01:00
|
|
|
if path.startswith("noinfo") and settings.PUBLIC_SITE and not request.user.is_authenticated():
|
|
|
|
return HttpResponseRedirect(reverse("auth_login") + '?next=%s' % request.path)
|
|
|
|
try:
|
|
|
|
o = open(os.path.normpath(settings.EXPOWEB + path), "rb")
|
|
|
|
except IOError:
|
|
|
|
raise Http404
|
|
|
|
if path.endswith(".htm") or path.endswith(".html"):
|
|
|
|
html = o.read()
|
2011-07-11 00:13:06 +01:00
|
|
|
|
2011-06-02 19:16:16 +01:00
|
|
|
m = re.search(r"<head>(.*)</head>.*<body>(.*)</body>", html, re.DOTALL)
|
2011-07-11 00:13:06 +01:00
|
|
|
mwithid = re.search(r'<head>(.*)</head>.*<body id="([^"]*)">(.*)</body>', html, re.DOTALL)
|
2011-06-02 19:16:16 +01:00
|
|
|
if m:
|
|
|
|
head, body = m.groups()
|
2011-07-11 00:13:06 +01:00
|
|
|
bodyid = None
|
|
|
|
elif mwithid:
|
|
|
|
head, bodyid, body = mwithid.groups()
|
2011-06-02 19:16:16 +01:00
|
|
|
else:
|
|
|
|
return HttpResponse(html + "Page could not be split into header and body")
|
|
|
|
if re.search(r"iso-8859-1", html):
|
|
|
|
body = unicode(body, "iso-8859-1")
|
2011-07-11 00:13:06 +01:00
|
|
|
return render_with_context(request, 'flatpage.html', {'editable': True, 'path': path, 'head': head, 'body': body, "bodyid": bodyid})
|
2011-06-02 19:16:16 +01:00
|
|
|
else:
|
|
|
|
return HttpResponse(o.read())
|
|
|
|
|
|
|
|
@login_required_if_public
|
|
|
|
def editflatpage(request, path):
|
2011-07-11 00:13:06 +01:00
|
|
|
try:
|
|
|
|
r = CaveRedirect.objects.get(originalURL = path)
|
|
|
|
return troggle.core.views_caves.editCave(request, r.cave.slug)
|
|
|
|
except CaveRedirect.DoesNotExist:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2011-06-02 19:16:16 +01:00
|
|
|
try:
|
|
|
|
filepath = os.path.normpath(settings.EXPOWEB + path)
|
|
|
|
o = open(filepath, "r")
|
|
|
|
except IOError:
|
|
|
|
raise Http404
|
|
|
|
html = o.read()
|
|
|
|
m = re.search(r"<head>(.*)</head>.*<body>(.*)</body>", html, re.DOTALL)
|
|
|
|
if m:
|
|
|
|
head, body = m.groups()
|
2011-07-11 00:13:06 +01:00
|
|
|
if re.search(r"iso-8859-1", html):
|
|
|
|
body = unicode(body, "iso-8859-1")
|
2011-06-02 19:16:16 +01:00
|
|
|
else:
|
|
|
|
return HttpResponse("Page could not be split into header and body")
|
|
|
|
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, 'head': head})
|
|
|
|
f.write(template.render(context))
|
|
|
|
f.close()
|
|
|
|
return HttpResponseRedirect(reverse('flatpage', args=[path])) # Redirect after POST
|
|
|
|
else:
|
|
|
|
flatpageForm = FlatPageForm({"html": body})
|
|
|
|
return render_with_context(request, 'editflatpage.html', {'path': path, 'form': flatpageForm, })
|
|
|
|
|
|
|
|
class FlatPageForm(forms.Form):
|
|
|
|
html = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
|