Esempio n. 1
0
def specific_post(post_id):
    post = Post.query.get(post_id)
    if request.method == "GET":
        return post.to_dict()
    elif request.method == "PUT":
        form = CreatePostForm()
        form['csrf_token'].data = request.cookies['csrf_token']
        if form.validate_on_submit():
            form.populate_obj(post)
            db.session.commit()

            if 'images' in request.files:
                images = request.files.getlist('images')
                for image in images:
                    if allowed_file(image.filename):
                        image.filename = get_unique_filename(image.filename)
                        image_url = upload_file_to_s3(image, Config.S3_BUCKET)
                        image = Post(post_id=post.id, image_url=imgUrl)
                        db.session.add(image)
                db.session.commit()
            return post.to_dict()
        return {'errors': validation_errors_to_error_messages(form.errors)}
    elif request.method == "DELETE":
        db.session.delete(post)
        db.session.commit()
        return post.to_dict()
    return post.to_dict()
Esempio n. 2
0
def createPost():
    form = CreatePostForm()
    data = request.get_json(force=True)
    form['csrf_token'].data = request.cookies['csrf_token']
    form['userId'].data = current_user.id
    form['photoData'].data = data['photoData']
    form['caption'].data = data['caption']
    form['location'].data = data['location']
    if form.validate_on_submit():
        newPost = Post()
        form.populate_obj(newPost)
        db.session.add(newPost)
        db.session.commit()
        return {"success": 'true'}
    return {'errors': validation_errors_to_error_messages(form.errors)}, 401