# Buggy code
def put(self, name):
    """Learning new predictor"""
    global model_swapping_map

    data = request.json
    to_predict = data.get("to_predict")

    try:
        kwargs = data.get("kwargs")
    except:
        kwargs = None

    if type(kwargs) != type({}):
        kwargs = {}

    if "stop_training_in_x_seconds" not in kwargs:
        kwargs["stop_training_in_x_seconds"] = 100

    if "equal_accuracy_for_all_output_categories" not in kwargs:
        kwargs["equal_accuracy_for_all_output_categories"] = True

    if "sample_margin_of_error" not in kwargs:
        kwargs["sample_margin_of_error"] = 0.005

    if "unstable_parameters_dict" not in kwargs:
        kwargs["unstable_parameters_dict"] = {}

    if "use_selfaware_model" not in kwargs["unstable_parameters_dict"]:
        kwargs["unstable_parameters_dict"]["use_selfaware_model"] = False

    try:
        retrain = data.get("retrain")
        if retrain in ("true", "True"):
            retrain = True
        else:
            retrain = False
    except:
        retrain = None

    ds_name = (
        data.get("data_source_name")
        if data.get("data_source_name") is not None
        else data.get("from_data")
    )
    from_data = g.default_store.get_datasource_obj(ds_name)

    if retrain is True:
        original_name = name
        name = name + "_retrained"

    g.mindsdb_native.learn(name, from_data, to_predict, kwargs)

    if retrain is True:
        try:
            model_swapping_map[original_name] = True
            g.mindsdb_native.delete_model(original_name)
            g.mindsdb_native.rename_model(name, original_name)
            model_swapping_map[original_name] = False
        except:
            model_swapping_map[original_name] = False

    return "", 200


# Traceback (AttributeError)
2020-06-25 14:30:32,318 - INFO - Starting MindsDB Mysql proxy server on tcp://127.0.0.1:47335
2020-06-25 14:30:32,334 - INFO - Waiting for incoming connections...
127.0.0.1 - - [25/Jun/2020 14:31:14] "GET /util/ping HTTP/1.1" 200 -
Exception on /datasources/ [GET]
Traceback (most recent call last):
File "/home/zoran/MyProjects/mindsdb-examples/new/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/home/zoran/MyProjects/mindsdb-examples/new/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/zoran/MyProjects/mindsdb-examples/new/lib/python3.7/site-packages/flask_restx/api.py", line 375, in wrapper
resp = resource(*args, **kwargs)
File "/home/zoran/MyProjects/mindsdb-examples/new/lib/python3.7/site-packages/flask/views.py", line 89, in view
return self.dispatch_request(*args, **kwargs)
File "/home/zoran/MyProjects/mindsdb-examples/new/lib/python3.7/site-packages/flask_restx/resource.py", line 44, in dispatch_request
resp = meth(*args, **kwargs)
File "/home/zoran/MyProjects/mindsdb-examples/new/lib/python3.7/site-packages/flask_restx/marshalling.py", line 248, in wrapper
resp = f(*args, **kwargs)
File "/home/zoran/MyProjects/mindsdb-examples/new/lib/python3.7/site-packages/mindsdb/api/http/namespaces/datasource.py", line 40, in get
return g.default_store.get_datasources()
File "/home/zoran/MyProjects/mindsdb-examples/new/lib/python3.7/site-packages/werkzeug/local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
AttributeError: '_AppCtxGlobals' object has no attribute 'default_store'
127.0.0.1 - - [25/Jun/2020 14:31:18] "GET /datasources/ HTTP/1.1" 500 -
Exception on /datasources/ [GET]

# Fixed code
def put(self, name):
    """Learning new predictor"""
    global model_swapping_map

    data = request.json
    to_predict = data.get("to_predict")

    try:
        kwargs = data.get("kwargs")
    except:
        kwargs = None

    if type(kwargs) != type({}):
        kwargs = {}

    if "stop_training_in_x_seconds" not in kwargs:
        kwargs["stop_training_in_x_seconds"] = 100

    if "equal_accuracy_for_all_output_categories" not in kwargs:
        kwargs["equal_accuracy_for_all_output_categories"] = True

    if "sample_margin_of_error" not in kwargs:
        kwargs["sample_margin_of_error"] = 0.005

    if "unstable_parameters_dict" not in kwargs:
        kwargs["unstable_parameters_dict"] = {}

    if "use_selfaware_model" not in kwargs["unstable_parameters_dict"]:
        kwargs["unstable_parameters_dict"]["use_selfaware_model"] = False

    try:
        retrain = data.get("retrain")
        if retrain in ("true", "True"):
            retrain = True
        else:
            retrain = False
    except:
        retrain = None

    ds_name = (
        data.get("data_source_name")
        if data.get("data_source_name") is not None
        else data.get("from_data")
    )
    from_data = ca.default_store.get_datasource_obj(ds_name)

    if retrain is True:
        original_name = name
        name = name + "_retrained"

    ca.mindsdb_native.learn(name, from_data, to_predict, kwargs)

    if retrain is True:
        try:
            model_swapping_map[original_name] = True
            ca.mindsdb_native.delete_model(original_name)
            ca.mindsdb_native.rename_model(name, original_name)
            model_swapping_map[original_name] = False
        except:
            model_swapping_map[original_name] = False

    return "", 200