Esempio n. 1
0
    def post(self):

        f = request.files['file']
        title = request.form["title"]
        content = request.form["content"]
        img_name = request.form["img_name"]

        s3 = boto3.resource('s3',
                            aws_access_key_id=ACCESS_KEY_ID,
                            aws_secret_access_key=ACCESS_SECRET_KEY,
                            config=Config(signature_version='s3v4'))

        # # Image Uploaded
        s3.Bucket(BUCKET_NAME).put_object(Key=img_name,
                                          Body=f,
                                          ACL='public-read')
        img_url = 'https://jkhatter-flask.s3-us-west-2.amazonaws.com/%s' % (
            img_name)
        print(img_url)

        safe = image_check(img_url)

        # if BlogModel.find_by_id(id):
        #     return({"message":"An item with name {} already exists.".format(title)})
        if safe > 0.50:
            blog = BlogModel(title, content, img_name, img_url)
            try:
                blog.insert_update()
            except:
                return ({
                    "message": "An error occurred in inserting the item."
                }), 500  #internal server error
            return blog.json(), 201
        else:
            return ({"message": "Image contains nudity content."}), 400
Esempio n. 2
0
  def post(self):
    args = blog_create_parser.parse_args()

    author_email = get_jwt_identity()
    author: UserModel = UserModel.query.filter_by(email=author_email).first()

    is_published = args["is_published"] if "is_published" in args else False
    thumbnail: FileStorage = args["thumbnail"]

    if is_allowed(thumbnail.filename):
      extension = get_extension(thumbnail.filename)
      filename = create_filename(author.id, args["title"], extension)
      filepath = f"thumbnails/{filename}"
      thumbnail.save("media/" + filepath)

      thumbnail_url = url_for("media.media", path=filepath, _external=True)

      blog = BlogModel(
          title=args["title"], content=args["content"], author=author.id, thumbnail=thumbnail_url, is_published=is_published)

      db.session.add(blog)
      db.session.commit()

      if current_app.testing:
        os.remove("media/" + filepath)

      return blog, 201

    abort(400, message="File is not an image file")
Esempio n. 3
0
    def put(self, id):

        f = request.files['file']
        title = request.form["title"]
        content = request.form["content"]
        img_name = request.form["img_name"]

        blog = BlogModel.find_by_id(id)

        s3 = boto3.resource('s3',
                            aws_access_key_id=ACCESS_KEY_ID,
                            aws_secret_access_key=ACCESS_SECRET_KEY,
                            config=Config(signature_version='s3v4'))

        # This will delete the old image
        obj = s3.Object(BUCKET_NAME, img_name)
        obj.delete()
        print("Delete done")

        # This will upload the new image
        s3.Bucket(BUCKET_NAME).put_object(Key=img_name,
                                          Body=f,
                                          ACL='public-read')
        img_url = 'https://jkhatter-flask.s3-us-west-2.amazonaws.com/%s' % (
            img_name)
        print(img_url)

        safe = image_check(img_url)
        print(safe)
        if safe > 0.50:
            if blog is None:
                blog = BlogModel(title, content, img_name, img_url)
            else:
                blog.title = title
                blog.content = content
                blog.img_name = img_name
                blog.img_url = img_url
            blog.insert_update()
            return blog.json()
        else:
            return ({"message": "Image contains nudity content."})
        return True
def newBlog():
    if request.form:
        form = request.form
        print(form)
        title = form['title']
        content = form['content']
        blog = BlogModel(title=title,
                         content=content,
                         author=current_user.user_id)
        session.add(blog)
        session.commit()
        return redirect('/blog?id={}'.format(blog.blog_id))
    else:
        return render_template("post_blog.html")
Esempio n. 5
0
def newBlog():
    form = BlogForm()
    if form.validate_on_submit():
        print(form)
        title = form['title'].data
        content = form['content'].data
        blog = BlogModel(name=title,
                         content=content,
                         author=current_user.id,
                         created_on=datetime.datetime.now())
        session.add(blog)
        session.commit()
        return redirect('/blog/{}'.format(blog.id))
    else:
        return render_template("post_blog.html", form=form)
Esempio n. 6
0
  def put(self, blog_id):
    self.blog_exists_quit(blog_id)

    args = blog_create_parser.parse_args()
    author_email = get_jwt_identity()
    author = UserModel.query.filter_by(dict(email=author_email)).first()

    is_published = args["is_published"] if "is_published" in args else False

    blog = BlogModel(
        title=args["title"], content=args["content"], author=author.id, is_published=is_published)

    db.session.add(blog)
    db.session.commit()

    return blog, 201
Esempio n. 7
0
    def delete(self, id):
        blog = BlogModel.find_by_id(id)
        s3 = boto3.resource('s3',
                            aws_access_key_id=ACCESS_KEY_ID,
                            aws_secret_access_key=ACCESS_SECRET_KEY,
                            config=Config(signature_version='s3v4'))

        # This will delete the old image
        obj = s3.Object(BUCKET_NAME, blog.img_name)
        obj.delete()
        print("Delete done")
        if blog:
            blog.delete()
            return {"message": "Item deleted"}
        else:
            return ({"message": "Item not found."})
Esempio n. 8
0
 def get(self, id):
     blog = BlogModel.find_by_id(id)
     if blog:
         return blog.json()
     return {"message": "Item not found"}, 404