For this simple example we are going to consider that you have knowledge of the client part.
First, in config.py:
from pyextdirect.configuration import create_configuration, expose
Base = create_configuration()
class Test(Base):
@expose
def upper(self, string):
return string.upper()
@expose(base=Base, action='Test')
def lower(string):
return string.lower()
Then, api.py:
from pyextdirect.api import create_api
import config
if __name__ == '__main__':
print "Content-type: text/javascript\r\n\r\n"
print create_api(config.Base)
And finally router.py:
from pyextdirect.router import Router
import config
import cgi
if __name__ == '__main__':
router = Router(config.Base)
fs = cgi.FieldStorage()
print "Content-type: application/json\r\n\r\n"
print router.route(fs.value)
Basic method
DirectLoad method
DirectSubmit method
DirectStore read method
DirectStore create-update-destroy methods
Each class created with this metaclass will have its exposed methods registered
A method can be exposed with the expose() decorator The registration is done by calling register()
Create a configuration base class
It is built using ConfigurationMeta. Subclassing such a base class will register exposed methods
Configuration dict that can be used by a Router or the API
Register an element in the configuration
Parameters: |
|
---|
Decorator to expose a function
Note
A module function can be decorated but base parameter has to be specified
Parameters: |
---|
Router able to route Ext.Direct requests to their right functions and provide the result
Parameters: | bases (Base or list of Base) – configuration bases |
---|
Call the appropriate function
Parameters: | request (dict) – request that describes the function to call |
---|---|
Returns: | response linked to the request that holds the value returned by the called function |
Return type: | dict |
Route an Ext Direct request to the appropriate function(s) and provide the response(s)
Parameters: | data (json or dict or list) – Ext Direct request |
---|---|
Returns: | appropriate response(s) |
Return type: | json |
Create necessary class instances from a configuration with no argument to the constructor
Parameters: | configuration (dict) – configuration dict like in configuration |
---|---|
Returns: | a class-instance mapping |
Return type: | dict |
Create the JS code for the API using one or more Bases
Parameters: |
|
---|
Create an API dict
Parameters: |
---|
Note
Keyword arguments type, url, actions and enableUrlEncode will be overridden
Base class for pyextdirect exceptions
Raised when an error occurs in a form handler method
A result dict can be provided and will be returned to the client
Parameters: |
|
---|
For example:
@expose(kind=SUBMIT)
def save(self, firstname, lastname):
errors = {}
if len(firstname) < 3:
errors['firstname'] = 'Invalid length'
if not lastname:
errors['lastname'] = 'Missing'
if errors:
raise FormError(errors, {'foo': 'bar'})
# ...
Will return the dict {'success': False, 'errors': {'firstname': 'Invalid length', 'lastname': 'Missing'}, 'foo': 'bar'} as result in a response