from flask import Blueprint
from flask_mongorest.methods import Create, BulkUpdate, List


class MongoRest(object):
    def __init__(self, app, **kwargs):
        self.app = app
        self.url_prefix = kwargs.pop('url_prefix', '')
        app.register_blueprint(Blueprint(self.url_prefix, __name__, template_folder='templates'))

    def register(self, **kwargs):
        def decorator(klass):

            # Construct a url based on a 'name' kwarg with a fallback to a Mongo document's name
            document_name = klass.resource.document.__name__.lower()
            name = kwargs.pop('name', document_name)
            url = kwargs.pop('url', '/%s/' % document_name)

            # Insert the url prefix, if it exists
            if self.url_prefix:
                url = '%s%s' % (self.url_prefix, url)

            # Add url rules
            pk_type = kwargs.pop('pk_type', 'string')
            view_func = klass.as_view(name)
            if List in klass.methods:
                self.app.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.app.add_url_rule(url, view_func=view_func, methods=[x.method for x in klass.methods if x in (Create, BulkUpdate)], **kwargs)
            self.app.add_url_rule('%s<%s:%s>/' % (url, pk_type, 'pk'), view_func=view_func, methods=[x.method for x in klass.methods if x not in (List, BulkUpdate)], **kwargs)
            return klass

        return decorator


@@ -135,16 +135,21 @@
 elf, app
+=None
 , **kwar
@@ -231,16 +231,36 @@
 x', '')%0A
+        if app:%0A    
         
@@ -348,16 +348,62 @@
 lates'))
+%0A        else:%0A            self.url_rules = %5B%5D
 %0A%0A    de
@@ -905,16 +905,38 @@
 rl rules
+ or store for init_app
 %0A       
@@ -1084,35 +1084,24 @@
     
-self.app.add_url_rule(url, 
+list_kwa = dict(
 defa
@@ -1161,26 +1161,225 @@
 .method%5D
-, **kwargs
+)%0A                list_kwa.update(**kwargs)%0A                if self.app:%0A                    self.app.add_url_rule(url, list_kwa)%0A                else:%0A                    self.url_rules.append((url, list_kwa)
 )%0A      
@@ -1463,35 +1463,26 @@
     
-self.app.add_url_rule(url, 
+create_kwa = dict(
 view
@@ -1572,244 +1572,863 @@
 te)%5D
-, **kwargs)%0A            self.app.add_url_rule('%25s%3C%25s:%25s%3E/' %25 (url, pk_type, 'pk'), view_func=view_func, methods=%5Bx.method for x in klass.methods if x not in (List, BulkUpdate)%5D, **kwargs)%0A            return klass%0A%0A        return decorator%0A
+)%0A                create_kwa.update(**kwargs)%0A                if self.app:%0A                    self.app.add_url_rule(url, create_kwa)%0A                else:%0A                    self.url_rules.append((url, create_kwa))%0A%0A                kwa = dict(view_func=view_func, methods=%5Bx.method for x in klass.methods if x not in (List, BulkUpdate)%5D)%0A                kwa.update(kwargs)%0A                url = '%25s%3C%25s:%25s%3E/' %25 (url, pk_type, 'pk')%0A            if self.app:%0A                self.app.add_url_rule(**kwa)%0A            else:%0A                self.url_rules.append((url, kwa))%0A            return klass%0A%0A        return decorator%0A%0A    def init_app(self, app):%0A        self.app = app%0A        app.register_blueprint(Blueprint(self.url_prefix, __name__, template_folder='templates'))%0A        for url, kwa in self.url_rules:%0A            self.app.add_url_rule(url, **kwa)
 %0A

enable MongoRest for app factories via init_app.