Пример #1
0
def pack_save_data(req_dict):
    '''
     {
       '_sync_key':{'id': 123} ,
       '_rule':{'locale':[]},
       '_meta':{}
        }
    '''
    pack_type = req_dict.get('pack_type')
    save_data = {}
    if pack_type == PackageType.Common:
        save_data = {
            'id': req_dict.get('id'),
            '_meta': req_dict.get('_meta'),
            '_rule': req_dict.get('_rule'),
            '_sync_key': req_dict.get('_sync_key'),
            'last_modified': now_timestamp()}
    elif pack_type == PackageType.Inject:
        rule_dict = req_dict.get('_rule')
        packages = rule_dict.pop('packages', [])
        if len(packages) == 1:
            rule_dict['package'] = ','.join(packages)
        else:
            rule_dict['package'] = packages

        save_data.update(req_dict.get('_meta'))
        save_data.update(rule_dict)
        save_data['id'] = req_dict.get('id')
        save_data['last_modified'] = now_timestamp()
    else:
        save_data.update(req_dict.get('_meta'))
        save_data['_rule'].update(req_dict.get('_rule'))
        save_data['id'] = req_dict.get('id')
        save_data['last_modified'] = now_timestamp()
    return save_data
Пример #2
0
 def new(cls, data):
     """
     creat locale instance
     """
     instance = cls()
     instance.title = data["title"]
     instance.first_created = now_timestamp()
     instance.last_modified = now_timestamp()
     return instance
Пример #3
0
 def new(cls, data):
     """
     creat package instance
     """
     instance = cls()
     instance.title = data["title"]
     instance.platform = data["platform"]
     instance.package_name = data["package_name"]
     instance.first_created = now_timestamp()
     instance.last_modified = now_timestamp()
     return instance
Пример #4
0
def upload2server(appname, modelname, item, server):
    Model_Class = classing_model(modelname)
    upload_table = get_upload_table(modelname)
    Remote_db = ArmoryMongo[server]
    # update status upload to local
    results = package_data(appname, modelname, item, server)
    # param1: success/failed true/false param2:msg  param3:return dict
    if not results[0]:
        return False, results[1]
    else:
        item_id = item.get('id')
        cond = {"id": item_id}
        res_dict = results[2]

        is_upload_server = "is_upload_%s" % server
        last_release_server = "last_release_%s" % server

        upt_status = {}
        upt_status[is_upload_server] = True
        upt_status[last_release_server] = now_timestamp()
        if server == _LOCAL:
            upt_status['release'] = UploadStatus.UNUPLOAD_EC2
        if server == _EC2:
            upt_status['release'] = UploadStatus.NOMORAL
        Model_Class.update(appname, cond, upt_status)

        save_data = pack_save_data(res_dict)
        Remote_db[upload_table].update(cond, save_data, True)
        return True, results[1]
Пример #5
0
def common_create(appname, modelname, temp_dict):
    Model_Name = classing_model(modelname)
    try:
        temp_dict = CommonBase.clean_data(modelname, temp_dict)
    except ParamError as e:
        return json_response_error(PARAM_ERROR, msg=e.msg)
    # when save data, filter some useless data
    pop_list = [
        'id', 'last_release_ec2', 'last_release_local', 'release',
        'is_upload_local', 'is_upload_ec2']
    for key in pop_list:
        temp_dict.pop(key, None)
    if hasattr(Model_Name, "fields_check"):
        try:
            temp_dict = get_valid_params(temp_dict, Model_Name.fields_check)
        except ParamError as e:
            return json_response_error(PARAM_ERROR, msg=e.msg)
    temp_dict['first_created'] = temp_dict['last_modified'] = now_timestamp()

    #this is a factory function, to check some save data before insert if need
    (check_success, msg) = check_save_dict(appname, modelname, temp_dict)
    if not check_success:
        return json_response_error(PARAM_ERROR, msg=msg)
    insert_id = Model_Name.insert(appname, temp_dict)

    temp_dict['id'] = insert_id
    # if the model has ref icon and rule, increase the reference
    inc_icon(appname, modelname, temp_dict)
    inc_rule(appname, modelname, temp_dict)
    return json_response_ok(temp_dict)
