Guest User

Untitled

a guest
Feb 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. from tastypie import fields
  2. from tastypie.resources import ModelResource
  3. from tastypie.authentication import BasicAuthentication
  4. from tastypie.authorization import Authorization
  5.  
  6. from haystack.query import SearchQuerySet
  7.  
  8. from nodes.models import Node
  9. from nodes.models import Site
  10.  
  11.  
  12. class SiteResource(ModelResource):
  13. class Meta:
  14. queryset = Site.objects.all()
  15. always_return_data = True
  16. authentication = BasicAuthentication()
  17. authorization = Authorization()
  18.  
  19. class NodeResource(ModelResource):
  20. site = fields.ForeignKey(SiteResource, 'site', null=True)
  21.  
  22. class Meta:
  23. queryset = Node.objects.all()
  24. always_return_data = True
  25. authentication = BasicAuthentication()
  26. authorization = Authorization()
  27.  
  28. def build_filters(self, filters=None):
  29. if filters is None:
  30. filters = {}
  31.  
  32. orm_filters = super(NodeResource, self).build_filters(filters)
  33.  
  34. if "q" in filters and filters["q"]:
  35. sqs = SearchQuerySet().auto_query(filters['q'])
  36. orm_filters = {"pk__in": [ i.pk for i in sqs ]}
  37.  
  38. return orm_filters
Add Comment
Please, Sign In to add comment