Example #1
0
    def post(self, table):
        try:
            context = g.context

            if request.json == None:
                abort(
                    400,
                    "cannot extract json data in http request for insert %s" %
                    (table))

            fetch = build_fetchxml_by_alias(context,
                                            table,
                                            None,
                                            request.json,
                                            type="insert")
            fetchparser = FetchXmlParser(fetch, context)
            rs = DatabaseServices.exec(fetchparser, context, fetch_mode=0)
            result = {
                "rows_affected": rs.get_cursor().rowcount,
                "inserted_id": rs.get_inserted_id()
            }

            return result
        except RestApiNotAllowed as err:
            logger.exception(f"{err}")
            abort(400, f"{err}")
        except Exception as err:
            logger.exception(f"{err}")
            abort(500, f"{err}")
Example #2
0
def __ui_app_info(context):
    app_id = context.get_arg("app_id", 1)
    fetch = build_fetchxml_by_alias(context, "api_ui_app", app_id)
    fetchparser = FetchXmlParser(fetch, context)
    rs_app = DatabaseServices.exec(fetchparser,
                                   context,
                                   run_as_system=True,
                                   fetch_mode=1)

    fetch = f"""
    <restapi type="select">
        <table name="api_ui_app_nav_item" alias="a"/>
        <select>
            <field name="id" table_alias="a"/>
            <field name="name" table_alias="a"/>
            <field name="url" table_alias="a"/>
            <field name="type_id" table_alias="a"/>
        </select>

        <filter type="and">
            <condition field="app_id" value="{app_id}" operator="="/>
        </filter>
    </restapi>
    """
    fetchparser = FetchXmlParser(fetch, context)
    rs_items = DatabaseServices.exec(fetchparser,
                                     context,
                                     run_as_system=True,
                                     fetch_mode=0)

    return (rs_app, rs_items)
Example #3
0
    def post(self, table):
        try:
            context = g.context

            json = request.form.to_dict()

            fetch = build_fetchxml_by_alias(context,
                                            table,
                                            None,
                                            json,
                                            type="insert")
            fetchparser = FetchXmlParser(fetch, context)
            rs = DatabaseServices.exec(fetchparser, context, fetch_mode=0)
            result = {
                "rows_affected": rs.get_cursor().rowcount,
                "inserted_id": rs.get_inserted_id()
            }

            next = HTTPRequest.redirect(
                request,
                default=f"/ui/v1.0/data/{table}/{rs.get_inserted_id()}",
                id=rs.get_inserted_id())

            return redirect(next, code=302)

        except RestApiNotAllowed as err:
            logger.info(f"RestApiNotAllowed Exception: {err}")
            return redirect(f"/ui/login?redirect=/ui/v1.0/data/{table}",
                            code=302)
        except Exception as err:
            return make_response(
                JinjaTemplate.render_status_template(context, 500, err), 500)
Example #4
0
    def get(self, table, id):
        try:
            create_parser().parse_args()
            context = g.context

            table_meta = read_table_meta(context, alias=table)
            solution_id = table_meta['solution_id']

            if solution_id == 1:
                file = f"templates/{table}_update.htm"
            else:
                file = f"solutions/{solution_id}/{table}_update.htm"

            fetch = build_fetchxml_by_alias(context, table, id, type="select")
            fetchparser = FetchXmlParser(fetch, context)
            rs = DatabaseServices.exec(fetchparser, context, fetch_mode=1)
            if rs.get_result() == None:
                abort(404, "Item not found => %s" % id)
            #
            # render the defined jinja template (in case ofahtm file)
            #
            logger.info(f"Redirect : {next}")

            template = JinjaTemplate.create_file_template(context, file)
            response = make_response(
                template.render({
                    "table": table,
                    "pagemode": "dataformupdate",
                    "id": id,
                    "data": rs.get_result(),
                    "context": context
                }))
            response.headers['content-type'] = 'text/html'

            return response

        except ConfigNotValid as err:
            logger.exception(f"Config not valid {err}")
            return make_response(
                JinjaTemplate.render_status_template(context, 500, err), 500)
        except jinja2.exceptions.TemplateNotFound as err:
            logger.exception(f"TemplateNotFound: {err}")
            return make_response(
                JinjaTemplate.render_status_template(
                    context, 404, f"Template not found {err}"), 404)
        except FileNotFoundError as err:
            logger.exception(f"FileNotFound: {err}")
            return make_response(
                JinjaTemplate.render_status_template(context, 404,
                                                     f"File not found {err}"),
                404)
        except RestApiNotAllowed as err:
            logger.exception(f"RestApiNotAllowed Exception: {err}")
            #return redirect(f"/auth/login.htm?redirect=/{path}", code=302)
            return redirect(f"/ui/login?redirect={request.url}", code=302)
        except Exception as err:
            logger.exception(f"Exception: {err}")
            return make_response(
                JinjaTemplate.render_status_template(context, 500, err), 500)
Example #5
0
 def delete(self,table,id):
     try:
         create_parser_delete().parse_args()
         context=g.context
         fetch=build_fetchxml_by_alias(context,table,id, type="delete")
         fetchparser=FetchXmlParser(fetch, context)
         rs=DatabaseServices.exec(fetchparser,context, fetch_mode=0)
         result={"rows_affected": rs.get_cursor().rowcount}
         rs.close()
         return result
     except RestApiNotAllowed as err:
         abort(400, f"{err}")
     except Exception as err:
         abort(500,f"{err}")
