Commit 894f6325 by Tom Christie

Remove use of Q objects.

parent 7886fa2b
...@@ -6,7 +6,6 @@ classes that can be added to a `View`. ...@@ -6,7 +6,6 @@ classes that can be added to a `View`.
from django.contrib.auth.models import AnonymousUser from django.contrib.auth.models import AnonymousUser
from django.core.paginator import Paginator from django.core.paginator import Paginator
from django.db.models.fields.related import ForeignKey from django.db.models.fields.related import ForeignKey
from django.db.models.query import Q
from django.http import HttpResponse from django.http import HttpResponse
from urlobject import URLObject from urlobject import URLObject
...@@ -479,39 +478,25 @@ class ModelMixin(object): ...@@ -479,39 +478,25 @@ class ModelMixin(object):
queryset = None queryset = None
def build_query(self, *args, **kwargs): def get_query_kwargs(self, *args, **kwargs):
""" Returns django.db.models.Q object to be used for the objects retrival. """
Return a dict of kwargs that will be used to build the
Arguments: model instance retrieval or to filter querysets.
- args: unnamed URL arguments
- kwargs: named URL arguments
If a URL passes any arguments to the view being the QueryMixin subclass
build_query manages the arguments and provides the Q object that will be
used for the objects retrival with filter/get queryset methods.
Technically, neither args nor kwargs have to be provided, however the default
behaviour is to map all kwargs as the query constructors so that if this
method is not overriden only kwargs keys being model fields are valid.
If positional args are provided, the last one argument is understood
as the primary key. However this usage should be considered
deperecated, and will be removed in a future version.
""" """
tmp = dict(kwargs) kwargs = dict(kwargs)
# If the URLconf includes a .(?P<format>\w+) pattern to match against # If the URLconf includes a .(?P<format>\w+) pattern to match against
# a .json, .xml suffix, then drop the 'format' kwarg before # a .json, .xml suffix, then drop the 'format' kwarg before
# constructing the query. # constructing the query.
if BaseRenderer._FORMAT_QUERY_PARAM in tmp: if BaseRenderer._FORMAT_QUERY_PARAM in kwargs:
del tmp[BaseRenderer._FORMAT_QUERY_PARAM] del kwargs[BaseRenderer._FORMAT_QUERY_PARAM]
return Q(**tmp) return kwargs
def get_instance_data(self, model, content, **kwargs): def get_instance_data(self, model, content, **kwargs):
""" """
Returns the dict with the data for model instance creation/update query. Returns the dict with the data for model instance creation/update.
Arguments: Arguments:
- model: model class (django.db.models.Model subclass) to work with - model: model class (django.db.models.Model subclass) to work with
...@@ -536,12 +521,11 @@ class ModelMixin(object): ...@@ -536,12 +521,11 @@ class ModelMixin(object):
return all_kw_args return all_kw_args
def get_object(self, *args, **kwargs): def get_instance(self, **kwargs):
""" """
Get the instance object for read/update/delete requests. Get a model instance for read/update/delete requests.
""" """
model = self.resource.model return self.get_queryset().get(**kwargs)
return model.objects.get(self.build_query(*args, **kwargs))
def get_queryset(self): def get_queryset(self):
""" """
...@@ -563,21 +547,15 @@ class ReadModelMixin(ModelMixin): ...@@ -563,21 +547,15 @@ class ReadModelMixin(ModelMixin):
""" """
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
model = self.resource.model model = self.resource.model
query_kwargs = self.get_query_kwargs(request, *args, **kwargs)
try: try:
self.model_instance = self.get_object(*args, **kwargs) self.model_instance = self.get_instance(**query_kwargs)
except model.DoesNotExist: except model.DoesNotExist:
raise ErrorResponse(status.HTTP_404_NOT_FOUND) raise ErrorResponse(status.HTTP_404_NOT_FOUND)
return self.model_instance return self.model_instance
def build_query(self, *args, **kwargs):
# Build query is overriden to filter the kwargs priori
# to use them as build_query argument
filtered_keywords = kwargs.copy()
return super(ReadModelMixin, self).build_query(*args, **filtered_keywords)
class CreateModelMixin(ModelMixin): class CreateModelMixin(ModelMixin):
""" """
...@@ -625,11 +603,12 @@ class UpdateModelMixin(ModelMixin): ...@@ -625,11 +603,12 @@ class UpdateModelMixin(ModelMixin):
""" """
def put(self, request, *args, **kwargs): def put(self, request, *args, **kwargs):
model = self.resource.model model = self.resource.model
query_kwargs = self.get_query_kwargs(request, *args, **kwargs)
# TODO: update on the url of a non-existing resource url doesn't work # TODO: update on the url of a non-existing resource url doesn't work
# correctly at the moment - will end up with a new url # correctly at the moment - will end up with a new url
try: try:
self.model_instance = self.get_object(*args, **kwargs) self.model_instance = self.get_instance(*query_kwargs)
for (key, val) in self.CONTENT.items(): for (key, val) in self.CONTENT.items():
setattr(self.model_instance, key, val) setattr(self.model_instance, key, val)
...@@ -645,9 +624,10 @@ class DeleteModelMixin(ModelMixin): ...@@ -645,9 +624,10 @@ class DeleteModelMixin(ModelMixin):
""" """
def delete(self, request, *args, **kwargs): def delete(self, request, *args, **kwargs):
model = self.resource.model model = self.resource.model
query_kwargs = self.get_query_kwargs(request, *args, **kwargs)
try: try:
instance = self.get_object(*args, **kwargs) instance = self.get_instance(**query_kwargs)
except model.DoesNotExist: except model.DoesNotExist:
raise ErrorResponse(status.HTTP_404_NOT_FOUND, None, {}) raise ErrorResponse(status.HTTP_404_NOT_FOUND, None, {})
...@@ -663,8 +643,9 @@ class ListModelMixin(ModelMixin): ...@@ -663,8 +643,9 @@ class ListModelMixin(ModelMixin):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
queryset = self.get_queryset() queryset = self.get_queryset()
ordering = self.get_ordering() ordering = self.get_ordering()
query_kwargs = self.get_query_kwargs(request, *args, **kwargs)
queryset = queryset.filter(self.build_query(**kwargs)) queryset = queryset.filter(**query_kwargs)
if ordering: if ordering:
queryset = queryset.order_by(*ordering) queryset = queryset.order_by(*ordering)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment