-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from grahamu/django-prepare-mixin
Add Django Model EndpointSet mixin
- Loading branch information
Showing
3 changed files
with
49 additions
and
31 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
|
||
class DjangoModelEndpointSetMixin(object): | ||
|
||
def get_pk(self): | ||
""" | ||
Convenience method returning URL PK kwarg. | ||
""" | ||
pk_url_kwarg = self.url.lookup["field"] | ||
return self.kwargs[pk_url_kwarg] if pk_url_kwarg in self.kwargs else None | ||
|
||
def get_resource_object_model(self): | ||
""" | ||
Convenience method returning Resource's object model, if any. | ||
""" | ||
if hasattr(self, "resource_class"): | ||
return self.resource_class.model if hasattr(self.resource_class, "model") else None | ||
else: | ||
return None | ||
|
||
def get_queryset(self): | ||
""" | ||
Convenience method returning all Resource's object model objects. | ||
""" | ||
return self.get_resource_object_model()._default_manager.all() | ||
|
||
def prepare(self): | ||
""" | ||
Sets `self.pk` to the requested PK | ||
Sets `self.obj` to a retrieved Resource object. | ||
Assumes Resource object is based on Django model. | ||
No action is taken if requested method does not operate on single objects. | ||
""" | ||
if self.get_resource_object_model(): | ||
if self.requested_method in ["retrieve", "update", "destroy"]: | ||
self.pk = self.get_pk() | ||
self.obj = self.get_object_or_404(self.get_queryset(), pk=self.pk) |
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