Ejemplo n.º 1
0
def recognize_position(category_id):
    category = Category.get_by_id(category_id)
    if category is None:
        raise ApiError("category is not exist")

    template = json.loads(request.form.get("position"))
    properties = {}

    new_position = MenuItem(category_id=category_id)

    for property_json in template["properties"]:
        key = property_json["name"]
        image_file = request.files[key]

        if key == 'image':
            filename = generate_filename(prefix=key + str(category_id))
            path = os.path.join(UPLOAD_FOLDER, filename)
            image_file.save(path)
            print(filename)
            print(path)
            new_position.image_name = filename
        else:
            property_value = image_to_string(image_file)
            properties[key] = property_value

    new_position.properties = str(properties)

    session.add(category)
    session.add(new_position)
    session.commit()

    return jsonify(new_position.serialize)