datasources: add a view template

This commit is contained in:
Frédéric Péters 2013-02-12 14:54:23 +01:00
parent 90ca4262d9
commit bd2750eff5
6 changed files with 92 additions and 0 deletions

View File

@ -8,6 +8,7 @@ from model_utils.managers import InheritanceManager
class BaseDataSource(models.Model):
title = models.CharField(max_length=50)
slug = models.SlugField()
description = models.TextField()
objects = InheritanceManager()

View File

@ -0,0 +1,43 @@
{% extends "passerelle/base.html" %}
{% load i18n %}
{% load url from future %}
{% block appbar %}
<h2>{{ title }}</h2>
{% endblock %}
{% block content %}
{% if description %}
<p>
{{ description }}
</p>
{% endif %}
<ul>
<li><abbr title="JavaScript Object Notation">JSON</abbr>: <a href="json">{{ request.build_absolute_uri }}json</a></li>
<li><abbr title="JSON with Padding">JSONP</abbr>: <a href="json?format=jsonp">{{ request.build_absolute_uri }}json?format=jsonp</a></li>
</ul>
{% if sample %}
<h3>{% trans 'Sample' %}</h3>
<pre class="sample">
{{ sample }}
</pre>
{% endif %}
<h3>{% trans 'Example' %}</h3>
<pre class="code">
$.ajax({
url: '{{ request.build_absolute_uri }}json?format=jsonp',
dataType: 'jsonp',
success: function(data) {
alert('Total results found: ' + data.data.length)
}
});
</pre>
{% endblock %}

View File

@ -1,5 +1,6 @@
from django.conf.urls import patterns, url, include
urlpatterns = patterns('passerelle.datasources.views',
url(r'(?P<datasource>\w+)/$', 'view'),
url(r'(?P<datasource>\w+)/json$', 'json'),
)

View File

@ -1,7 +1,27 @@
from django.views.generic.base import TemplateView
from jsonresponse import to_json
from models import BaseDataSource
class View(TemplateView):
template_name = 'datasources/view.html'
def get_context_data(self, datasource=None, **kwargs):
context = super(View, self).get_context_data(**kwargs)
ds = BaseDataSource.objects.get_subclass(slug=datasource)
context['title'] = ds.title
context['description'] = ds.description
from json import dumps
context['sample'] = dumps(
to_json('api').obj_to_response(self.request, ds.get_data()[:3]),
indent=4,
ensure_ascii=False,
encoding='utf-8')
return context
view = View.as_view()
@to_json('api')
def json(request, datasource):
ds = BaseDataSource.objects.get_subclass(slug=datasource)

View File

@ -104,6 +104,18 @@ TEMPLATE_DIRS = (
os.path.join(PROJECT_PATH, 'passerelle', 'templates'),
)
TEMPLATE_CONTEXT_PROCESSORS = (
'django.contrib.auth.context_processors.auth',
'django.core.context_processors.debug',
'django.core.context_processors.i18n',
'django.core.context_processors.media',
'django.core.context_processors.static',
'django.core.context_processors.tz',
'django.core.context_processors.request',
'django.contrib.messages.context_processors.messages',
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',

View File

@ -144,6 +144,10 @@ div#content h2 {
background: url(../images/a22.gif) bottom repeat-x;
}
div#content ul {
line-height: 1.5em;
}
#footer
{
font-size: 70%;
@ -281,6 +285,17 @@ div.content {
margin-top: -10px;
}
abbr {
border-bottom: 1px dotted #8c8c73;
cursor: help;
}
pre.code, pre.sample {
border: 1px solid #babdb6;
padding: 0.5em 1em;
background: #fefefe;
}
/* makes the font 33% larger relative to the icon container */
.icon-large:before {
vertical-align: top;