Esempio n. 1
0
def get_coll(source_id):
    import os
    import importlib
    source_name = encryptor.get_value(source_id)
    dir = os.path.dirname(app.mdl.__file__)
    model_path = dir + os.sep + "models" + os.sep + source_name + ".py"
    if not os.path.isfile(model_path):
        dir = os.path.dirname(app.mdl.__file__)
        dir = dir + os.sep + "models"
        tem_file = dir + os.sep + "model.txt"
        file = open(tem_file, "r")
        txt = file.read()
        file.close()
        txt_content = txt.replace("@model_name", source_name)
        file_code = open(model_path, "w")
        file_code.write(txt_content)
        file_code.close()

    coll = importlib.import_module(app.mdl.__name__ + ".models." + source_name)
    return getattr(coll, source_name)
Esempio n. 2
0
def create_data_combobox(args):
    #Hàm get dropdown list theo tên collection và tên cột
    try:
        global __collectionName
        global __collection

        ret      = {}
        ret_list = []

        if (args['data'] != None) or (not args['data'].has_key('key')) or (args['data']['key'] == ""):

            combobox_code = encryptor.get_value(args['data']['key'])

            combobox_info = qmongo.models.HCSSYS_ComboboxList.aggregate.project(
                combobox_code       =   1,
                language            =   1,
                display_name        =   1,
                description         =   1,
                table_name          =   1,
                table_fields        =   1,
                display_fields      =   1,
                predicate           =   1,
                value_field         =   1,
                caption_field       =   1,
                sorting_field       =   1,
                filter_field        =   1,
                multi_select        =   1,
                parent_field        =   1,
                page_size           =   1)

            language_code = quicky.language.get_language()
            combobox_info = combobox_info.match("combobox_code == {0} and language == {1}", combobox_code, language_code).get_item()

            if combobox_info == None:
                return {"error":"combobox not defined"}

            __collectionName = combobox_info['table_name']

            try:
                __collection = getattr(models, __collectionName)()
            except Exception as ex:
                return {"error":"Not found collection name"}

            column       = combobox_info['table_fields']
            where        = combobox_info['predicate']
            sort         = combobox_info['sorting_field']
            parent_field = combobox_info['parent_field']
            filter    = []
            page_size = 30

            if combobox_info.has_key("filter_field"):
                filter = combobox_info['filter_field']

            if combobox_info.has_key("page_size"):
                page_size = combobox_info['page_size']

            if column != []:
                try:
                    dict_column = dict()
                    for x in column:
                        dict_column.update({x:1})
                    if where.has_key("column") and (where.get('column', None) != None and len(where.get('column', None)) > 0):
                        for x in where['column']:
                            dict_column.update({x:1})
                    ret = __collection.aggregate().project(dict_column)
                except Exception as ex:
                    raise(Exception("column not exist in collection"))
            else:
                raise(Exception("Not found column name"))

            #predicate
            if where.has_key('operator') and \
               (where.get('column', None) != None and\
               len(where.get('column', None)) > 0) and\
               where.get('operator', '') != "":
                try:
                    if args['data'].has_key('value') and args['data'].get('value', None) != None and type(args['data']['value']) is list:
                        dict_where = dict()
                        for x in args['data']['value']:
                            new_key    = x.keys()[0].replace("@", "")
                            old_key    = x.keys()[0]
                            x[new_key] = x.pop(old_key)
                            dict_where.update(x)

                        ret.match(where['operator'], dict_where)
                        if args['data'].has_key('code') and args['data'].get('code', None) != None and args['data']['code'] != "":
                            ret.match(combobox_info['value_field'] + " == {0}", args['data']['code'])
                    else:
                        ret.match(where['operator'])

                except Exception as ex:
                    raise(Exception("syntax where error"))

            #filter with text from browser
            if args['data'].has_key('search') and args['data'].get('search', None) != None and args['data']['search'] != "":
                if len(filter) > 0:
                    filter_query = ""
                    filter_dict = dict()
                    for i in range(len(filter)):
                        if i == (len(filter) - 1):
                            filter_query += "contains(" + filter[i] + ", " + "@" + filter[i] + ")"
                        else:
                            filter_query += "contains(" + filter[i] + ", " + "@" + filter[i] + ") or "
                        filter_dict.update({filter[i].format() : args['data']['search']})

                    ret.match(filter_query, filter_dict)

            if sort != {}:
                try:
                    ret.sort(sort)
                except Exception as ex:
                    raise(Exception("syntax sort error"))

            #Pagination
            page_index = 0
            data = dict()
            if args['data'].has_key('pageIndex') and args['data'].get('pageIndex', None) != None:
                page_index = args['data']['pageIndex']

            if parent_field == None or parent_field == "":
                #delete pagination
                #data = ret.get_page(page_index, page_size)
                result = ret.get_list()
                data = {
                    "items": result,
                    "total_items": len(result),
                    "page_index": page_index,
                    "page_size": page_size,
                    "note": "current version not support pagination"
                }
            else:
                result = ret.get_list()
                data = {
                    "items":result,
                    "total_items": len(result),
                    "page_index": 0,
                    "page_size": 100,
                    "note": "tree combobox not using pagination"
                    }

            if data != {}:
                for y in where['column']:
                    for x in data['items']:
                        if y not in data['items'][0].keys():
                            del x[y.format()]

            ret_list = data

            return {"data"           : ret_list,
                    "display_name"   : combobox_info["display_name"],
                    "display_fields" : combobox_info["display_fields"],
                    "value_field"    : combobox_info["value_field"],
                    "caption_field"  : combobox_info["caption_field"],
                    "sorting_field"  : combobox_info["sorting_field"],
                    "filter_field"   : combobox_info["filter_field"],
                    "page_size"      : combobox_info["page_size"],
                    "parent_field"   : combobox_info["parent_field"],
                    "error"          : None}
        raise(Exception("Not found collection name"))

    except Exception as ex:
        logger.debug(ex)
        return {"data": None, "error": ex.message}
