예제 #1
0
    def post(self):
        """
        add products and product details.
        :return:

            {"payload" : "Product details added successfully."}
        """
        try:
            data = request.data
            if type(data) == bytes:
                data = data.decode('utf-8')

            data = json.loads(data)

            product_name = str(data['product_name'])
            product_type = float(data['product_type'])

            new_product = Products(product_name=product_name,
                                   product_type=product_type)

            db_save(new_product)
            logger.info(cn.PRODUCT_ADDED_SUCC)
            return response_generator(cn.PRODUCT_ADDED_SUCC, status=201)

        except Exception as e:
            logger.error(cn.INTERNAL_SERV_ERR, exc_info=True)
            return response_generator(cn.INTERNAL_SERV_ERR, status=500)
예제 #2
0
    def post(self):
        """
        endpoint to save a config
        :arg:
            a json string {"template_id":"Template's ID", "template_name": "used template's name", "saved_name": "configs to be saved as", "created_by": "Name of creator", "cfg_json": {} }
        :param:
            template_id
            template_name
            saved_name
            created_by
            cfg_json
        :return:
            { "payload" : "Config added successfully" }
        """
        try:
            data = json.loads(request.data.decode('utf-8'))
            template_id = data["template_id"]
            template_name = data["template_name"]
            saved_name = data["saved_name"]
            created_by = data["created_by"]
            cfg_json = data["cfg_json"]

            new_config = Cfg(template_id=template_id,
                             template_name=template_name,
                             saved_name=saved_name,
                             created_by=created_by,
                             cfg_json=cfg_json)
            db_push(new_config)
            return response_generator(const.CONFIG_ADDED_SUCC,
                                      status.HTTP_201_CREATED)
        except Exception as ex:
            logger.error(const.INTERNAL_SERV_ERR, exc_info=True)
            db_rollback()
            return response_generator(const.INTERNAL_SERV_ERR,
                                      status.HTTP_500_INTERNAL_SERVER_ERROR)
예제 #3
0
    def put(self, config_name):
        """
        endpoint to update an existing config
        :param:
            config_name
        :return:
            { "payload" : "Config updated successfully" }
        """
        try:
            data = json.loads(request.data.decode('utf-8'))
            saved_name = data["saved_name"]
            created_by = data["created_by"]
            cfg_json = data["cfg_json"]

            new_config = Cfg(saved_name=saved_name,
                             created_by=created_by,
                             cfg_json=cfg_json)
            Cfg.query.filter(Cfg.config_name == config_name).update(new_config)

            db.session.commit()
            return response_generator(payload=const.CONFIG_UPDATED_SUCC,
                                      status=status.HTTP_201_CREATED)
        except Exception as ex:
            logger.error(const.INTERNAL_SERV_ERR, exc_info=True)
            db_rollback()
            return response_generator(const.INTERNAL_SERV_ERR,
                                      status.HTTP_500_INTERNAL_SERVER_ERROR)
예제 #4
0
    def put(self, template_id):
        """
        endpoint to update an existing template
        :param:
            template_id
        :return:
            { "payload" : "Template updated successfully" }
        """
        try:
            data = json.loads(request.data.decode('utf-8'))
            template_name = data["template_name"]
            created_by = data["created_by"]
            template_json = data["template"]

            new_template = Cfg_template(template_name=template_name,
                                        created_by=created_by,
                                        template=template_json)

            Cfg_template.query.filter(
                Cfg_template.template_id == template_id).update(new_template)

            db.session.commit()
            return response_generator(payload=const.TEMPLATE_UPDATED_SUCC,
                                      status=status.HTTP_201_CREATED)
        except Exception as ex:
            logger.error(const.INTERNAL_SERV_ERR, exc_info=True)
            db_rollback()
            return response_generator(const.INTERNAL_SERV_ERR,
                                      status.HTTP_500_INTERNAL_SERVER_ERROR)
예제 #5
0
    def post(self):
        """
        endpoint to save a template
        :arg:
            a json string {"template_name": "Name for template to be saved as", "created_by": "Name of template creator", "template": {} }
        :param:
            template_name
            created_by
            template
        :return:
            { "payload" : "Template added successfully" }
        """
        try:
            data = json.loads(request.data.decode('utf-8'))
            template_name = data["template_name"]
            created_by = data["created_by"]
            template_json = data["template"]

            new_template = Cfg_template(template_name=template_name,
                                        created_by=created_by,
                                        template=template_json)
            db_push(new_template)
            return response_generator(const.TEMPLATE_ADDED_SUCC,
                                      status.HTTP_201_CREATED)
        except Exception as ex:
            logger.error(const.INTERNAL_SERV_ERR, exc_info=True)
            db_rollback()
            return response_generator(const.INTERNAL_SERV_ERR,
                                      status.HTTP_500_INTERNAL_SERVER_ERROR)
예제 #6
0
def datetime_format(obj,format):
    if format == '%Y-%m-%d %H:%M:%S':
        datetime_output=datetime.datetime.fromtimestamp(obj).strftime('%Y-%m-%d %H:%M:%S')
    elif format == '%Y-%m-%dt%H-%M-%S':
        datetime_output=datetime.datetime.fromtimestamp(obj).strftime('%Y-%m-%dt%H-%M-%S')
    elif format == '%Y-%m':
        datetime_output=datetime.datetime.fromtimestamp(obj).strftime('%Y-%m')
    else:
        logger.error(cn.MSG_UNSUPPORTED_DATE_FORMAT, exc_info=True)
        return response_generator(cn.MSG_UNSUPPORTED_DATE_FORMAT, status.HTTP_400_BAD_REQUEST)

    return datetime_output
예제 #7
0
 def get(self):
     """
     endpoint to get a list of all templates with their template_id
     :return:
         { "payload" : [ {}, {}, {} ] }
     """
     try:
         return fetch_template_list(connection=connection)
     except Exception as ex:
         logger.error(const.INTERNAL_SERV_ERR, exc_info=True)
         return response_generator(const.INTERNAL_SERV_ERR,
                                   status.HTTP_500_INTERNAL_SERVER_ERROR)
예제 #8
0
 def get(self, config_name):
     """
     endpoint to get all configs against a saved config name
     :param:
         config's saved name
     :return:
         { "payload" : [ {}, {}, {} ] }
     """
     try:
         return fetch_config(connection=connection, config_name=config_name)
     except Exception as ex:
         logger.error(const.INTERNAL_SERV_ERR, exc_info=True)
         return response_generator(const.INTERNAL_SERV_ERR,
                                   status.HTTP_500_INTERNAL_SERVER_ERROR)
예제 #9
0
from src.instance.config import config
import src.misc.constants as cn
from src.misc.service_logger import serviceLogger as logger
from src.core.db_connection import DataBase

# flask application initialization
app = Flask(__name__)

# cross origin resource sharing
CORS(app)

# database connection
try:
    connection = DataBase().get()
except Exception as ex:
    logger.error(cn.DB_UNAVAILABLE, exc_info=True)
    print("Database unavailable")
    connection = None

# restplus swagger-ui
api = Api(app,
          version='1.0',
          title='flask-minimal-boilerplate',
          prefix='/api/v1',
          description='A boilerplate for flask-restful API services')

# Flask app configurations
app.config['SQLALCHEMY_BINDS'] = {'bind_db': config['database']['db_url']}
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['CORS_HEADERS'] = 'Content-Type'