Skip to content

Instantly share code, notes, and snippets.

@marteinn
Created September 8, 2012 14:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save marteinn/3675459 to your computer and use it in GitHub Desktop.
Save marteinn/3675459 to your computer and use it in GitHub Desktop.
Tastypie and ApiToken resource example
from tastypie.exceptions import NotFound
from tastypie.resources import ModelResource
from tastypie.authentication import BasicAuthentication, ApiKeyAuthentication
from tastypie.models import ApiKey
from django.contrib.auth.models import User
__author__ = 'martinsandstrom'
class ApiTokenResource(ModelResource):
class Meta:
queryset = ApiKey.objects.all()
resource_name = "token"
include_resource_uri = False
fields = ["key"]
list_allowed_methods = []
detail_allowed_methods = ["get"]
authentication = BasicAuthentication()
def obj_get(self, request=None, **kwargs):
if kwargs["pk"] != "auth":
raise NotImplementedError("Resource not found")
user = request.user
if not user.is_active:
raise NotFound("User not active")
api_key = ApiKey.objects.get(user=request.user)
return api_key
@mkubenka
Copy link

Updated Gist for tastypie 0.9.12
https://gist.github.com/mkubenka/5142319

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