Esempio n. 3
0
def get_init_data_combobox(args):
    #Hàm get data init cho combobox
    if args['data'] != None:
        result = []
        if args['data'].has_key('name'):
            list_name = args["data"].get("name", None)
            if list_name != None:
                try:
                    if type(list_name) is list:
                        for x in list_name:
                            result.append({
                                "key":x['key'],
                                "value":None,
                                "predicate": (lambda x: x['predicate'] if x.has_key('predicate') else None)(x),
                                "code":x['code'],
                                "name":encryptor.get_value(x['key']),
                                "display_fields":[],
                                "caption_field":"",
                                "value_field": "",
                                "multi_select": False,
                                "parent_field":"",
                                "alias": (lambda x: x['alias'] if x.has_key('alias') else "")(x)
                                })
                        for x in result:
                            display_fields = [[]]
                            caption_field = [[]]
                            value_field = [[]]
                            multi_select = [[]]
                            parent_field = [[]]
                            x['value'] = create_data_init_combobox(dict(data = {"key":x["key"], "code":x["code"], "value":x["predicate"]}), display_fields, caption_field, value_field, multi_select, parent_field)
                            x['display_fields'] = display_fields[0]
                            x['caption_field'] = caption_field[0]
                            x['value_field'] = value_field[0]
                            x['multi_select'] = multi_select[0]
                            x['parent_field'] = parent_field[0]

                        return result
                    else:
                        display_fields = [[]]
                        caption_field = [[]]
                        value_field = [[]]
                        multi_select = [[]]
                        parent_field = [[]]
                        return {
                            "key": args["data"].get("name", "")["key"],
                            "value": create_data_init_combobox(dict(data = {"key":args["data"].get("name", "")["key"], "code":args["data"].get("name", "")["code"], "value":args["data"].get("name", "").get("predicate", None)}), display_fields, caption_field, value_field, multi_select, parent_field),
                            "code": args["data"].get("name", "")["code"],
                            "name":encryptor.get_value(args["data"].get("name", "")['key']),
                            "display_fields": display_fields[0],
                            "caption_field": caption_field[0],
                            "value_field": value_field[0],
                            "multi_select": multi_select[0],
                            "parent_field": parent_field[0],
                            "alias":(lambda x: x['alias'] if x.has_key("alias") else "")(args["data"].get("name", ""))
                            }
                except Exception as ex:
                    logger.debug(ex)
                    return {"value": None, "error": ex.message}
            else:
                return {
                    "error":"name is not empty"
                    }
