Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 2 changed files with 56 additions and 12 deletions. Show diff stats Hide diff stats

  1. +1 1  setup.py
  2. +55 11 tastypie_mongodb/resources.py
2  setup.py
@@ -13,7 +13,7 @@ def read(fname):
13 13
14 14 setup(
15 15 name="tastypie-mongodb-resource",
16   - version="0.0.6",
  16 + version="0.0.8",
17 17 author="Fatih Erikli",
18 18 author_email="fatiherikli@gmail.com",
19 19 description=("Tastypie MongoDB Resource"),
66 tastypie_mongodb/resources.py
@@ -2,15 +2,17 @@
2 2
3 3 from bson import ObjectId
4 4
5   -from django.core.exceptions import ObjectDoesNotExist
6 5 from tastypie.bundle import Bundle
7 6 from tastypie.resources import Resource
  7 +from django.core.exceptions import ObjectDoesNotExist
  8 +from django.http import QueryDict
8 9
9 10
10 11 class MongoDBResource(Resource):
11 12 """
12 13 A base resource that allows to make CRUD operations for mongodb.
13 14 """
  15 +
14 16 def get_object_class(self):
15 17 return self._meta.object_class
16 18
@@ -20,19 +22,44 @@ def get_collection(self):
20 22 """
21 23 raise NotImplementedError("You should implement get_collection method.")
22 24
  25 + def apply_filters(self, request, applicable_filters):
  26 + return list(map(self.get_object_class(), self.get_collection().find(applicable_filters)))
  27 +
  28 + def build_filters(self, filters):
  29 + if isinstance(filters, QueryDict):
  30 + return filters.dict()
  31 +
  32 + return filters
  33 +
23 34 def get_object_list(self, request):
24   - return self.obj_get_list(request)
  35 + bundle = self.build_bundle(request=request)
  36 + return self.obj_get_list(bundle)
25 37
26   - def obj_get_list(self, request=None, **kwargs):
  38 + def obj_get_list(self, bundle, **kwargs):
27 39 """
28   - Maps mongodb documents to Document class.
  40 + Maps mongodb documents to resource's object class.
29 41 """
30   - return list(map(self.get_object_class(), self.get_collection().find()))
31 42
32   - def obj_get(self, request=None, **kwargs):
  43 + filters = {}
  44 + result = []
  45 +
  46 + self.authorized_read_list(result, bundle)
  47 +
  48 + if hasattr(bundle.request, 'GET'):
  49 + filters = bundle.request.GET.copy()
  50 +
  51 + # Update with the provided kwargs.
  52 + filters.update(kwargs)
  53 +
  54 + applicable_filters = self.build_filters(filters=filters)
  55 +
  56 + return self.apply_filters(bundle.request, applicable_filters)
  57 +
  58 + def obj_get(self, bundle, **kwargs):
33 59 """
34 60 Returns mongodb document from provided id.
35 61 """
  62 +
36 63 obj = self.get_collection().find_one({
37 64 "_id": ObjectId(kwargs.get("pk"))
38 65 })
@@ -40,37 +67,53 @@ def obj_get(self, request=None, **kwargs):
40 67 if not obj:
41 68 raise ObjectDoesNotExist
42 69
  70 + self.authorized_read_detail(obj, bundle)
  71 +
43 72 return self.get_object_class()(obj)
44 73
45 74 def obj_create(self, bundle, **kwargs):
46 75 """
47 76 Creates mongodb document from POST data.
48 77 """
  78 +
49 79 bundle.data.update(kwargs)
50   - bundle.obj = self.get_collection().insert(bundle.data)
51   - return bundle
52 80
53   - def obj_update(self, bundle, request=None, **kwargs):
  81 + self.authorized_create_detail(bundle.data, bundle)
  82 +
  83 + oid = self.get_collection().insert(bundle.data)
  84 + obj = self._meta.object_class.objects.get(_id=ObjectId(oid))
  85 +
  86 + return self.build_bundle(request=bundle.request, data=bundle.data, obj=obj)
  87 +
  88 + def obj_update(self, bundle, **kwargs):
54 89 """
55 90 Updates mongodb document.
56 91 """
  92 +
  93 + self.authorized_update_detail(bundle.data, bundle)
57 94 self.get_collection().update(
58 95 {"_id": ObjectId(kwargs.get("pk"))},
59 96 {"$set": bundle.data}
60 97 )
  98 +
61 99 return bundle
62 100
63   - def obj_delete(self, request=None, **kwargs):
  101 + def obj_delete(self, bundle, **kwargs):
64 102 """
65 103 Removes single document from collection
66 104 """
  105 +
67 106 parameters = {"_id": ObjectId(kwargs.get("pk"))}
  107 +
  108 + self.authorized_delete_detail(parameters, bundle)
68 109 self.get_collection().remove(parameters)
69 110
70   - def obj_delete_list(self, request=None, **kwargs):
  111 + def obj_delete_list(self, bundle, **kwargs):
71 112 """
72 113 Removes all documents from collection
73 114 """
  115 +
  116 + self.authorized_delete_list(bundle.data, bundle)
74 117 self.get_collection().remove()
75 118
76 119 def detail_uri_kwargs(self, bundle_or_obj):
@@ -80,6 +123,7 @@ def detail_uri_kwargs(self, bundle_or_obj):
80 123
81 124 By default, it uses the model's ``pk`` in order to create the URI.
82 125 """
  126 +
83 127 detail_uri_name = getattr(self._meta, 'detail_uri_name', 'pk')
84 128 kwargs = {}
85 129

0 comments on commit b15a7a7

Please sign in to comment.
Something went wrong with that request. Please try again.