| OLD | NEW |
| (Empty) |
| 1 # -*- test-case-name: twisted.web.test.test_woven -*- | |
| 2 | |
| 3 __version__ = "$Revision: 1.13 $"[11:-2] | |
| 4 | |
| 5 from zope.interface import Interface | |
| 6 | |
| 7 class IModel(Interface): | |
| 8 """A MVC Model.""" | |
| 9 def addView(view): | |
| 10 """Add a view for the model to keep track of. | |
| 11 """ | |
| 12 | |
| 13 def removeView(view): | |
| 14 """Remove a view that the model no longer should keep track of. | |
| 15 """ | |
| 16 | |
| 17 def notify(changed=None): | |
| 18 """Notify all views that something was changed on me. | |
| 19 Passing a dictionary of {'attribute': 'new value'} in changed | |
| 20 will pass this dictionary to the view for increased performance. | |
| 21 If you don't want to do this, don't, and just use the traditional | |
| 22 MVC paradigm of querying the model for things you're interested | |
| 23 in. | |
| 24 """ | |
| 25 | |
| 26 def getData(): | |
| 27 """Return the raw data contained by this Model object, if it is a | |
| 28 wrapper. If not, return self. | |
| 29 """ | |
| 30 | |
| 31 def setData(request, data): | |
| 32 """Set the raw data referenced by this Model object, if it is a | |
| 33 wrapper. This is done by telling our Parent model to setSubmodel | |
| 34 the new data. If this object is not a wrapper, keep the data | |
| 35 around and return it for subsequent getData calls. | |
| 36 """ | |
| 37 | |
| 38 def lookupSubmodel(request, submodelPath): | |
| 39 """Return an IModel implementor for the given submodel path | |
| 40 string. This path may be any number of elements separated | |
| 41 by /. The default implementation splits on "/" and calls | |
| 42 getSubmodel until the path is exhausted. You will not normally | |
| 43 need to override this behavior. | |
| 44 """ | |
| 45 | |
| 46 def getSubmodel(request, submodelName): | |
| 47 """Return an IModel implementor for the submodel named | |
| 48 "submodelName". If this object contains simple data types, | |
| 49 they can be adapted to IModel using | |
| 50 model.adaptToIModel(m, parent, name) before returning. | |
| 51 """ | |
| 52 | |
| 53 def setSubmodel(request, submodelName, data): | |
| 54 """Set the given data as a submodel of this model. The data | |
| 55 need not implement IModel, since getSubmodel should adapt | |
| 56 the data to IModel before returning it. | |
| 57 """ | |
| 58 | |
| 59 | |
| 60 class IView(Interface): | |
| 61 """A MVC View""" | |
| 62 def __init__(model, controller=None): | |
| 63 """A view must be told what its model is, and may be told what its | |
| 64 controller is, but can also look up its controller if none specified. | |
| 65 """ | |
| 66 | |
| 67 def modelChanged(changed): | |
| 68 """Dispatch changed messages to any update_* methods which | |
| 69 may have been defined, then pass the update notification on | |
| 70 to the controller. | |
| 71 """ | |
| 72 | |
| 73 def controllerFactory(): | |
| 74 """Hook for subclasses to customize the controller that is associated | |
| 75 with the model associated with this view. | |
| 76 | |
| 77 Default behavior: Look up a component that implements IController | |
| 78 for the self.model instance. | |
| 79 """ | |
| 80 | |
| 81 def setController(controller): | |
| 82 """Set the controller that this view is related to.""" | |
| 83 | |
| 84 def importViewLibrary(moduleOrObject): | |
| 85 """Import the given object or module into this View's view namespace | |
| 86 stack. If the given object or module has a getSubview function or | |
| 87 method, it will be called when a node has a view="foo" attribute. | |
| 88 If no getSubview method is defined, a default one will be provided | |
| 89 which looks for the literal name in the namespace. | |
| 90 """ | |
| 91 | |
| 92 def getSubview(request, node, model, viewName): | |
| 93 """Look for a view named "viewName" to handle the node "node". | |
| 94 When a node <div view="foo" /> is present in the template, this | |
| 95 method will be called with viewName set to "foo". | |
| 96 | |
| 97 Return None if this View doesn't want to provide a Subview for | |
| 98 the given name. | |
| 99 """ | |
| 100 | |
| 101 def setSubviewFactory(name, factory, setup=None): | |
| 102 """Set the callable "factory", which takes a model and should | |
| 103 return a Widget, to be called by the default implementation of | |
| 104 getSubview when the viewName "name" is present in the template. | |
| 105 | |
| 106 This would generally be used like this: | |
| 107 | |
| 108 view.setSubviewFactory("foo", MyFancyWidgetClass) | |
| 109 | |
| 110 This is equivalent to:: | |
| 111 | |
| 112 def wvfactory_foo(self, request, node, m): | |
| 113 return MyFancyWidgetClass(m) | |
| 114 | |
| 115 Which will cause an instance of MyFancyWidgetClass to be | |
| 116 instanciated when template node <div view="foo" /> is encountered. | |
| 117 | |
| 118 If setup is passed, it will be passed to new instances returned | |
| 119 from this factory as a setup method. The setup method is called | |
| 120 each time the Widget is generated. Setup methods take (request, | |
| 121 widget, model) as arguments. | |
| 122 | |
| 123 This is equivalent to:: | |
| 124 | |
| 125 def wvupdate_foo(self, request, widget, model): | |
| 126 # whatever you want | |
| 127 """ | |
| 128 | |
| 129 def __adapt__(adaptable, default): | |
| 130 if hasattr(adaptable, 'original'): | |
| 131 return IView(adaptable.original, default) | |
| 132 return default | |
| 133 | |
| 134 | |
| 135 class IController(Interface): | |
| 136 """A MVC Controller""" | |
| 137 def setView(view): | |
| 138 """Set the view that this controller is related to. | |
| 139 """ | |
| 140 | |
| 141 def importControllerLibrary(moduleOrObject): | |
| 142 """Import the given object or module into this Controllers's | |
| 143 controller namespace stack. If the given object or module has a | |
| 144 getSubcontroller function or method, it will be called when a node | |
| 145 has a controller="foo" attribute. If no getSubcontroller method is | |
| 146 defined, a default one will be provided which looks for the literal | |
| 147 name in the namespace. | |
| 148 """ | |
| 149 | |
| 150 def getSubcontroller(request, node, model, controllerName): | |
| 151 """Look for a controller named "controllerName" to handle the node | |
| 152 "node". When a node <div controller="foo" /> is present in the | |
| 153 template, this method will be called with controllerName set to "foo". | |
| 154 | |
| 155 Return None if this Controller doesn't want to provide a Subcontroller | |
| 156 for the given name. | |
| 157 """ | |
| 158 | |
| 159 def setSubcontrollerFactory(name, factory): | |
| 160 """Set the callable "factory", which takes a model and should | |
| 161 return an InputHandler, to be called by the default implementation of | |
| 162 getSubview when the controllerName "name" is present in the template. | |
| 163 | |
| 164 This would generally be used like this:: | |
| 165 | |
| 166 view.setSubcontrollerFactory("foo", MyFancyInputHandlerClass) | |
| 167 | |
| 168 This is equivalent to:: | |
| 169 | |
| 170 def wcfactory_foo(self, request, node, m): | |
| 171 return MyFancyInputHandlerClass(m) | |
| 172 | |
| 173 Which will cause an instance of MyFancyInputHandlerClass to be | |
| 174 instanciated when template node <div controller="foo" /> is | |
| 175 encountered. | |
| 176 """ | |
| 177 | |
| 178 def __adapt__(adaptable, default): | |
| 179 if hasattr(adaptable, 'original'): | |
| 180 return IController(adaptable.original, default) | |
| 181 return default | |
| 182 | |
| 183 | |
| 184 class IWovenLivePage(Interface): | |
| 185 def getCurrentPage(): | |
| 186 """Return the current page object contained in this session. | |
| 187 """ | |
| 188 | |
| 189 def setCurrentPage(page): | |
| 190 """Set the current page object contained in this session. | |
| 191 """ | |
| 192 | |
| 193 def sendJavaScript(js): | |
| 194 """Send "js" to the live page's persistent output conduit for | |
| 195 execution in the browser. If there is no conduit connected yet, | |
| 196 save the js and write it as soon as the output conduit is | |
| 197 connected. | |
| 198 """ | |
| OLD | NEW |