diff options
author | Martin Green <martin.speleo@gmail.com> | 2011-06-02 19:16:16 +0100 |
---|---|---|
committer | Martin Green <martin.speleo@gmail.com> | 2011-06-02 19:16:16 +0100 |
commit | 50545af223eaee708cc465363247201123707bd3 (patch) | |
tree | 31dc3fd8ab20dba26e0276e7663c9597f1e50c4a /flatpages | |
parent | ede9e4a9bdfe3cfc49f53afa7347850d31f0014d (diff) | |
download | troggle-50545af223eaee708cc465363247201123707bd3.tar.gz troggle-50545af223eaee708cc465363247201123707bd3.tar.bz2 troggle-50545af223eaee708cc465363247201123707bd3.zip |
Added editing of flat pages. Added slugfields to models to refer to them.
Diffstat (limited to 'flatpages')
-rw-r--r-- | flatpages/__init__.py | 0 | ||||
-rw-r--r-- | flatpages/models.py | 3 | ||||
-rw-r--r-- | flatpages/tests.py | 23 | ||||
-rw-r--r-- | flatpages/views.py | 62 |
4 files changed, 88 insertions, 0 deletions
diff --git a/flatpages/__init__.py b/flatpages/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/flatpages/__init__.py diff --git a/flatpages/models.py b/flatpages/models.py new file mode 100644 index 0000000..71a8362 --- /dev/null +++ b/flatpages/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/flatpages/tests.py b/flatpages/tests.py new file mode 100644 index 0000000..2247054 --- /dev/null +++ b/flatpages/tests.py @@ -0,0 +1,23 @@ +""" +This file demonstrates two different styles of tests (one doctest and one +unittest). These will both pass when you run "manage.py test". + +Replace these with more appropriate tests for your application. +""" + +from django.test import TestCase + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.failUnlessEqual(1 + 1, 2) + +__test__ = {"doctest": """ +Another way to test that 1 + 1 is equal to 2. + +>>> 1 + 1 == 2 +True +"""} + diff --git a/flatpages/views.py b/flatpages/views.py new file mode 100644 index 0000000..9c7483e --- /dev/null +++ b/flatpages/views.py @@ -0,0 +1,62 @@ +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 + +import os +import re + +def flatpage(request, path): + print path + 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() + m = re.search(r"<head>(.*)</head>.*<body>(.*)</body>", html, re.DOTALL) + if m: + head, body = m.groups() + 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") + return render_with_context(request, 'flatpage.html', {'editable': True, 'path': path, 'head': head, 'body': body}) + else: + return HttpResponse(o.read()) + +@login_required_if_public +def editflatpage(request, path): + 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() + 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})) |