Commits

Peter Nixon committed fbdbff0

Add page editing capability

Comments (0)

Files changed (5)

+# -*- coding: utf-8 -*-
+from django import forms
+
+class WikiEditForm(forms.Form):
+    wikitext = forms.CharField(widget=forms.Textarea)

hgwiki/templates/hgwiki/page.html

 {% extends "base.html" %}
 
 {% block content %}
+<a href="{{ edit_url }}">edit</a>
+<hr />
+
 {{ page }}
 {% endblock %}
 

hgwiki/templates/hgwiki/page_edit.html

+{% extends "base.html" %}
+
+{% block content %}
+
+    {% if form.errors %}
+        <p style="color: red;">
+            Please correct the error{{ form.errors|pluralize }} below.
+        </p>
+    {% endif %}
+
+<div class="field">
+    <form action="." method="post">
+	{{ form.wikitext }}
+        <input type="submit" value="Save">
+    </form>
+</div>
+
+
+{% endblock %}
+
 urlpatterns = patterns('hgwiki.views',
     url(r'^$', 'wiki_index', name="hgwiki_index"),
     url(r'^(?P<slug>[-\w]+\.rst)$', 'wiki_page', name="hgwiki_page"),
+    url(r'^(?P<slug>[-\w]+\.rst)/edit/$', 'wiki_page_edit', name="hgwiki_page_edit"),
 )
 # -*- coding: utf-8 -*-
-from django.http import Http404, HttpResponse
+from django.http import Http404, HttpResponse, HttpResponseRedirect
 from django.shortcuts import render_to_response
 from django.template import RequestContext
 from django.utils.safestring import mark_safe
 
 from django.conf import settings
 
+from forms import WikiEditForm
+
 from docutils.core import publish_parts
 import os
 
     SAFE_DOCUTILS = dict(file_insertion_enabled=False, raw_enabled=False)
     content = publish_parts(page.read(), writer_name='html', settings_overrides=SAFE_DOCUTILS)['html_body']
 
+    edit_url = request.path.rstrip('/') + '/edit/'
+
     return render_to_response("hgwiki/page.html", {
-        "page": mark_safe(content)
+        "page": mark_safe(content),
+        "edit_url": edit_url
     }, context_instance=RequestContext(request))
 
+
+def wiki_page_edit(request, slug):
+    file = os.path.join(settings.HGWIKI_DIRECTORY_PATH, slug)
+    if os.path.isfile(file) == False:
+      raise Http404
+
+    if request.method == 'POST':
+        form = WikiEditForm(request.POST)
+        if form.is_valid():
+            wikitext = form.cleaned_data['wikitext']
+
+            f = open(file, 'w')
+            f.write(wikitext)
+            f.close()
+            # TODO: Hook to "hg ci" here
+            return HttpResponseRedirect(os.path.dirname(request.path.rstrip('/')))
+    else:
+        page = open(file, 'r')
+        wikitext = page.read()
+
+        form = WikiEditForm(
+            initial={'wikitext': wikitext}
+        )
+
+    return render_to_response('hgwiki/page_edit.html', {
+        'form': form
+    }, context_instance=RequestContext(request))