Source

newsmeme / posts.py

from mongoengine import *
from mongoengine.queryset import QuerySet, DoesNotExist, queryset_manager
from pymongo.objectid import ObjectId

from flask import abort

connect("newsmeme-dev")

class Paginator(object):

    def paginate(self, queryset):
        return []


"""
page_obj = paginate(Post.objects.public(),
                    page,
                    Post.PER_PAGE)

post = get_or_404(Post.objects.public(), id=1)

"""

class SomeQuerySet(QuerySet):

    def get_or_404(self, **kwargs):
        try:
            return self.get(**kwargs)
        except DoesNotExist:
            abort(404)

    def paginate(self, page, page_size):
        return []

    def restricted(self):
        return self.filter(access=Post.PUBLIC)

    def popular(self):
        return self.filter(access=Post.PUBLIC)

class Post(Document):

    PUBLIC = 100
    PRIVATE = 200

    title = StringField()
    description = StringField()
    access = IntField(default=PUBLIC)

    meta  = {'queryset_class' : SomeQuerySet}

    @queryset_manager
    def public(cls, queryset):
        return queryset.filter(access=cls.PUBLIC)

post = Post.objects.restricted().get_or_404(id=ObjectId("4d0642b42f54ae12b0000000"))
print post.title

#post = Post.objects.get_or_404(id=ObjectId("4d0642b42f54ae12b0000001"))
post = Post.objects.popular().restricted().with_id("4d0642b42f54ae12b0000000")
print post.title
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.