< Task tree | Table of content | Datasets >
The client application sends a request to the server each time the following methods are called:
loadmethod of the task,open,apply,server,total_recordsmethods of a data item orprint methodof a report item.
The total_records method is called by a table to display the number of pages in the paginator.
All these methods use the process_request method of the task to send a request to the server. You can set a breakpoint there to check all client-server interactions.
The process_request method sends to the server the following information:
nameof the requestIDof thetaskIDof theitem, whose method was calledrequestparameters
Note
Each item of the task tree has a unique ID attribute.
The server on receiving the request, based on passed IDs, finds the corresponding task (it can be Project task or Administrator task) and the item on the server, executes an appropriate method of the item with passed parameters and returns the result of the execution to the client.
The server methods can trigger events that can modify their default behavior.
Every item of the task tree on the server has
- the
environand - the
session
attributes that store context of the current request.
Let's take a closer look at the server method of the item on the client. You can use it to execute a function declared in the server module of an item.
For example, we can execute the test function declared in the server module of the catalog item:
def test(item, a, b):
return a + bby using the server method
task.catalog.server('test', [1,2], function(result, error) {
console.log(result, error)
})We pass it, the name of the function and the list of parameters.
Note
The first parameter of the function on the server must be an item object.
If a callback function is specified, the function on the server is executed asynchronously, after which the callback is executed with parameters that are:
- the
resultof the server function and - the
error text, if an exception was raised during the server function execution.
Otherwise the server method is executed synchronously and returns the result of execution. If an exception was raised during execution, the server method throws an exception.
Let's raise an Exception:
def test(item, a, b):
raise Exception('Some error')
return a + b task.catalog.server('test', [1,2], function(result, error) {
console.log(result, error)
})Knowing all of these you can access the environ and session attributes and call the can_view, can_create, can_edit, can_delete methods.
def test(item, a, b):
print 'Session:', item.session
print 'Environment', item.environ
return a + b task.catalog.server('test', [1,2], function(result, error) {
console.log(result, error)
})Let's set a safe mode and change the privileges of the user role
def test(item, a, b):
print 'User_name', item.session['user_info']['user_name']
print 'Role_id', item.session['user_info']['role_id']
print 'Can view', item.can_view()
print 'Can create', item.can_create()
print 'Can edit', item.can_edit()
print 'Can delete', item.can_delete()
return a + bYou can use item.session['user_info']['role_id'] or item's can_view,
can_create, can_edit, can_delete methods to limit user rights.
def test(item, a, b):
if item.session['user_info']['role_id'] == 2:
raise Exception('%s, you are not allowed to perform this operation' % \
item.session['user_info']['user_name'])
return a + b task.catalog.server('test', [1,2], function(result, error) {
if (error) {
task.warning(error)
}
else {
console.log(result)
}
})Besides this client-server exchange mechanism you can:
- download files from server
staticfolder, - upload files to the server,
- using the client task
uploadmethod, - and process
postrequests from other application or services by using theon_ext_requestevent handler by the server task.