Views
=====

To demonstrate the views we need to first set up the base test system.

  >>> from django.core.urlresolvers import reverse
  >>> from django.test import Client

kss.js
------

KSS is a pluggable framework. That means Javascript is distributed
with Python packages. It also means that each plugin can provide its
own additional Javascript. To make it possible to present all this
code as a single file to the browser kss.django has a view which
concatinates everything.

  >>> c = Client()
  >>> response = c.get(reverse('kss.js'))
  >>> response.status_code
  200
  >>> print response.content
  kukit=new function(){...

This is of course send as Javascript.

  >>> response['Content-Type']
  'text/javascript'

It also sets the cache headers so that it does not do more work than
necessary (this also means you can cache it inside Apache, Varnish,
Squid etc.).

  >>> [header for header, value in sorted(response.items())]
  ['Cache-Control', 'Content-Type', 'ETag', 'Expires', 'Last-Modified', 'Vary']

By default the view also caches the concatinated Javascript in a
Django cache.

  >>> from django.conf import settings
  >>> settings.CACHE_BACKEND = 'simple:///'

If the views is executed again the Javascript should show up in the
cache.
  
  >>> from django.core.cache import cache
  >>> response = c.get(reverse('kss.js'))
  >>> cache.get('kss.js') == response.content
  True

When debugging mode is enabled the caching is disabled as well. This
is usefull for when you develop your own KSS plugins.

  >>> settings.KSS_DEBUG = True

The next example shows this by first removing the previous entry from
the cache.

  >>> cache.set('kss.js', None)
  >>> response = c.get(reverse('kss.js'))
  >>> cache.get('kss.js') == response.content
  False


Extra Javascripts
-----------------

The KSS framework and plugins (may) require extra third party
Javascripts. Plugins are distributed as Python packages. They (should)
contain the third party Javascripts they require.

By default Django requires you to set up a directory with files which
will be served by a dedicated webserver like Apache. While this is
possible the kss.django package also provides a view which can be
handy for development purposes.

This view can load any registered script from the filesystem. It will
not load random files but only those Javascript files setup either in
the core system or in a plugin.

Let's look at an example of this view.

  >>> response = c.get(reverse('kss_extra_scripts', args=['base2-dom-fp.js']))
  >>> response.status_code
  200

  >>> print response.content
  eval(function(p,a,c,k,e,r)...

Loading a script name which is not registered triggers a 404.

  >>> response = c.get(reverse('kss_extra_scripts', args=['cookies.js']))
  >>> response.status_code
  404

The headers for each script are set for optimal caching.

  >>> response = c.get(reverse('kss_extra_scripts', args=['base2-dom-fp.js']))
  >>> [header for header, value in sorted(response.items())]
  ['Cache-Control', 'Content-Type', 'ETag', 'Expires', 'Last-Modified', 'Vary']
  
