Skip to content

Instantly share code, notes, and snippets.

@d7my11
Created February 8, 2016 12:03
Show Gist options
  • Save d7my11/3b7155f02c053c3730f4 to your computer and use it in GitHub Desktop.
Save d7my11/3b7155f02c053c3730f4 to your computer and use it in GitHub Desktop.
# base tastypie class
class BaseResource(ModelResource):
# Endpoint refers to function in the Resource eg. function_data_whatever
# IMPORTANT: URL will be replaced to function/data/whatever
def alter_list_data_to_serialize(self, request, data):
""" Used to return only meta data, if we have meta_only = true """
if request.GET.get('meta_only'):
return {'meta': data['meta']}
return data
def add_url(self, endpoint):
"""Adds additional urls to the resource
:endpoint: the end point API to be added
:returns: the url created ( for tastypie )
"""
api = r'(?P<resource_name>%s)/(?P<endpoint>%s)%s$' % (
self._meta.resource_name, endpoint, trailing_slash())
endpoint = endpoint.replace('/', '_')
return url(api, self.wrap_view(endpoint), name='api_%s' % endpoint)
def get_object_list(self, request):
return super(CosmeResource, self).get_object_list(request).distinct()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment