Skip to content

Instantly share code, notes, and snippets.

@hughes
Forked from EdwardIII/pie.py
Created December 11, 2012 19:12
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 hughes/4261170 to your computer and use it in GitHub Desktop.
Save hughes/4261170 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):
print request
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):
people = fields.ToManyField(PersonResource, 'person_set')
class Meta:
queryset = Basket.objects.all()
resource_name = 'basket'
authentication = Authentication()
authorization = BasketAuthorization()
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