Beispiel #1
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)
Beispiel #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)