Skip to content

Instantly share code, notes, and snippets.

@0atman
Last active December 19, 2015 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0atman/5933572 to your computer and use it in GitHub Desktop.
Save 0atman/5933572 to your computer and use it in GitHub Desktop.
Stub out tastypie resources with static json
from django.http import HttpResponse
class Stub(object):
"""
Mixin. Use:
class MyResource(Stub, ...) # (Stub must be the first parent class)
Now MyResource will do no processing, auth or model-checking.
It will simply respond with the contents of the json file in the local directory
that matches the URL. (eg /api/v1/content/ -> api/v1/content.json)
"""
def dispatch(*args, **kwargs):
uri_path = args[2].META['PATH_INFO']
file_path = uri_path[1:-1] + ".json"
with open(file_path) as json_file:
return HttpResponse(
json_file.read(),
content_type="application/json"
)
@0atman
Copy link
Author

0atman commented Jul 5, 2013

Create a folder hirearchy like:
api/v1/my_endpoint
to hold your static json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment