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)
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)
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)
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)
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)
def fetch_template(connection, template_id): cur = connection.cursor(cursor_factory=RealDictCursor) try: sql_query = fetch_template_query(template_id) cur.execute(sql_query) res = cur.fetchall() return response_generator(payload=res, status=status.HTTP_200_OK) except Exception as ex: connection.rollback() return response_generator(const.MSG_NO_DATA_FOUND, status.HTTP_404_NOT_FOUND) finally: cur.close()
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
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)
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)