Created
October 10, 2012 17:33
-
-
Save boronine/3867109 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CompanyResource(ModelResource): | |
class Meta: | |
resource_name = 'companies' | |
api_name = 'v1' | |
detail_uri_name = 'domain' | |
queryset = Company.objects.all() | |
def prepend_urls(self): | |
return [ | |
url(r"^(?P<resource_name>%s)/(?P<domain>[\w\d_.-]+)/$" % | |
self._meta.resource_name, | |
self.wrap_view('dispatch_detail'), | |
name="api_dispatch_detail"), | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CompanyResource(ModelResource): | |
class Meta: | |
resource_name = 'companies' | |
queryset = Company.objects.all() | |
def override_urls(self): | |
return [ | |
url(r"^(?P<resource_name>%s)/(?P<domain>[\w\d_.-]+)/$" % | |
self._meta.resource_name, | |
self.wrap_view('dispatch_detail'), | |
name="api_dispatch_detail"), | |
] | |
def get_resource_uri(self, bundle_or_obj): | |
kwargs = { | |
'resource_name': self._meta.resource_name, | |
'api_name': self._meta.api_name | |
} | |
if isinstance(bundle_or_obj, Bundle): | |
kwargs['domain'] = bundle_or_obj.obj.domain | |
else: | |
kwargs['domain'] = bundle_or_obj.domain | |
return self._build_reverse_url("api_dispatch_detail", kwargs=kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CompanyResource(Resource): | |
class Meta: | |
resource_name = 'companies' | |
api_name = 'v1' | |
def obj_get(self, request=None, **kwargs): | |
return Company.objects.get(domain=kwargs['domain']) | |
def prepend_urls(self): | |
return [ | |
url(r"^(?P<resource_name>%s)/(?P<domain>[\w\d_.-]+)/$" % | |
self._meta.resource_name, | |
self.wrap_view('dispatch_detail'), | |
name="api_dispatch_detail"), | |
] | |
# This (along with _meta.api_name and _meta.resource_name) will | |
# need to be plugged into the above URL | |
def detail_uri_kwargs(self, bundle_or_obj): | |
if isinstance(bundle_or_obj, Bundle): | |
return { 'domain': bundle_or_obj.obj.domain } | |
else: | |
return { 'domain': bundle_or_obj.domain } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment