from flask import request

from lexos.helpers.error_messages import NO_DATA_SENT_ERROR_MESSAGE, \
    INVALID_DATA_KEY_MESSAGE_FORMAT


class BaseModel:
    def __init__(self):
        """This is the base model for all the models

        used to handle requests and other common stuff
        """
        if request.json:  # check request.json (ajax request) first
            self._option = request.json
        elif request.form:  # fall back to request.form (form request)
            self._option = request.form
        else:  # no data send to the back end
            self._option = None

    def get_front_end_data(self, data_key: str):
        """get a data from request

        wrapped in a data to prevent bad request error
        :param data_key: the name (key) of the data you want to get
        :return: the value of the data
        """
        if not self._option:
            raise KeyError(NO_DATA_SENT_ERROR_MESSAGE)

        try:
            return self._option[data_key]
        except KeyError:
            raise KeyError(
                INVALID_DATA_KEY_MESSAGE_FORMAT.format(data_key=data_key))

@@ -1116,8 +1116,393 @@
 a_key))%0A
+%0A    def check_data_exists(self, data_key: str) -%3E bool:%0A        %22%22%22check whether the data key exists in the data sent from front end%0A%0A        :param data_key: the key of the data%0A        :return: whether that data key is in the request parameter.%0A        %22%22%22%0A        if not self._option:%0A            raise KeyError(NO_DATA_SENT_ERROR_MESSAGE)%0A%0A        return data_key in self._option%0A

add a new function to base model to check for whether a data is in request