def wrap__resource__call_method(modelname, ids, method, method_params, success_code): """Function to call the model method for records by IDs. :param str modelname: The name of the model. :param list ids: The record ids of which we want to call method. :param str method: The name of the method. :param int success_code: The success code. :returns: successful response if the method execution did not cause an error otherwise error response :rtype: werkzeug.wrappers.Response """ model_obj = get_model_for_read(modelname) if not hasattr(model_obj, method): return error_response(*CODE__invalid_method) records = model_obj.browse(ids).exists() results = [] args = method_params.get("args", []) kwargs = method_params.get("kwargs", {}) for record in records or [model_obj]: result = getattr(record, method)(*args, **kwargs) results.append(result) if len(ids) <= 1 and len(results): results = results[0] model_obj.flush() # to recompute fields return successful_response(success_code, data=results)
def get_dict_from_model(model, spec, id, **kwargs): """Fetch dictionary from one record according to spec. :param str model: The model against which to validate. :param tuple spec: The spec to validate. :param int id: The id of the record. :param dict kwargs: Keyword arguments. :param tuple kwargs['include_fields']: The extra fields. This parameter is not implemented on higher level code in order to serve as a soft ACL implementation on top of the framework's own ACL. :param tuple kwargs['exclude_fields']: The excluded fields. :returns: The python dictionary of the requested values. :rtype: dict :raise: werkzeug.exceptions.HTTPException if the record does not exist. """ include_fields = kwargs.get( "include_fields", ()) # Not actually implemented on higher level (ACL!) exclude_fields = kwargs.get("exclude_fields", ()) model_obj = get_model_for_read(model) record = model_obj.browse([id]) if not record.exists(): raise werkzeug.exceptions.HTTPException(response=error_response( *CODE__res_not_found)) return get_dict_from_record(record, spec, include_fields, exclude_fields)
def wrap__resource__create_one(modelname, context, data, success_code, out_fields): """Function to create one record. :param str model: The name of the model. :param dict context: TODO :param dict data: Data received from the user. :param int success_code: The success code. :param tuple out_fields: Canned fields. :returns: successful response if the create operation is performed otherwise error response :rtype: werkzeug.wrappers.Response """ model_obj = get_model_for_read(modelname) try: created_obj = model_obj.with_context(context).create(data) test_mode = request.registry.test_cr if not test_mode: # Somehow don't making a commit here may lead to error # "Record does not exist or has been deleted" # Probably, Odoo (10.0 at least) uses different cursors # to create and to read fields from database request.env.cr.commit() except Exception as e: return error_response(400, type(e).__name__, str(e)) out_data = get_dict_from_record(created_obj, out_fields, (), ()) return successful_response(success_code, out_data)