Пример #6
0
def create_icon(appname, data):
    for key in Icon.params:
        if not data.get(key):
            return json_response_error(PARAM_REQUIRED, msg="no param:%s" % key)

    # if not check_ospn_params(appname, data):
    #     return json_response_error(
    #         PARAM_ERROR, msg="param:platform or package error")

    data["title"] = "%s_%s" % (data["title"], now_timestamp())
    icon_info = Icon.find_one(appname, {"title": data["title"]})
    if icon_info:
        return json_response_error(
            DUPLICATE_FIELD, msg="the icon title exist")

    # save icon file to local file system
    data["icon"], data["height"], data["width"] = save_icon_file(
        data["icon"], appname)
    data["icon"] = _ICON_BASE_DIR + appname + '/' + data["icon"]

    # upload icon to file server
    file_path = os.path.join(MEDIA_ROOT, data["icon"])
    file_transfer = FileTransfer(file_path)
    data["guid"], data["download_url"] = file_transfer.start()

    # insert icon dict to database 
    icon_instance = Icon.new(data)
    Icon.save(appname, icon_instance)
    _LOGGER.info("add a new icon:%s", data["title"])

    icon_info = Icon.find_one(appname, {"title": data["title"]}, None)
    icon_info["id"] = icon_info.pop("_id")
    return json_response_ok(icon_info)
Пример #7
0
def common_update(appname, modelname, temp_dict, item_id):
    Model_Name = classing_model(modelname)
    temp_dict.pop("id", 0)
    try:
        temp_dict = CommonBase.clean_data(modelname, temp_dict)
    except ParamError as e:
        return json_response_error(PARAM_ERROR, msg=e.msg)
    if hasattr(Model_Name, "fields_check"):
        try:
            temp_dict = get_valid_params(temp_dict, Model_Name.fields_check)
        except ParamError as e:
            return json_response_error(PARAM_ERROR, msg=e.msg)
    cond = {"id": int(item_id)}
    temp_dict['last_modified'] = now_timestamp()
    # if the model has release field
    if check_release(modelname):
        temp_dict['release'] = UploadStatus.UNUPLOAD_LOCAL
    # find the old item before update, aim to track icon and rule
    old_item = Model_Name.find_one(appname, {'id': item_id}, {'_id': 0})

    #this is a factory function, to check some save data before save if need
    (check_success, msg) = check_save_dict(
        appname, modelname, temp_dict, item_id)
    if not check_success:
        return json_response_error(PARAM_ERROR, msg=msg)

    Model_Name.update(appname, cond, temp_dict)
    temp_dict['id'] = item_id
    # if model has refered icon and rule
    mod_icon(appname, modelname, temp_dict, old_item)
    mod_rule(appname, modelname, temp_dict, old_item)
    return json_response_ok(temp_dict)
