Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience methods to EndpointSet #10

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion pinax/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.conf import settings
from django.conf.urls import url
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db.models.base import ModelBase
from django.http import HttpResponse, Http404
from django.views.generic import View
from django.views.decorators.csrf import csrf_exempt
Expand Down Expand Up @@ -79,8 +80,43 @@ def handle_exception(self, exc):
else:
return self.render_error("unknown server error", status=500)

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):
pass
"""
Sets `self.obj` to a retrieved Resource object.

No action is taken if requested method does not operate on single objects.

No action is taken if EndpointSet.get_object_model_class()
does not return a Django model.
"""
# EndpointSets may use a different data storage than Django models.
# Do not assume Django models are used.
if isinstance(self.get_resource_object_model(), ModelBase):
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)

def check_authentication(self, handler):
user = None
Expand Down