Esempio n. 4
0
def create_data_init_combobox(args, display_field, caption_field, value_field, multi_select, parent_field):
    try:
        global __collectionName
        global __collection

        ret      = {}
        ret_list = []

        if (args['data'] != None) or (not args['data'].has_key('key')) or (args['data']['key'] == ""):

            combobox_code = encryptor.get_value(args['data']['key'])

            combobox_info = qmongo.models.HCSSYS_ComboboxList.aggregate.project(
                combobox_code       =   1,
                language            =   1,
                display_name        =   1,
                description         =   1,
                table_name          =   1,
                table_fields        =   1,
                display_fields      =   1,
                parent_field        =   1,
                predicate           =   1,
                value_field         =   1,
                caption_field       =   1,
                sorting_field       =   1,
                filter_field        =   1,
                multi_select        =   1,
                page_size           =   1)

            language_code = quicky.language.get_language()
            combobox_info = combobox_info.match("combobox_code == {0} and language == {1}", combobox_code, language_code).get_item()

            if combobox_info == None:
                return {"error":"combobox not defined"}

            __collectionName = combobox_info['table_name']
            display_field[0] = combobox_info['display_fields']
            caption_field[0] = combobox_info['caption_field']
            value_field[0] = combobox_info['value_field']
            multi_select[0] = combobox_info['multi_select']
            parent_field[0] = combobox_info['parent_field']

            try:
                __collection = getattr(models, __collectionName)()
            except Exception as ex:
                return {"error":"Not found collection name"}



            column    = combobox_info['table_fields']
            where     = combobox_info['predicate']
            multi     = combobox_info['multi_select']

            if column != []:
                try:
                    dict_column = dict()
                    for x in column:
                        dict_column.update({x:1})
                    ret = __collection.aggregate().project(dict_column)
                except Exception as ex:
                    raise(Exception("column not exist in collection"))
            else:
                raise(Exception("Not found column name"))

            if args['data'].has_key('code'):
                if multi == True:
                    ret.match(combobox_info['value_field'] + " in {0}", args['data']['code'])
                else:
                    ret.match(combobox_info['value_field'] + " == {0}", args['data']['code'])

            #predicate
            if where.has_key('operator') and \
               (where.get('column', None) != None and\
               len(where.get('column', None)) > 0) and\
               where.get('operator', '') != "":
                try:
                    if args['data'].has_key('value') and args['data'].get('value', None) != None and type(args['data']['value']) is list:
                        dict_where = dict()
                        for x in args['data']['value']:
                            new_key    = x.keys()[0].replace("@", "")
                            old_key    = x.keys()[0]
                            x[new_key] = x.pop(old_key)
                            dict_where.update(x)

                        ret.match(where['operator'], dict_where)
                    #else:
                    #    ret.match(where['operator'])

                except Exception as ex:
                    raise(Exception("syntax where error"))

            if multi == True:
                return ret.get_list()
            else:
                return ret.get_item()

        raise(Exception("Not found collection name"))

    except Exception as ex:
        logger.debug(ex)
        return {"data": None, "error": ex.message}