Пример #8
0
def get_valid_params(query_dict, keys):
    '''
    get valid params by params rule
    '''
    try:
        result = {}
        for key in keys:
            paras = key.split('&')
            (param_key, param_option,
             param_type, default_value) = tuple(
                paras) + (None,) * (4 - len(paras))
            if not param_key or param_option not in PARAM_OPTION_LIST:
                # invalid config for parameter %key%
                continue
            param_value = query_dict.get(param_key)
            if param_value is None or param_value is '':
                if param_option == 'need':
                    msg = param_key + ' is required'
                    raise ParamError(msg)
                if param_option == 'noneed':
                    # the key will not put into the result
                    continue
                if param_option == 'option':
                    if default_value is not None:
                        param_value = _conv(eval(param_type))(default_value)
                    else:
                        if param_type == 'time':
                            param_value = now_timestamp()
                        elif param_type == 'str':
                            param_value = ''
                        else:
                            param_value = None
                        # if default_value is None
            else:
                try:
                    if param_type.startswith('list') and param_type != 'list_dict':
                        if param_value:
                            sub_type = param_type.split('_')[1]
                            param_value = [_conv(eval(sub_type))(it) for it in param_value]
                    elif param_type == "json":
                        val = simplejson.loads(param_value)
                        if not isinstance(val, types.DictType):
                            msg = param_key + " param value error"
                            raise ParamError(msg)
                    else:
                        if param_type not in ["str", "list_dict", "dict", "time"]:
                            param_value = _conv(eval(param_type))(param_value)
                except Exception as e:
                    msg = param_key + " param value error"
                    raise ParamError(msg)
            result[param_key] = param_value
        return result
    except Exception as e:
        _LOGGER.exception(e)
        if not isinstance(e, ParamError):
            raise UnknownError('get param error')
        else:
            _LOGGER.warn('check parameter exception![%s]' % e.msg)
            raise e
Пример #9
0
 def new(cls, faq_dict):
     """
     creat icon instance
     """
     instance = cls()
     instance.title = faq_dict["title"]
     instance.icon = faq_dict["icon"]
     instance.guid = faq_dict["guid"]
     instance.download_url = faq_dict["download_url"]
     instance.platform = faq_dict["platform"]
     instance.width = faq_dict.get("width", 0)
     instance.height = faq_dict.get("height", 0)
     instance.package = faq_dict["package"]
     instance.type = faq_dict["type"]
     instance.refered_info = []
     instance.first_created = now_timestamp()
     instance.last_modified = now_timestamp()
     return instance
Пример #10
0
 def new(cls, group_name, permission_list={}):
     """
     creat group instance
     """
     instance = cls()
     instance.data = {}
     instance.data['group_name'] = group_name
     instance.data['permission_list'] = permission_list
     instance.data['modified'] = now_timestamp()
     return instance
Пример #11
0
    def new(
            cls, projectname, app_name, module_name, id,
            uid, check_uid=[], status=1, mark=''):
        '''
        create app instance
        '''
        instance = cls()
        instance.data = {}

        instance.data['projectname'] = projectname
        instance.data['app_name'] = app_name
        instance.data['module_name'] = module_name
        instance.data['check_id'] = id
        instance.data['submit_uid'] = uid
        instance.data['check_uid'] = check_uid
        instance.data['mark'] = mark
        instance.data['status'] = status
        instance.data['first_created'] = now_timestamp()
        instance.data['last_modified'] = now_timestamp()
        return instance
Пример #12
0
 def new(cls, rule_dict):
     """
     creat source instance
     """
     instance = cls()
     instance.title = rule_dict["title"]
     instance.platform = rule_dict["platform"]
     instance.package = rule_dict["package"]
     instance.operator = rule_dict["operator"]
     instance.source = rule_dict["source"]
     instance.locale = rule_dict["locale"]
     instance.min_version = rule_dict["min_version"]
     instance.max_version = rule_dict.get("max_version", _MAX_VERSION)
     instance.min_value = rule_dict.get("min_value", 0)
     instance.min_value = rule_dict.get("min_value", 0)
     instance.max_value = rule_dict.get("max_value", 0)
     instance.gray_start = rule_dict.get("gray_start", 1)
     instance.gray_scale = rule_dict.get("gray_scale", 100)
     instance.first_created = now_timestamp()
     instance.last_modified = now_timestamp()
     return instance
Пример #13
0
def user_login(appname, user_name, password, session):
    user_cond = {"user_name": user_name, "password": password}
    user_check = User.find_one_user(appname, user_cond, None)
    if not user_check:
        return json_response_error(AUTH_ERROR, {}, msg="username or password err")
    elif not user_check["is_active"]:
        return json_response_error(AUTH_ERROR, {}, msg="user is not active")
    else:
        session["uid"] = int(user_check["_id"])
        uid = user_check["_id"]
        upt_dict = {"last_login": now_timestamp(), "total_login": user_check.get("total_login") + 1}
        User.update_user(appname, {"_id": uid}, upt_dict)
        # 业务相关拆分
        # permissions = Permission.init_menu(uid)
        return json_response_ok({"uid": uid})
Пример #14
0
def update_status_by_id(appname, projectname, modulename, id, status):
    coll = get_coll_by_module(appname, projectname, modulename)
    cond = {"_id": id}
    field = {
        "_id": 0, "checked": 1, "last_modified": 1,
        "upload_local": 1, "upload_ec2": 1}
    info = ArmoryMongo[projectname][coll].find_one(cond, field)
    if info:
        info["checked"] = status
        info["last_modified"] = now_timestamp()
        ArmoryMongo[projectname][coll].update(cond, {'$set': info})
        info["release"] = get_status(info)
        info["last_modified"] = unixto_string(info["last_modified"])
        info["id"] = id
        return info
    else:
        return None
Пример #15
0
def check_content(appname, projectname, applabel, module, id, data):
    '''
    this api is used to check content
    Request URL: /<projectname>/auth/<applabel>/<modulename>/check/<id>
    HTTP Method: POST
    Parameters:
        {
            "checked": 3,
            "mark": ""
        }
    Return:
     {
        "status":0
        "data":{
            "id": 1,
            "checked": 3,
            "release": 1,
            "last_modified": "2015-04-16 14:10:37"
            }
        "msg":""
     }
    '''
    id = int(id)
    cond = {
        "check_id": id, 'projectname': projectname, "check_uid": data["uid"],
        "app_name": applabel, "module_name": module}
    check_info = CheckList.find_one(appname, cond, None)
    if not check_info:
        _LOGGER.info("check info id %s is not submit" % id)
        return json_response_error(
            PARAM_ERROR, data, msg="invalid id, %s is not submit" % id)

    #update response info in coll
    info = update_status_by_id(
        appname, projectname, module, id, data["checked"])
    if not info:
        return json_response_error(
            PARAM_ERROR, data, msg="invalid id,check parameters")

    #update check list
    data["last_modified"] = now_timestamp()
    CheckList.update(appname, cond, data)
    _send_email_to_user(
        appname, projectname, module, data["uid"], check_info["submit_uid"],
        id, data["mark"])
    return json_response_ok(info)
Пример #16
0
def save_loginfo(data_obj):
    act_info = {}
    sid = data_obj.get('sid')
    url = data_obj.get('url')
    rev_d = data_obj.get('data')
    try:
        src_d = simplejson.loads(simplejson.loads(rev_d))
        if src_d.get('data'):
            act_info.update({
                'msg': src_d.get('msg'),
                'status': src_d.get('status')})
            act_info = filter_url(url)
            if not act_info:
                return PARAM_ERROR
            # as the diff action, get diff data from respone data
            action = act_info.get('action')
            models = []
            if action in ['add', 'update']:
                model_dict = {
                    'modelid': src_d['data'].get('id'),
                    'modeltitle': src_d['data'].get('title') or
                    src_d['data'].get('name')}
                models.append(model_dict)
                act_info.update({'models': models})
            elif action in ['delete', 'online', 'offline', 'upload']:
                models = src_d['data'].get('userlog')
                memo = src_d['data'].get('memo')
                act_info.update({'models': models})
                act_info.update({'memo': memo})
            else:
                _LOGGER.error('action error, do not know this action')
                act_info.update({'models': None})
            user_name = get_userinfo(sid)
            if user_name:
                act_info.update({
                    'username': user_name,
                    'optime': now_timestamp()})
                save_userlog(act_info)
                return OK
            else:
                return PERMISSION_DENY
                _LOGGER.error('error no session, redirect login')
    except ValueError, e:
        _LOGGER.error(e)
        return UNKNOWN_ERROR
