Esempio n. 1
0
def del_from_admin(appname, icon_ids):
    data = {"success": [], "failed": []}
    for icon_id in icon_ids:
        icon_id = int(icon_id) if not isinstance(icon_id, int) else icon_id
        cond = {"_id": icon_id}
        icon = Icon.find_one(appname, cond, None)
        if not icon:
            _LOGGER.info("icon id %s is not exist" % icon_id)
            continue
        if icon["refered_info"]:
            _LOGGER.info("icon id %s is refer" % icon_id)
            refer_info = {"id":  icon_id, "refered_info": []}
            refer_info["refered_info"] = icon["refered_info"]
            data["failed"].append(refer_info)
        else:
            # remove from database
            Icon.remove(appname, cond)
            # remove from filesystem 
            remove_icon_file(appname, icon["icon"])
            data["success"].append({"id": icon_id})
    if data["failed"]:
        return json_response_error(
            DATA_RELETED_BY_OTHER, data, msg="releted by other")
    else:
        return json_response_ok(data)
Esempio n. 2
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)