Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
bug 730707: Send doc metadata to kumascript
Browse files Browse the repository at this point in the history
* Kumascript now accepts env vars as headers with base64/utf8/JSON
  encoded data structures

* Assemble some env vars from document metadata to send in the request
  to kumascript
  • Loading branch information
lmorchard committed Apr 18, 2012
1 parent d37b4c6 commit 395efcc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
37 changes: 37 additions & 0 deletions apps/wiki/tests/test_views.py
@@ -1,6 +1,7 @@
import logging
import json
import base64
import time

from django.conf import settings
from django.contrib.sites.models import Site
Expand Down Expand Up @@ -229,6 +230,7 @@ def setUp(self):
super(KumascriptIntegrationTests, self).setUp()

self.d, self.r = doc_rev()
self.d.tags.set('foo', 'bar', 'baz')
self.url = reverse('wiki.document',
args=['%s/%s' % (self.d.locale, self.d.slug)],
locale=settings.WIKI_DEFAULT_LANGUAGE)
Expand Down Expand Up @@ -459,6 +461,41 @@ def my_requests_get(url, headers=None, timeout=None):
for error in expected_errors['logs']:
ok_(error['message'] in response.content)

@mock.patch('requests.get')
def test_env_vars(self, mock_requests_get):
"""Kumascript reports errors in HTTP headers, Kuma should display them"""

# Now, trap the request from the view.
trap = {}
def my_requests_get(url, headers=None, timeout=None):
trap['headers'] = headers
return FakeResponse(
status_code=200,
body='HELLO WORLD',
headers={}
)
mock_requests_get.side_effect = my_requests_get

# Ensure kumascript is enabled
constance.config.KUMASCRIPT_TIMEOUT = 1.0
constance.config.KUMASCRIPT_MAX_AGE = 600

# Fire off the request, and capture the env vars that would have been
# sent to kumascript
response = self.client.get(self.url)
pfx = 'x-kumascript-env-'
vars = dict(
(k[len(pfx):], json.loads(base64.b64decode(v)))
for k,v in trap['headers'].items()
if k.startswith(pfx))

# Ensure the env vars intended for kumascript match expected values.
for n in ('title', 'slug', 'locale'):
eq_(getattr(self.d, n), vars[n])
eq_(self.d.get_absolute_url(), vars['path'])
eq_(time.mktime(self.d.modified.timetuple()), vars['modified'])
eq_(sorted([u'foo', u'bar', u'baz']), sorted(vars['tags']))


class DocumentEditingTests(TestCaseBase):
"""Tests for the document-editing view"""
Expand Down
27 changes: 24 additions & 3 deletions apps/wiki/views.py
@@ -1,4 +1,5 @@
from datetime import datetime
import time
import json
from collections import defaultdict
import base64
Expand Down Expand Up @@ -241,7 +242,7 @@ def set_common_headers(r):
# * The request *has* asked for macro evaluation
# (eg. ?raw&macros)
resp_body, resp_errors = _perform_kumascript_request(
request, response_headers, document_locale, document_slug)
request, response_headers, doc, document_locale, document_slug)
if resp_body:
doc_html = resp_body
if resp_errors:
Expand Down Expand Up @@ -314,8 +315,8 @@ def _invalidate_kumascript_cache(document):
document.locale))


def _perform_kumascript_request(request, response_headers, document_locale,
document_slug):
def _perform_kumascript_request(request, response_headers, document,
document_locale, document_slug):
"""Perform a kumascript GET request for a document locale and slug.
This is broken out into its own utility function, both to make the view
Expand Down Expand Up @@ -359,6 +360,26 @@ def _perform_kumascript_request(request, response_headers, document_locale,
'Cache-Control': cache_control
}

# Assemble some KumaScript env vars
# TODO: See dekiscript vars for future inspiration
# http://developer.mindtouch.com/en/docs/DekiScript/Reference/Wiki_Functions_and_Variables
path = document.get_absolute_url()
env_vars = dict(
path=path,
url=request.build_absolute_uri(path),
id=document.pk,
locale=document.locale,
title=document.title,
slug=document.slug,
tags=[x.name for x in document.tags.all()],
modified=time.mktime(document.modified.timetuple()),
)
# Encode the vars as kumascript headers, as base64 JSON-encoded values.
headers.update(dict(
('x-kumascript-env-%s' % k,
base64.b64encode(json.dumps(v)))
for k, v in env_vars.items()))

# Set up for conditional GET, if we have the details cached.
c_meta = cache.get_many([ck_etag, ck_modified])
if ck_etag in c_meta:
Expand Down
2 changes: 1 addition & 1 deletion kumascript
Submodule kumascript updated from aa1078 to 4f90f7

0 comments on commit 395efcc

Please sign in to comment.