Пример #17
0
def info_mod(appname, MODELNAME, data):
    info_id = data.pop("id")
    old_info = MODELNAME.find_one(appname, {"title": data["title"]})
    if old_info and old_info["_id"] != info_id:
        return json_response_error(PARAM_ERROR, msg="the title exist")

    cond = {"_id": info_id}
    info = MODELNAME.find_one(appname, cond, MODELNAME.fields)
    if not info:
        return json_response_error(PARAM_ERROR, "invalid id: %s" % info_id)

    data["last_modified"] = now_timestamp()
    MODELNAME.update(appname, cond, data)
    _LOGGER.info("update %s:%s success", MODELNAME.model_name, info_id)

    data["last_modified"] = unixto_string(data.get("last_modified"))
    data["id"] = info_id
    return json_response_ok(data)
Пример #18
0
def mod_icon(appname, icon_id, data):
    cond = {"_id": icon_id}
    icon = Icon.find_one(appname, cond, Icon.fields)
    if not icon:
        return json_response_error(PARAM_ERROR, msg='%s not in db' % icon_id)

    is_modified = True
    if data.get("icon") is None:
        data["icon"] = icon["icon"]
        is_modified = False

    for key in Icon.params:
        if not data.get(key):
            return json_response_error(PARAM_REQUIRED, msg="no param:%s" % key)

    # if not check_ospn_params(appname, data):
    #     return json_response_error(
    #         PARAM_ERROR, msg="param:platform or package error")

    icon_info = Icon.find_one(appname, {"title": data["title"]})
    if icon_info and icon_info["_id"] != icon_id:
        return json_response_error(DUPLICATE_FIELD, msg="the title exist")

    if is_modified:
        # remove old icon
        remove_icon_file(appname, icon["icon"])

        # save new icon
        data["icon"], data["height"], data["width"] = save_icon_file(
            data["icon"], appname)
        data["icon"] = _ICON_BASE_DIR + appname + '/' + data["icon"]

        # upload new icon to file server
        file_path = os.path.join(MEDIA_ROOT, data["icon"])
        file_transfer = FileTransfer(file_path)
        data["guid"], data["download_url"] = file_transfer.start()

        # update to database
        data["last_modified"] = now_timestamp()
        Icon.update(appname, cond, data)
        _LOGGER.info("update icon:%s success", icon_id)

    data["id"] = icon_id
    return json_response_ok(data)
Пример #19
0
def checklist_create(appname, projectname, applabel, module, uid, ids):
    '''
    this api to add checklist.
    Request URL: /<projectname>/auth/<applabel>/<modulename>/submit
    Parameters:
    {
        'app_name': 'homeadmin',
        'module_name': 'banner',
        'items': [1, 2, 3]
    }
    '''
    data = {"success": [], "failed": []}
    check_uids = []
    check_uids = get_check_uids(appname, projectname, applabel, module)
    ids = [int(id) for id in ids]
    for id in ids:
        info = update_status_by_id(appname, projectname, module, id, 1)
        if not info:
            data["failed"].append(id)
            continue
        cond = {
            "check_id": id, 'projectname': projectname, "submit_uid": uid,
            "app_name": applabel, "module_name": module}
        old_check_info = CheckList.find_one(appname, cond, {"_id": 0})
        if old_check_info:
            old_check_info["status"] = 1
            old_check_info["mark"] = ""
            old_check_info["check_uid"] = check_uids
            old_check_info["last_modified"] = now_timestamp()
            CheckList.update(appname, cond, old_check_info)
        else:
            checklist_instance = CheckList.new(
                projectname, applabel, module, id, uid, check_uids)
            CheckList.save(appname, checklist_instance)
        data["success"].append(info)
    _send_email_to_assessor(appname, projectname, module, uid, check_uids, ids)
    if data["failed"]:
        return json_response_error(PARAM_ERROR, data, msg="invalid id")
    return json_response_ok(data)