Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Allow blueprint to be passed as an argument to MongoRest? #18

Open
ddorian opened this issue Jan 6, 2013 · 1 comment
Open

Allow blueprint to be passed as an argument to MongoRest? #18

ddorian opened this issue Jan 6, 2013 · 1 comment

Comments

@ddorian
Copy link

ddorian commented Jan 6, 2013

Basically i have some other portions of the rest api that don't use mongoengine and i want to keep them together in 1 blueprint.

@ddorian
Copy link
Author

ddorian commented Jan 7, 2013

I was thinking something like this.Url prefix is taken from the blueprint.

class MongoRestBlueprint(object):
    def __init__(self, blueprint, **kwargs):
        self.blueprint = blueprint

    def register(self, **kwargs):
        def decorator(klass):
            document_name = klass.resource.document.__name__.lower()
            name = kwargs.pop('name', document_name)
            url = kwargs.pop('url', '/%s/' % document_name)
            pk_type = kwargs.pop('pk_type', 'string')
            view_func = klass.as_view(name)
            if List in klass.methods:
                self.blueprint.add_url_rule(url, defaults={'pk': None}, view_func=view_func, methods=[List.method], **kwargs)
            if Create in klass.methods or BulkUpdate in klass.methods:
                self.blueprint.add_url_rule(url, view_func=view_func, methods=[x.method for x in klass.methods if x in (Create, BulkUpdate)], **kwargs)
            self.blueprint.add_url_rule('%s<%s:%s>/' % (url, pk_type, 'pk'), view_func=view_func, methods=[x.method for x in klass.methods], **kwargs)
            return klass
        return decorator

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant