Exemplo n.º 1
0
def addPost():
    #get image and make sure dimensions are ok
    img = request.files['image']
    img.save('posts/' + secure_filename(
        img.filename))  # save to filesystem first bc pillow messes with it
    pillow_image = Image.open(request.files['image'])
    image_size = pillow_image.size
    if image_size[0] > image_size[1] * 2:
        return "image is too wide. Can be at max 2:1"
    if image_size[1] * 4 > image_size[0] * 5:
        return "image is too tall. Can be at max 4:5"

    #make sure account exists and user provided a caption
    form_info = request.form.to_dict()
    bot_name = form_info.get('username')
    caption = form_info.get('caption')
    if caption is None:
        caption = ""
    if bot_name is None:
        return "username is a required field"
    try:
        bot = Account("accounts/" + bot_name + ".acc")
    except:
        return "account with that username does not exist"

    #save to bot with the caption
    bot.addPost(os.getcwd() + "/posts/" + secure_filename(img.filename),
                caption)
    bot.save()
    return "ok"