Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #12 from tunix/master

added simple filtering for listing & authorization methods
  • Loading branch information...
commit b15a7a754522b1299a4df09422bf2d0ac1ab147d 2 parents 6af3d6f + a922e7a
Fatih Erikli authored
Showing with 56 additions and 12 deletions.
  1. +1 −1  setup.py
  2. +55 −11 tastypie_mongodb/resources.py
2  setup.py
View
@@ -13,7 +13,7 @@ def read(fname):
setup(
name="tastypie-mongodb-resource",
- version="0.0.6",
+ version="0.0.8",
author="Fatih Erikli",
author_email="fatiherikli@gmail.com",
description=("Tastypie MongoDB Resource"),
66 tastypie_mongodb/resources.py
View
@@ -2,15 +2,17 @@
from bson import ObjectId
-from django.core.exceptions import ObjectDoesNotExist
from tastypie.bundle import Bundle
from tastypie.resources import Resource
+from django.core.exceptions import ObjectDoesNotExist
+from django.http import QueryDict
class MongoDBResource(Resource):
"""
A base resource that allows to make CRUD operations for mongodb.
"""
+
def get_object_class(self):
return self._meta.object_class
@@ -20,19 +22,44 @@ def get_collection(self):
"""
raise NotImplementedError("You should implement get_collection method.")
+ def apply_filters(self, request, applicable_filters):
+ return list(map(self.get_object_class(), self.get_collection().find(applicable_filters)))
+
+ def build_filters(self, filters):
+ if isinstance(filters, QueryDict):
+ return filters.dict()
+
+ return filters
+
def get_object_list(self, request):
- return self.obj_get_list(request)
+ bundle = self.build_bundle(request=request)
+ return self.obj_get_list(bundle)
- def obj_get_list(self, request=None, **kwargs):
+ def obj_get_list(self, bundle, **kwargs):
"""
- Maps mongodb documents to Document class.
+ Maps mongodb documents to resource's object class.
"""
- return list(map(self.get_object_class(), self.get_collection().find()))
- def obj_get(self, request=None, **kwargs):
+ filters = {}
+ result = []
+
+ self.authorized_read_list(result, bundle)
+
+ if hasattr(bundle.request, 'GET'):
+ filters = bundle.request.GET.copy()
+
+ # Update with the provided kwargs.
+ filters.update(kwargs)
+
+ applicable_filters = self.build_filters(filters=filters)
+
+ return self.apply_filters(bundle.request, applicable_filters)
+
+ def obj_get(self, bundle, **kwargs):
"""
Returns mongodb document from provided id.
"""
+
obj = self.get_collection().find_one({
"_id": ObjectId(kwargs.get("pk"))
})
@@ -40,37 +67,53 @@ def obj_get(self, request=None, **kwargs):
if not obj:
raise ObjectDoesNotExist
+ self.authorized_read_detail(obj, bundle)
+
return self.get_object_class()(obj)
def obj_create(self, bundle, **kwargs):
"""
Creates mongodb document from POST data.
"""
+
bundle.data.update(kwargs)
- bundle.obj = self.get_collection().insert(bundle.data)
- return bundle
- def obj_update(self, bundle, request=None, **kwargs):
+ self.authorized_create_detail(bundle.data, bundle)
+
+ oid = self.get_collection().insert(bundle.data)
+ obj = self._meta.object_class.objects.get(_id=ObjectId(oid))
+
+ return self.build_bundle(request=bundle.request, data=bundle.data, obj=obj)
+
+ def obj_update(self, bundle, **kwargs):
"""
Updates mongodb document.
"""
+
+ self.authorized_update_detail(bundle.data, bundle)
self.get_collection().update(
{"_id": ObjectId(kwargs.get("pk"))},
{"$set": bundle.data}
)
+
return bundle
- def obj_delete(self, request=None, **kwargs):
+ def obj_delete(self, bundle, **kwargs):
"""
Removes single document from collection
"""
+
parameters = {"_id": ObjectId(kwargs.get("pk"))}
+
+ self.authorized_delete_detail(parameters, bundle)
self.get_collection().remove(parameters)
- def obj_delete_list(self, request=None, **kwargs):
+ def obj_delete_list(self, bundle, **kwargs):
"""
Removes all documents from collection
"""
+
+ self.authorized_delete_list(bundle.data, bundle)
self.get_collection().remove()
def detail_uri_kwargs(self, bundle_or_obj):
@@ -80,6 +123,7 @@ def detail_uri_kwargs(self, bundle_or_obj):
By default, it uses the model's ``pk`` in order to create the URI.
"""
+
detail_uri_name = getattr(self._meta, 'detail_uri_name', 'pk')
kwargs = {}
Please sign in to comment.
Something went wrong with that request. Please try again.