Skip to content

Commit

Permalink
Adding data.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe Álvarez committed May 6, 2015
1 parent 373b720 commit 972b81e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
16 changes: 15 additions & 1 deletion ddah_web/tests/ddah_instances_web_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
from promises.models import Promise
from django.core.urlresolvers import reverse
import markdown
from ddah_web.views import DDAHInstanceWebView
from ddah_web.views import DDAHInstanceWebView, DDAHInstanceWebJSONView
from ddah_web.models import DDAHInstanceWeb
from instances.models import Instance
import json


class DDAHInstanceWebTestCase(TestCase):
Expand Down Expand Up @@ -97,3 +99,15 @@ def test_get_the_thing(self):
self.assertTrue(content)
self.assertIn(self.instance.label, response.content)

def test_get_the_data_as_json(self):
url = reverse('data_json')
request = self.factory.get(url)
request.instance = self.instance
instance_web = DDAHInstanceWeb.objects.get(id=self.instance.id)
response = DDAHInstanceWebJSONView.as_view()(request)
self.assertEquals(response.status_code, 200)
self.assertEquals(response['Content-Type'], 'application/json')
self.assertEquals(response.content, instance_web.to_json())
the_data = json.loads(response.content)
self.assertTrue(the_data)

5 changes: 4 additions & 1 deletion ddah_web/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
from django.conf.urls import patterns, url
from ddah_web.views import DDAHInstanceWebView
from ddah_web.views import DDAHInstanceWebView, DDAHInstanceWebJSONView

urlpatterns = patterns('',
url(r'^data.json$',
DDAHInstanceWebJSONView.as_view(),
name='data_json'),
url(r'^$',
DDAHInstanceWebView.as_view(),
name='instance_home'),
Expand Down
17 changes: 17 additions & 0 deletions ddah_web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from ddah_web.models import DDAHInstanceWeb
from django.views.generic.detail import DetailView
import pystache
from django.http import HttpResponse
import json


class MoustacheTemplateResponse(TemplateResponse):
Expand All @@ -23,3 +25,18 @@ def get_object(self):

def get_slug_field(self):
return 'label'

class DDAHInstanceWebJSONView(DetailView):
model = DDAHInstanceWeb
context_object_name = 'instance'

def get_object(self):
return self.model.objects.get(id=self.request.instance.id)

def get_slug_field(self):
return 'label'

def render_to_response(self, context, **response_kwargs):
response_data = self.object.to_json()
return HttpResponse(response_data, content_type="application/json")

0 comments on commit 972b81e

Please sign in to comment.