python - How to inherit paginate/count methods in RecordView controller? -
i trying understand why method 2 fails , best way to re-use views.
our app using flaskbootstrapsecurity base pattern. we've customize app.controller have 'recordlist' (see method #1). plugins have lists need pagination... wanted avoid duplicating pagination code... tried other methods
method #2 fails saying plugin1 class not exist on -- super(plugin1...).initi() . if use '*arg, **kwargs' within init functions, registry of blueprint classes fails because says providing args when there none?
i testing method 3 now.
can explain why plugin1 class not work in method 2 ?
common cases
app/controller.py
import os flask.views import methodview, methodviewtype flask import current_app, render_template, request, jsonify flask.ext.paginate import pagination flask._compat import with_metaclass class viewmeta(methodviewtype): def __init__(cls, name, bases, dct): super(viewmeta, cls).__init__(name, bases, dct) #***do stuff ** # following fails if *args, **kwargs used in __init__ of recordlist bp.add_url_rule(route, view_func=cls().as_view(endpoint)) class baseview(with_metaclass(viewmeta, methodview)): route = notimplemented blueprint = notimplemented method #1 -- no init, override methods
app/controller.py
class recordlist(templateview): # no __init__ def count(self): return notimplemented def paginate(self, page, per_page): return notimplemented app/app/plugins/plugin1/controller.py
class plugin1list(recordlist): blueprint = plugin route = '/plugin1' route_name = 'plugin1' template_name = 'plugin1.html' decorators = [register_menu(plugin, '.plugin1.plugin1', 'plugin1 list'), login_required, roles_required('plugin1')] # no __init__ def count(self): return plugin1_database.objects.count() def paginate(self, page, per_page): return plugin1_database.objects.paginate(page, per_page) method #2 -- use init
class recordlist(templateview): def __init__(self, *args, **kwargs): ''' init added default self.db ''' super(recordlist,self).__init__() #super(recordlist,self).__init__(*args, **kwargs) self.db = none def count(self): return self.db.objects.count() def paginate(self, page, per_page): return self.db.objects.paginate(page, per_page) app/app/plugins/plugin1/controller.py
class plugin1list(recordlist): # initialize local vars (see method #1) def __init__(self): super(plugin1list,self).__init__() self.db = plugin1_database # count/paginate default parent class method #3 -- use local var in class
app/controller.py
class recordlist(templateview): database = notimplemented def count(self): return self.database.objects.count() def paginate(self, page, per_page): return self.database.objects.paginate(page, per_page) app/app/plugins/plugin1/controller.py
class recordlist(templateview): database = plugin1_database ## no __init__ # count/paginate default parent class
Comments
Post a Comment