Skip to content

Commit

Permalink
Forcing json as the response
Browse files Browse the repository at this point in the history
  • Loading branch information
pydanny committed Sep 14, 2010
1 parent 62b287c commit ceb21e9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
29 changes: 29 additions & 0 deletions apps/apiv1/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django.core.urlresolvers import reverse
from django.http import HttpResponse

from tastypie.api import Api as TastyPieApi
from tastypie.serializers import Serializer
from tastypie.utils.mime import build_content_type

class Api(TastyPieApi):

def top_level(self, request, api_name=None):
"""
A view that returns a serialized list of all resources registers
to the ``Api``. Useful for discovery.
"""
serializer = Serializer()
available_resources = {}

if api_name is None:
api_name = self.api_name

for name in sorted(self._registry.keys()):
available_resources[name] = reverse("api_dispatch_list", kwargs={
'api_name': api_name,
'resource_name': name,
})

desired_format = "application/json"
serialized = serializer.serialize(available_resources, desired_format)
return HttpResponse(content=serialized, content_type=build_content_type(desired_format))
10 changes: 8 additions & 2 deletions apps/apiv1/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@

# TODO - exclude ID, repo_commits, and other fields not yet used

class EnhancedModelResource(ModelResource):
class BaseResource(ModelResource):

def determine_format(self, *args, **kwargs):

return "application/json"

class EnhancedModelResource(BaseResource):
def obj_get(self, **kwargs):
"""
A ORM-specific implementation of ``obj_get``.
Expand Down Expand Up @@ -105,7 +111,7 @@ class Meta:
lookup_field = 'slug'
excludes = ["id"]

class RepoResource(ModelResource):
class RepoResource(BaseResource):

class Meta:
queryset = Repo.objects.all()
Expand Down
2 changes: 1 addition & 1 deletion urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@

)

from tastypie.api import Api
from apiv1.api import Api
from apiv1.resources import (
GotwResource, DpotwResource,
PackageResource, CategoryResource, RepoResource,
Expand Down

0 comments on commit ceb21e9

Please sign in to comment.