Example #6
0
 def get(self, table):
     try:
         create_parser().parse_args()
         context = g.context
         fetch = build_fetchxml_by_alias(context,
                                         table,
                                         None,
                                         None,
                                         type="select")
         fetchparser = FetchXmlParser(fetch, context)
         rs = DatabaseServices.exec(fetchparser, context, fetch_mode=0)
         return rs.get_result()
     except RestApiNotAllowed as err:
         logger.info(f"{err}")
         abort(400, f"{err}")
     except Exception as err:
         abort(500, f"{err}")
Example #7
0
    def put(self,table,id):
        try:
            create_parser_put().parse_args()
            context=g.context

            if request.json==None:
                abort(400, "cannot extract json data in body %s %s" % (table,id))

            fetch=build_fetchxml_by_alias(context, table, id, request.json, type="update")
            fetchparser=FetchXmlParser(fetch, context)
            rs=DatabaseServices.exec(fetchparser,context, fetch_mode=0)
            result={"rows_affected": rs.get_cursor().rowcount}
            rs.close()
            return result
        except RestApiNotAllowed as err:
            abort(400, f"{err}")
        except Exception as err:
            abort(500,f"{err}")
Example #8
0
    def get(self,table,id,field=""):
        try:
            create_parser_get().parse_args()
            context=g.context

            fetch=build_fetchxml_by_alias(context, table, id, type="select")
            fetchparser=FetchXmlParser(fetch, context)
            rs=DatabaseServices.exec(fetchparser,context, fetch_mode=1)
            if rs.get_result()==None:
                abort(400, "Item not found => %s" % id)

            if field=="":
                return rs.get_result()
            else:
                return rs.get_result()[field]

        except RestApiNotAllowed as err:
            abort(400, f"{err}")
        except Exception as err:
            abort(500,f"{err}")
Example #9
0
    def post(self,table,id):
        try:
            create_parser_post().parse_args()
            context=g.context


            json=request.form.to_dict()
            for key in list(json):
                if key.startswith("__"):
                    del json[key]

            fetch=build_fetchxml_by_alias(context, table, id, json, type="update")
            fetchparser=FetchXmlParser(fetch, context)
            rs=DatabaseServices.exec(fetchparser,context, fetch_mode=0)

            next=HTTPRequest.redirect(request, default=f"/ui/v1.0/data/{table}/$$id$$", id=id)

            return redirect(next, code=302)

        except RestApiNotAllowed as err:
            logger.info(f"RestApiNotAllowed Exception: {err}")
            return redirect(f"/ui/login?redirect=/ui/v1.0/data/{table}/{id}", code=302)
        except Exception as err:
           return make_response(JinjaTemplate.render_status_template(500, err), 500)
Example #10
0
    def get(self, path="index.htm", session_id=None, content_name=None):
        try:
            logger.info(f"Path: {path}")
            logger.info(f"content_name: {content_name}")
            logger.info(f"session_id: {session_id}")

            create_parser().parse_args()
            context = g.context

            portal_id = "default"
            #
            # content class
            #
            if not path.endswith("login.htm"):
                pass

            #
            # render the defined jinja template (in case ofahtm file)
            #
            if path.endswith('.htm'):
                next = HTTPRequest.redirect(request)
                logger.info(f"Redirect : {next}")

                fetch = build_fetchxml_by_alias(context,
                                                "api_portal",
                                                portal_id,
                                                type="select")
                fetchparser = FetchXmlParser(fetch, context)
                rs = DatabaseServices.exec(fetchparser, context, fetch_mode=1)
                if rs.get_result() == None:
                    abort(404, "Portal not found => %s" % id)

                # render the content first
                if content_name == None:
                    content_name = HTTPRequest.get_querystring_value(
                        request, "content_name", default="home")

                content = self.__get_content(context, portal_id, content_name)
                if content == None:
                    abort(404, "Content not found => %s" % content_name)

                params = {
                    "context": context,
                    "content_name": content_name,
                    "content_id": content['id']
                }

                template = JinjaTemplate.create_string_template(
                    context,
                    content['content'].encode('utf-8').decode('utf-8'))
                content_str = template.render(params)

                #
                # Render the complete page
                #
                params['content'] = content_str
                template = JinjaTemplate.create_file_template(context, path)
                page_str = template.render(params)

                response = make_response(page_str)
                response.headers['content-type'] = 'text/html'

                return response
            else:
                www_root = FileSystemTools.format_path(
                    AppInfo.get_current_config('ui', 'wwwroot',
                                               exception=True))
                return send_file(f"{www_root}{path}")

        except ConfigNotValid as err:
            logger.exception(f"Config not valid {err}")
            return make_response(
                JinjaTemplate.render_status_template(context, 500, err), 500)
        except jinja2.exceptions.TemplateNotFound as err:
            logger.exception(f"TemplateNotFound: {err}")
            return make_response(
                JinjaTemplate.render_status_template(
                    context, 404, f"Template not found {err}"), 404)
        except FileNotFoundError as err:
            logger.exception(f"FileNotFound: {err}")
            return make_response(
                JinjaTemplate.render_status_template(context, 404,
                                                     f"File not found {err}"),
                404)
        except RestApiNotAllowed as err:
            logger.exception(f"RestApiNotAllowed Exception: {err}")
            return redirect(f"/ui/login?redirect=/{path}", code=302)
        except Exception as err:
            logger.exception(f"Exception: {err}")
            return make_response(
                JinjaTemplate.render_status_template(context, 500, err), 500)
Example #11
0
    def test_fetch_by_alias(self):
        by_alias=build_fetchxml_by_alias(self.context,"dummy",id=None,data={"port":"3306"},auto_commit=0, type="insert")

        print("====================")
        print(by_alias)
        print("====================")