Last active
December 19, 2015 09:29
-
-
Save 0atman/5933572 to your computer and use it in GitHub Desktop.
Stub out tastypie resources with static json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create a folder hirearchy like:
api/v1/my_endpoint
to hold your static json