예제 #1
0
파일: user_routes.py 프로젝트: katyeh/kafei
def new_photo(id):
    try:
        form = UploadPhotoForm()

        form['csrf_token'].data = request.cookies['csrf_token']

        if form.validate_on_submit():
            key_list = request.files.keys()

            if request.files:
                if "pic_url" in key_list:
                    new_image_data = request.files["pic_url"]
                    new_image_key = f"photos/{uuid.uuid4()}_{new_image_data.filename}"
                    # new_image_key = f"photos/{id}/{uuid.uuid4()}_{new_image_data.filename}"
                    client.put_object(Body=new_image_data, Bucket="kafei", Key=new_image_key,
                                      ContentType=new_image_data.mimetype, ACL="public-read")

            photo = Photo(
                pic_url=f"https://kafei.s3-us-west-1.amazonaws.com/{new_image_key}",
                user_id=id
            )
            db.session.add(photo)
            db.session.commit()
            return photo.to_dict()
    except Exception as error:
        return jsonify(error=repr(error))
예제 #2
0
def new_photo():
    data = request.json

    try:
        photo = Photo(
            user_id=data['userId'],
            entry_id=data['entryId'],
            photos_url=data['photosUrl']
        )
        db.session.add(photo)
        db.session.commit()
        return {'photo': photo.to_dict()}
    except SQLAlchemyError as e:
        error = str(e.__dict__['orig'])
        print(error)
        return {'errors': ['An error occurred while creating the picture']}, 500