Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#74 #106

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

#74 #106

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ At a glance, Waliki has these features:
* A very simple *per slug* `ACL system <http://waliki.readthedocs.org/en/latest/acl.html>`_
* A nice `attachments manager <http://waliki.readthedocs.org/en/latest/attachments.html>`_ (that respects the permissions over the page)
* Realtime `collaborative edition <http://waliki.readthedocs.org/en/latest/togetherjs.html>`_ via togetherJS
* REST `API <http://waliki.readthedocs.org/en/latest/rest.html>`_
* Wiki content embeddable in any django template (as a "`dummy CMS <http://waliki.readthedocs.org/en/latest/boxes.html>`_")
* Few helpers to migrate content (particularly from MoinMoin, using moin2git_)
* It `works <https://travis-ci.org/mgaitan/waliki>`_ with Python 2.7, 3.3, 3.4 or PyPy in Django 1.6 or newer
Expand Down Expand Up @@ -74,6 +75,7 @@ Add ``waliki`` and the optionals plugins to your INSTALLED_APPS::
'waliki.pdf', # optional
'waliki.slides', # optional
'waliki.togetherjs', # optional
'waliki.rest', # optional
...
)

Expand Down
64 changes: 64 additions & 0 deletions docs/rest.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
.. _rest:

=========
REST API
=========
The ``waliki.rest`` plugin together ``waliki.git`` provides a set of REST API endpoints.

With this plugin you'll get:

URLs
----

| List all Pages
| ``GET http://yoursite.com[/<waliki_prefix>]/<WALIKI_API_ROOT>/all``
|
| Add Page
| ``POST http://yoursite.com[/<waliki_prefix>]/<WALIKI_API_ROOT>/new``
|
| Retrieve Page
| ``GET http://yoursite.com[/<waliki_prefix>]/<WALIKI_API_ROOT>/<slug>``
|
| Edit Page
| ``POST http://yoursite.com[/<waliki_prefix>]/<WALIKI_API_ROOT>/<slug>/edit``
|
| Move Page
| ``POST http://yoursite.com[/<waliki_prefix>]/<WALIKI_API_ROOT>/<slug>/move``
|
| Delete Page
| ``POST http://yoursite.com[/<waliki_prefix>]/<WALIKI_API_ROOT>/<slug>/delete``
|
| History of changes
| ``GET http://yoursite.com[/<waliki_prefix>]/<WALIKI_API_ROOT>/<slug>/history``
|
| Retrieve a version
| ``GET http://yoursite.com[/<waliki_prefix>]/<WALIKI_API_ROOT>/<slug>/version/<version>/``
|
| Diff
| ``GET http://yoursite.com[/<waliki_prefix>]/<WALIKI_API_ROOT>/<slug>/diff/<new_version>..<old_version>``

Setup
-------

It requires `djangorestframework`_ as requirement. Install it via pip::

$ pip install djangorestframework

To install it, add ``'waliki.rest'`` and ``'rest_framework'`` after ``'waliki.git'`` in your ``settings.INSTALLED_APPS``::

INSTALLED_APPS = (
...
'waliki',
...
'waliki.git',
'waliki.rest',
...
'rest_framework',
...
)

| Default url for restful service:

``WALIKI_API_ROOT = 'API'``

.. _djangorestframework: https://github.com/tomchristie/django-rest-framework
8 changes: 4 additions & 4 deletions tests/test_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def test_commit_existent_page_with_no_previous_commits(self):
data["message"] = "testing :)"
response = self.client.post(self.edit_url, data)
self.assertEqual(self.page.raw, "test content")
self.assertEqual(Git().version(self.page, 'HEAD'), "test content")
self.assertEqual(Git().version(self.page, 'HEAD')["raw"], "test content")
self.assertIn("testing :)", git.log('-s', '--format=%s', self.page.path))

def test_commit_existent_page_with_previous_commits(self):
Expand All @@ -44,7 +44,7 @@ def test_commit_existent_page_with_previous_commits(self):
data["raw"] = "test content"
response = self.client.post(self.edit_url, data)
self.assertEqual(self.page.raw, "test content")
self.assertEqual(Git().version(self.page, 'HEAD'), "test content")
self.assertEqual(Git().version(self.page, 'HEAD')["raw"], "test content")

def test_commit_new_page(self):
assert not Page.objects.filter(slug='test').exists()
Expand All @@ -60,7 +60,7 @@ def test_commit_new_page(self):
page = Page.objects.get(slug='test')
self.assertEqual(page.title, "Test Page")
self.assertEqual(page.raw, "hey there\n")
self.assertEqual(Git().version(page, 'HEAD'), "hey there\n")
self.assertEqual(Git().version(page, 'HEAD')["raw"], "hey there\n")

def test_concurrent_edition_with_no_conflict(self):
self.page.raw = "\n- item1\n"
Expand Down Expand Up @@ -170,7 +170,7 @@ def test_unicode_content(self):
data['message'] = 'testing :)'
response = self.client.post(self.edit_url, data)
self.assertRedirects(response, reverse('waliki_detail', args=(self.page.slug,)))
self.assertEqual(Git().version(self.page, 'HEAD'), u'Ω')
self.assertEqual(Git().version(self.page, 'HEAD')["raw"], u'Ω')

def test_commit_page_with_no_changes(self):
self.page.raw = 'lala'
Expand Down
22 changes: 21 additions & 1 deletion waliki/git/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import re
import json
from io import StringIO
from django.contrib.auth.models import User
from django.utils.translation import ugettext_lazy as _
from django.utils import six
Expand Down Expand Up @@ -98,7 +99,26 @@ def history(self, page):

def version(self, page, version):
try:
return git.show('%s:%s' % (version, page.path)).stdout.decode('utf8')
out = str(git.show('-s', "--pretty=format:%aN%n%aD%n%B%n%h%e", version))

buf = StringIO()
buf.write(out)
buf.seek(0)
author = str(buf.readline())
date = str(buf.readline())
message = str(buf.readline())
buf.close()

raw = str(git.show('%s:%s' % (version, page.path)))

data = {
"author": author,
"date": date,
"message": message,
"raw": raw,
}

return data
except:
return ''

Expand Down
14 changes: 8 additions & 6 deletions waliki/git/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ def history(request, slug, pag=1):
def version(request, slug, version, raw=False):
page = get_object_or_404(Page, slug=slug)
content = Git().version(page, version)
if not content:
raise Http404

form = PageForm(instance=page, initial={'message': _('Restored version @%s') % version, 'raw': content},
is_hidden=True)

if raw:
return HttpResponse(content, content_type='text/plain; charset=utf-8')
return HttpResponse(json.dumps(content), content_type='application/json')

content = Page.preview(page.markup, content)
if content["raw"]:
content = Page.preview(page.markup, content["raw"])
else:
content = ''
return render(request, 'waliki/version.html', {'page': page,
'content': content,
'slug': slug,
Expand All @@ -69,8 +71,8 @@ def diff(request, slug, old, new, raw=False):
return HttpResponse(content, content_type='text/plain; charset=utf-8')
space = smart_text(b'\xc2\xa0', encoding='utf-8') # non-breaking space character
tab = space * 4
old_content = Git().version(page, old).replace('\t', tab).replace(' ', space)
new_content = Git().version(page, new).replace('\t', tab).replace(' ', space)
old_content = Git().version(page, old)["raw"].replace('\t', tab).replace(' ', space)
new_content = Git().version(page, new)["raw"].replace('\t', tab).replace(' ', space)
return render(request, 'waliki/diff.html', {'page': page,
'old_content': old_content,
'new_content': new_content,
Expand Down