Permalink
Browse files

Working editions

  • Loading branch information...
1 parent fed8ec6 commit 74f3cf9e658131bf0d7c6078c8bfecbe67b112ed @emesik committed Dec 25, 2010
View
@@ -0,0 +1,3 @@
+#!/bin/sh
+export DJANGO_SETTINGS_MODULE='settings'
+./manage.py $*
View
@@ -1,4 +1,5 @@
from django import forms
+from django.utils.translation import ugettext as _
from . import models
class PageEditForm(forms.ModelForm):
@@ -27,3 +28,9 @@ def clean(self):
raise forms.ValidationError(_("Somebody else has modified this page in the meantime. "\
"Stash your changes somewhere else and reapply wit the latest revision."))
return self.cleaned_data
+
+ def save(self, *args, **kwargs):
+ if not self.page.pk:
+ self.page.save()
+ self.instance.page = self.page
+ super(PageEditForm, self).save(*args, **kwargs)
View
@@ -13,20 +13,27 @@ def __unicode__(self):
return self.title
def last_revision(self):
- return self.revisions.order_by('-created')[0]
+ try:
+ return self.revisions.order_by('-created')[0]
+ except IndexError:
+ return None
def last_change(self):
- return self.last_revision().created
+ last = self.last_revision()
+ if last:
+ return last.created
def last_author(self):
- return self.last_revision().author
+ last = self.last_revision()
+ if last:
+ return last.author
class PageRevision(models.Model):
page = models.ForeignKey(Page, related_name='revisions')
content = models.TextField(_("Content"), blank=True)
created = models.DateTimeField(_("Created"), auto_now_add=True)
- author = models.ForeignKey(User, label=_("Author"))
+ author = models.ForeignKey(User, verbose_name=_("Author"), null=True, blank=True)
description = models.CharField(_("Description"), max_length=400, blank=True)
class Meta:
@@ -0,0 +1,11 @@
+{% extends 'base.html' %}
+{% block page %}
+<div class="pawiki">
+ <div class="actions">
+ {% block pawiki_actions %}{% endblock %}
+ </div>
+ <div class="main">
+ {% block pawiki_main %}{% endblock %}
+ </div>
+</div>
+{% endblock %}
@@ -0,0 +1,10 @@
+{% extends 'pawiki/base.html' %}
+{% load i18n %}
+{% block title %}{{ page.title }} | {{ block.super }}{% endblock %}
+{% block pawiki_actions %}
+<ul>
+ <li><a href="{% url pawiki-page-view page.title %}">{% trans "Page" %}</a></li>
+ <li><a href="{% url pawiki-page-edit page.title %}">{% trans "Edit page" %}</a></li>
+ <li><a href="{% url pawiki-page-history page.title %}">{% trans "History" %}</a></li>
+</ul>
+{% endblock %}
@@ -0,0 +1,12 @@
+{% extends 'pawiki/base_page.html' %}
+{% load i18n %}
+{% block title %}{% trans "Edit page" %}: {{ block.super }}{% endblock %}
+{% block pawiki_main %}
+<div class="page edit">
+ <form action="" method="post">
+ {% csrf_token %}
+ {{ form }}
+ <button type="submit">{% trans "Save" %}</button>
+ </form>
+</div>
+{% endblock %}
@@ -0,0 +1,28 @@
+{% extends 'pawiki/base_page.html' %}
+{% load i18n %}
+{% block title %}{% trans "Page history" %}: {{ block.super }}{% endblock %}
+{% block pawiki_main %}
+<div class="page history">
+ <table>
+ <thead>
+ <tr>
+ <th>{% trans "Modification time" %}</th>
+ <th>{% trans "Author" %}</th>
+ <th>{% trans "Description" %}</th>
+ </tr>
+ </thead>
+ <tbody>
+ {% for revision in history %}
+ <tr>
+ <td>{{ revision.created }}</td>
+ <td>
+ {% if revision.author %}{{ revision.author }}
+ {% else %}<em>{% trans "anonymous" %}</em>{% endif %}
+ </td>
+ <td>{{ revision.description }}</td>
+ </tr>
+ {% endfor %}
+ </tbody>
+ </table>
+</div>
+{% endblock %}
@@ -0,0 +1,6 @@
+{% extends 'pawiki/base_page.html' %}
+{% block pawiki_main %}
+<div class="page content">
+{{ page.rendered_content|safe }}
+</div>
+{% endblock %}
View
@@ -1,23 +1,71 @@
+# -*- coding: utf-8 -*-
+from django.contrib.auth.models import User
+from django.core.urlresolvers import reverse
+from django.test import TestCase
+from django.test.client import Client
+from . import models
+
+content1 = u"""
+= Hello world! =
+
+This is a simple test page.
"""
-This file demonstrates two different styles of tests (one doctest and one
-unittest). These will both pass when you run "manage.py test".
+description1 = u"Initial page version"
+content2 = content1 + """
+== Subsection ==
-Replace these with more appropriate tests for your application.
+This page has a subsection.
"""
+description2 = u"Subsection added"
+content3 = """
+= Hello world! =
+
+Some text added here.
+This is a simple test page.
+
+== Subsection ==
+
+This page has a subsection.
+"""
+description3 = u"Added some text"
-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)
+ def setUp(self):
+ self.user1 = User.objects.create(username='foouser')
+ self.password1 = 'foopassword'
+ self.user1.set_password(self.password1)
+ self.user1.save()
-__test__ = {"doctest": """
-Another way to test that 1 + 1 is equal to 2.
+ def _page_edit(self, title, content, description='', username=None, password=None):
+ client = Client()
+ if username:
+ client.login(username=username, password=password)
+ rev_count = models.PageRevision.objects.filter(page__title=title).count()
+ try:
+ prev_rev = models.PageRevision.objects.filter(page__title=title).order_by('-created')[0].pk
+ except IndexError:
+ prev_rev = ''
+ r = client.get(reverse('pawiki-page-edit', kwargs={'title': title}))
+ self.assertEqual(r.status_code, 200)
+ r = client.post(reverse('pawiki-page-edit', kwargs={'title': title}),
+ {'content': content, 'description': description, 'prev_revision': prev_rev})
+ self.assertEqual(r.status_code, 302)
+ p = models.Page.objects.get(title=title)
+ self.assertEqual(p.revisions.count(), rev_count + 1)
+ self.assertEqual(p.last_revision().content, content)
+ if username:
+ self.assertEqual(p.last_revision().author.username, username)
+ else:
+ self.assertEqual(p.last_revision().author, None)
+ self.assertEqual(p.last_revision().description, description)
->>> 1 + 1 == 2
-True
-"""}
+ def test_new_page_creation(self):
+ self._page_edit(u"User Page", content1, description1, self.user1.username, self.password1)
+ self._page_edit(u"Anonymous Page", content2, description2)
+ def test_subsequent_edits(self):
+ title = u"Test page"
+ self._page_edit(title, content1, description1)
+ self._page_edit(title, content2, description2)
+ self._page_edit(title, content3, description3)
View
@@ -4,4 +4,5 @@
urlpatterns = patterns('',
url(r'(?P<title>[^/]+)$', views.view, name='pawiki-page-view'),
url(r'(?P<title>[^/]+)/edit/$', views.edit, name='pawiki-page-edit'),
+ url(r'(?P<title>[^/]+)/history/$', views.history, name='pawiki-page-history'),
)
View
@@ -1,19 +1,33 @@
+from django.core.urlresolvers import reverse
+from django.http import HttpResponseRedirect
from django.views.generic.simple import direct_to_template
from django.shortcuts import get_object_or_404
from . import models, forms
-def view(self, title):
+def view(request, title):
page = get_object_or_404(models.Page, title=title)
return direct_to_template(request, 'pawiki/view.html', {'page': page})
-def edit(self, title):
+def edit(request, title):
try:
page = models.Page.objects.get(title=title)
- except Page.DoesNotExist:
- page = Page(title=title)
- revision = models.PageRevision(page=Page, author=request.user)
- form = forms.PageEditForm(data=request.POST or None, instance=revision, page=page)
+ last_content = page.last_revision().content
+ except models.Page.DoesNotExist:
+ page = models.Page(title=title)
+ last_content = ''
+ revision = models.PageRevision(page=page,
+ author=request.user if request.user.is_authenticated() else None)
+ form = forms.PageEditForm(
+ data=request.POST or None, instance=revision, page=page,
+ initial={'content': last_content})
if request.method == 'POST':
if form.is_valid():
form.save()
+ return HttpResponseRedirect(
+ reverse('pawiki-page-view', kwargs={'title': title}))
return direct_to_template(request, 'pawiki/edit.html', {'form': form, 'page': page})
+
+def history(request, title):
+ page = get_object_or_404(models.Page, title=title)
+ history = page.revisions.order_by('-created')
+ return direct_to_template(request, 'pawiki/history.html', {'page': page, 'history': history})
View
@@ -16,7 +16,7 @@
DATABASES = {
'default': {
- 'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
+ 'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'pawiki-devel.db', # Or path to database file if using sqlite3.
'USER': '', # Not used with sqlite3.
'PASSWORD': '', # Not used with sqlite3.
@@ -86,6 +86,7 @@
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
+ os.path.join(PROJECT_ROOT, 'templates'),
)
INSTALLED_APPS = (
View
@@ -0,0 +1 @@
+oh noes
View
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+ <title>{% block title %}Pawiki{% endblock %}</title>
+</head>
+<body>
+{% block page %}{% endblock %}
+</body>
+</html>
+

0 comments on commit 74f3cf9

Please sign in to comment.