Skip to content

Instantly share code, notes, and snippets.

@EdwardIII
Created December 11, 2012 18:54
Show Gist options
  • Save EdwardIII/4261013 to your computer and use it in GitHub Desktop.
Save EdwardIII/4261013 to your computer and use it in GitHub Desktop.
from tastypie.resources import ModelResource, ALL_WITH_RELATIONS
from people.models import Basket, Person
from tastypie import fields
from tastypie.authentication import Authentication
from tastypie.authorization import Authorization
class BasketAuthorization(Authorization):
def is_authorized(self, request, object=None):
if request and hasattr(request, 'user') and hasattr(request.user, 'employer'):
return True
else:
return False
# Optional but useful for advanced limiting, such as per user.
def apply_limits(self, request, object_list):
if request and hasattr(request, 'user') and hasattr(request.user, 'employer'):
return object_list.filter(employer=request.user.employer)
return object_list.none()
class BasketResource(ModelResource):
class Meta:
queryset = Basket.objects.all()
resource_name = 'basket'
authentication = Authentication()
authorization = BasketAuthorization()
people = fields.ToManyField('PersonResource', 'person_set')
class PersonResource(ModelResource):
basket = fields.ForeignKey(BasketResource, 'basket')
class Meta:
queryset = Person.objects.all()
resource_name = 'person'
authorization = Authorization()
filtering = {
'basket': ALL_WITH_RELATIONS
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment