Commits

Anonymous committed fcc6f31

custom queryset

Comments (0)

Files changed (2)

flaskext/mongoengine.py

 from __future__ import absolute_import
 
 import math
+import warnings
 
 import mongoengine
 
-from mongoengine.queryset import MultipleObjectsReturned, DoesNotExist
+from mongoengine.queryset import MultipleObjectsReturned, DoesNotExist, \
+    QuerySet
+
 from mongoengine import ValidationError
 
 from flask import abort
 
         _include_mongoengine(self)
 
-        self.Model = Model
-
         if app is not None:
             self.init_app(app)
 
     # for custom QuerySets.
 
     def get_or_404(self, qs, *args, **kwargs):
+
+        warnings.warn("deprecated:use Model.objects.get_or_404", 
+                      DeprecationWarning)
+
         try:
             return qs.get(*args, **kwargs)
         except (MultipleObjectsReturned, DoesNotExist, ValidationError):
 
     def first_or_404(self, qs):
 
+        warnings.warn("deprecated:use Model.objects.first_or_404",
+                      DeprecationWarning)
+
         obj = qs.first()
         if obj is None:
             abort(404)
 
     def paginate(self, qs, page, per_page, error_out=True):
         
+        warnings.warn("deprecated:use Model.objects.paginate",
+                      DeprecationWarning)
+
         if error_out and page < 1:
             abort(404)
         
                 last = num
 
 
-class Model(mongoengine.Document):
+class BaseQuerySet(QuerySet):
 
-    pass
+    def get_or_404(self, *args, **kwargs):
 
+        warnings.warn("deprecated:use Model.objects.get_or_404", 
+                      DeprecationWarning)
+
+        try:
+            return self.get(*args, **kwargs)
+        except (MultipleObjectsReturned, DoesNotExist, ValidationError):
+            abort(404)
+
+    def first_or_404(self):
+
+        warnings.warn("deprecated:use Model.objects.first_or_404",
+                      DeprecationWarning)
+
+        obj = self.first()
+        if obj is None:
+            abort(404)
+
+        return obj
+
+    def paginate(self, page, per_page, error_out=True):
+        
+        warnings.warn("deprecated:use Model.objects.paginate",
+                      DeprecationWarning)
+
+        if error_out and page < 1:
+            abort(404)
+        
+        offset = (page - 1) * per_page
+        items = self[offset:per_page]
+
+        if not items and page != 1 and error_out:
+            abort(404)
+
+        return Pagination(self, page, per_page, self.count(), items)
+
+
+

test_mongoengine.py

 
 
 def make_todo_model(db):
-    class Todo(db.Model):
+    class Todo(db.Document):
         title = db.StringField(max_length=60)
         text = db.StringField()
         done = db.BooleanField(default=False)
         pub_date = db.DateTimeField(default=datetime.utcnow)
+        meta = {'queryset_class' : mongoengine.BaseQuerySet}
     return Todo
 
 
 
         @app.route('/show/<id>/')
         def show(id):
-            todo = db.get_or_404(self.Todo.objects, id=id)
+            todo = self.Todo.objects.get_or_404(id=id)
 
         self.app = app
         self.db = db