zoom.mvc module¶
zoom.mvc
classes to support the model, view, controller pattern
-
class
zoom.mvc.Controller(model=None, **kwargs)¶ Bases:
zoom.mvc.DispatcherControls a Model
Use this class when an action is going to change the state of the model.
-
class
zoom.mvc.Dispatcher(model=None, **kwargs)¶ Bases:
objectdispatches actions to a method
Accepts incoming user input actions and calls the appropriate method to handle the request. Unlike the Controller and the View, the Dispatcher doesn’t alter the incoming input in any way, but rather passes it along verbatim to the method handling the request.
>>> class MyDispatcher(Dispatcher): ... def add(self, a, b): ... return a + b >>> dispatcher = MyDispatcher() >>> dispatcher('add', 1, 2) 3
-
home= None¶
-
-
class
zoom.mvc.DynamicView(model=None, **k)¶ Bases:
zoom.mvc.ViewDynamic View - experimental (may change)
A decorator class that provides views of objects dynamically loading its own templates in the process.
Within templates the object being decorated is referred to as self. Any attribtues or properties can be simply accessed using self.<name> for whatever the name is. Templates are rendered using python format() function so object structures can be taversed in the usual way within templates.
The object optionally passed as the first parameter upon construction is referred to as self.model. Additional objects can be added as keyword parameters, which can then also be referenced with self.<name>.
-
asset_types= ['html', 'css', 'js']¶
-
fill_js(script, obj)¶ Fill js tags
DynamicView object attributes and properties can be accessed from their accompanying .js content via a {self.<name>} reference.
This method is responsible for filling in these tags and can be overridden by subclasses of DynamicView if a different behaviour is desired.
-
get_assets(name=None)¶ Get view assets
-
index()¶ return the default rendered view
-
render(view=None)¶ Render the view
-
-
class
zoom.mvc.View(model=None, **kwargs)¶ Bases:
zoom.mvc.DispatcherViews a model
Use to view a model without altering it.
>>> class MyView(View): ... def index(self): ... return 'index page' ... def show(self, item): ... return 'showing %s' % item ... def throw(self, item): ... raise Exception('thrown') >>> view = MyView() >>> view() 'index page' >>> view('100') 'showing 100' >>> view('100', data=dict(d='extra')) 'showing 100'
>>> thrown = False >>> try: ... view('throw') ... except Exception: ... thrown = True >>> thrown True
-
show(*args, **kwargs)¶ View a specific item (stub)
-
-
zoom.mvc.as_attr(text)¶ Replace hyphens with underscores
>>> as_attr('this-page') 'this_page'
-
zoom.mvc.dispatch(*args)¶ Create and call dispatchers in order
Returns a function that will handle a request by trying each argument in succession. If the argument is a Dispatcher it will be created before being called. If it is a callable, it will be called as-is. As soon as one of them returns a response we exit. If none of the returns a response we return None, which generally results in a 404.
>>> class MyView(View): ... def index(self): ... return 'home page' ... def show(self, key): ... return 'showing %s' % key >>> main = zoom.dispatch(MyView)
>>> main((), zoom.utils.Bunch(data={})) 'home page'
>>> main(('100',), zoom.utils.Bunch(data={})) 'showing 100'
-
zoom.mvc.evaluate(obj, name, route, data)¶ Get the value of an attribute
>>> thing = zoom.utils.Bunch(name='Thing', show=lambda a, name: 'showing %s' % name) >>> route, data = ('app',), {'name': 'one'} >>> evaluate(thing, 'name', route, data) 'Thing' >>> evaluate(thing, 'show', route, data) 'showing one'
Remove buttons from input data
>>> data = dict(name='Pat', age=20, save_button='Save') >>> zoom.utils.pp(remove_buttons(data)) [ { "save_button": "Save" }, { "age": 20, "name": "Pat" } ]