Esempio n. 5
0
def get_dropdown_list(args):
    #Hàm get dropdown list theo tên collection và tên cột
    try:
        global __collectionName 
        global __collection 

        ret      = {}
        ret_list = []

        if (args['data'] != None) or (not args['data'].has_key('key')) or (args['data']['key'] == ""):

            combobox_code = encryptor.get_value(args['data']['key'])

            combobox_info = models.HCSSYS_ComboboxList().aggregate().project(
                combobox_code       =   1,
                language            =   1,
                display_name        =   1,
                description         =   1,
                table_name          =   1,
                table_fields        =   1,
                display_fields      =   1,
                predicate           =   1,
                value_field         =   1,
                value_custom        =   1,
                caption_field       =   1,
                sorting_field       =   1,
                filter_field        =   1,
                multi_select        =   1,
                page_size           =   1)

            language_code = quicky.applications.get_settings().LANGUAGE_CODE
            combobox_info = combobox_info.match("combobox_code == {0} and language == {1}", combobox_code, language_code).get_item()

            __collectionName = combobox_info['table_name']

            try:
                __collection = getattr(models, __collectionName)()
            except Exception as ex:
                return {"error":"Not found collection name"}

            column    = combobox_info['table_fields']
            where     = combobox_info['predicate']
            sort      = combobox_info['sorting_field']
            filter    = []
            page_size = 30

            if combobox_info.has_key("filter_field"):
                filter = combobox_info['filter_field']

            if combobox_info.has_key("page_size"):
                page_size = combobox_info['page_size']

            if column != []:
                try:
                    dict_column = dict()
                    for x in column:
                        dict_column.update({x:1})
                    if where.has_key("column") and (where.get('column', None) != None and len(where.get('column', None)) > 0):
                        for x in where['column']:
                            dict_column.update({x:1})
                    ret = __collection.aggregate().project(dict_column)
                except Exception as ex:
                    raise(Exception("column not exist in collection"))
            else:
                raise(Exception("Not found column name"))

            #predicate
            if where.has_key('operator') and \
               (where.get('column', None) != None and\
               len(where.get('column', None)) > 0) and\
               where.get('operator', '') != "":
                try:
                    if args['data'].has_key('value') and args['data'].get('value', None) != None and type(args['data']['value']) is list:
                        dict_where = dict()
                        for x in args['data']['value']:
                            new_key    = x.keys()[0].replace("@", "")
                            old_key    = x.keys()[0]
                            x[new_key] = x.pop(old_key)
                            dict_where.update(x)

                        ret.match(where['operator'], dict_where)
                        if args['data'].has_key('code') and args['data'].get('code', None) != None and args['data']['code'] != "":
                            ret.match(combobox_info['value_field'] + " == {0}", args['data']['code'])
                    else:
                        ret.match(where['operator'])

                except Exception as ex:
                    raise(Exception("syntax where error"))

                #filter with text from browser
                if args['data'].has_key('search') and args['data'].get('search', None) != None and args['data']['search'] != "":
                    if len(filter) > 0:
                        filter_query = ""
                        filter_dict = dict()
                        for i in range(len(filter)):
                            if i == (len(filter) - 1):
                                filter_query += "contains(" + filter[i] + ", " + "@" + filter[i] + ")"
                            else:
                                filter_query += "contains(" + filter[i] + ", " + "@" + filter[i] + ") or "
                            filter_dict.update({filter[i].format() : args['data']['search']})

                        ret.match(filter_query, filter_dict)

            if sort != {}:
                try:
                    ret.sort(sort)
                except Exception as ex:
                    raise(Exception("syntax sort error"))

            #Pagination
            page_index = 0
            data = dict()
            if args['data'].has_key('pageIndex') and args['data'].get('pageIndex', None) != None:
                page_index = args['data']['pageIndex']

            data = ret.get_page(page_index, page_size)

            if data != {}:
                for y in where['column']:
                    for x in data['items']:
                        if y not in data['items'][0].keys():
                            del x[y.format()]

            ret_list = data


            return {"data"           : ret_list,
                    "display_name"   : combobox_info["display_name"],
                    "display_fields" : combobox_info["display_fields"],
                    "value_field"    : combobox_info["value_field"],
                    "caption_field"  : combobox_info["caption_field"],
                    "value_custom"   : combobox_info["value_custom"],
                    "sorting_field"  : combobox_info["sorting_field"],
                    "filter_field"   : combobox_info["filter_field"],
                    "page_size"      : combobox_info["page_size"],
                    "error"          : None}
        raise(Exception("Not found collection name"))

    except Exception as ex:
        logger.debug(ex)
        return {"data": None, "error": ex.message}