Rest Functions¶
-
class
pumpwood_viewutils.rest_function.
RestAction
(fn, model_class, icon, description, validation_dict, static, parameters, opt_parameters, return_dict, request_user=None)¶ Class responsible for keeping the instructions to expose an action as a rest service. This class is also used to keep information about the function that will be exposed so it can be retrived and passed to front end when it is serialized.
It is used inside @rest_function wrapper.
Parameters: - fn (function) – Function to extract name and class
- model_class (str) – Name of the model of the function that will be exposed
- icon (str) – Path to the icon of the function.
- description (str) – String to describe action.
- validation_dict (dict) – A dictonary that indicates validations that will be performed when data is received
- static (bool) – Tell if the function is static and not associated to an specific object
- parameters (dict) – A dictionary containing the description of the parameters necessary to run the fucntion
- opt_parameters (dict) – A dictionary containing the description of the optional parameters to run the fucntion
- return_dict (dict) – A dictionary containing the description of what the function will return
- request_user (string) – A string telling if the request user will be used as as argument in function. None no, a string name of the kwargs key that will hold user name
-
class
pumpwood_viewutils.rest_function.
RestFunctionSerializer
¶ Wraps SerializerDictionary and taget function to run the function and validates if all necessary parameters were passed
Parameters: - function (function) – Function that will be used to process request income data
- request – Income Django request
Returns: Function results in serializable simples Python objects (int, list, dict, ...)
-
static
fun_args_parser
(action_obj, request)¶ Uses function action_obj to transform request.data so it can be used to run the function
Parameters: - RestAction (action_obj) – action_obj associated with the function by rest_function wrapper
- request – Inconme Django request
Returns: a dictionary of the function kwargs
Return type: dict
-
static
result_parser
(action_obj, results)¶ Uses function action_obj to transform function results to python serializible simple functions
Parameters: - RestAction (action_obj) – action_obj associated with the function by rest_function wrapper
- request – Inconme Django request
Returns: function result in serializible objects (int, list, dict, ...)
-
class
pumpwood_viewutils.rest_function.
SerializerDictionary
¶ Class used to store how the function variables will be serialized.
-
classmethod
add_serializer_in
(var_type, one_lambda, many_lambda)¶ Add a receiving data serializer
Parameters: - str (var_type) – String corresponding to a type that will be later used in @rest_function parameters or opt_parameters definition
- function (many_lambda) – Function defining how the income data will be converted to Python simple or complex (Model Objects, pandas.DataFrame, ...), when data is not many (list)
- function – Function defining how the income data will be converted to Python simple objects (list, dict, int, ...)
Returns: None
-
classmethod
add_serializer_out
(var_type, one_lambda, many_lambda)¶ Add a sending data serializer
Parameters: - str (var_type) – String corresponding to a type that will be later used in @rest_function parameters or opt_parameters definition
- function (many_lambda) – Function defining how the sending data will be converted to Python simple or complex (Model Objects, pandas.DataFrame, ...), when data is not many (list)
- function – Function defining how the sending data will be converted to Python simple objects (list, dict, int, ...)
Returns: None
-
classmethod
get_in_serializer
(var_type)¶ Check if in serializer is avaible for var_type
Parameters: str (var_type) – String corresponding to variable type Returns: var_type in serializer Raises: PumpWoodException – If in serializer for var_type have not been added
-
classmethod
get_out_serializer
(var_type)¶ Check if out serializer is avaible for var_type
Parameters: str (var_type) – String corresponding to variable type Returns: var_type out serializer Raises: PumpWoodException – If out serializer for var_type have not been added
-
classmethod
serialize_in
(var_type, data)¶ Serialize income data acording to its var_type
Parameters: - str (var_type) – var_type of the income data
- variable (data) – Data to be serialized
Returns: Data serialized acording to __serializer_in_dict[var_type]
-
classmethod
serialize_out
(var_type, data)¶ Serialize sending data acording to its var_type
Parameters: - str (var_type) – var_type of the sending data
- variable (data) – Data to be serialized
Returns: Data serialized acording to __serializer_out_dict[var_type]
-
classmethod
-
pumpwood_viewutils.rest_function.
rest_function
(model_class, icon, description, validation_dict, static, parameters, opt_parameters, return_dict, request_user=None)¶ Function Wrapper defining rest caracteristics to be used for income data a
Usage:
class ExampleModel(models.Model): ... @rest_function(model_class='ExampleModel' , icon='example_icon' , description='A great description for a example function' , validation_dict={} , static=False , parameters={ 'arg1': {'type': 'dict', 'many': False} , 'arg2': {'type': 'int', 'many': True} , 'arg3': {'type': 'PandasDataframe', 'many': True} } , opt_parameters={ 'arg4': {'type': 'bool', 'many': False} , 'arg5': {'type': 'int', 'many': False} , 'arg6': {'type': 'str', 'many': True} } , return_dict={ 'return1': {'type': 'int', 'many': False} , 'return2': {'type': 'str', 'many': False} , 'return3': {'type': 'bool', 'many': True} } , request_user='user') def example_function(self, user, arg1, arg2, arg3, arg4=False, arg5=5, arg6=['apple', 'orange', 'pineapple']): ... return {'return1': 1, 'return2': 'Chubaca', 'return3': [True, False, True, True]} @rest_function(model_class='ExampleModel' , icon='example_icon' , description='A great description for a example function' , validation_dict={} , static=False , parameters={ 'arg1': {'type': 'str', 'many': False} } , opt_parameters={} , return_dict={ 'flat': {'type': 'pandas.DataFrame', 'many': True} } , request_user=None) @classmethod def example_cls_function(cls, arg1): ... return [ pandas.DataFrame(data_set1), pandas.DataFrame(data_set2) ]
Parameters: - RestAction (action_obj) – action_obj associated with the function by rest_function wrapper
- request – Inconme Django request
Returns: function result in serializible objects (int, list, dict, ...)
Raises: - par_dict –
- KeyError – Transformation dict {error_name} entry {par_dict_key} does not have type and many keys’ if parameter dict does not have type and many keys
- KeyError – Transformation dict {error_name} entry {par_dict_key} does not have type and many keys’
- TypeError –
- Exception –