Пример #1
0
def edit(post_id):
    # Return post form, but filled out
    # Use existing PostForm
    post = Post.query.filter_by(id=post_id).first_or_404()
    form = PostForm(obj=post) # post here is the post in previous line; fills out the form with that data
    if form.validate_on_submit():
        original_image=post.image
        form.populate_obj(post) # Replaces post with the contents of the form
        if form.image.has_file():
            image = request.files.get('image')
            try:
                filename = uploaded_images.save(image)
            except:
                flash("The image was not uploaded.")
            if filename:
                post.image=filename
        else:
            post.image = original_image
        
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            post.category = new_category
        db.session.commit()
        return redirect(url_for('article', slug=post.slug))
    return render_template('blog/post.html', form=form, post=post, action="edit")
Пример #2
0
def edit(post_id):
    post = Post.query.filter_by(id=post_id).first_or_404()
    form = PostForm(obj=post)
    if form.validate_on_submit():
        original_image = post.image
        form.populate_obj(post)
        if form.image.has_file():
            image = request.files.get('image')
            try:
                filename = uploaded_images.save(image)
            except:
                flash('The image was not uploaded!')

            if filename:
                post.image = filename

        else:
            post.image = original_image

        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            post.category = new_category
        db.session.commit()
        return redirect(url_for('article', slug=post.slug))
    return render_template('blog/post.html', form=form, post=post, action='edit')
Пример #3
0
def post():

    form = PostForm()
    if form.validate_on_submit():
        import pdb; pdb.set_trace()
        image = request.files.get('image')
        filename = None
        try:
            filename = uploaded_images.save(image)

        except:
            flash("The Image was not uploaded")

        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        else:
            category = form.category.data
        blog = Blog.query.first()
        author = User.query.filter_by(username=session['username']).first()
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        post = Post(blog, author, title, body, category, slug, filename)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug))
    return render_template('blog/post.html', form=form, action='new')
Пример #4
0
def post():
    form = PostForm()
    if form.validate_on_submit():
        image = request.files.get("image")
        filename = None
        try:
            filename = uploaded_images.save(image)
        except:
            flash("The image was not uploaded")
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        else:
            category = form.category.data
        blog = Blog.query.first()
        author = Author.query.filter_by(username=session["username"]).first()
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        post = Post(blog, author, title, body, category, filename, slug)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for("article", slug=slug))
    return render_template("blog/post.html", form=form, action="new")
Пример #5
0
def post():
    form = PostForm()
    if form.validate_on_submit():
        #pdb.set_trace()
        image = request.files.get('image')
        filename = None
        print("image: ", image)
        try:
            filename = uploaded_images.save(image)
            print("filename: ", filename)
        except:
            flash("The image was not uploaded.")
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        else:
            category = form.category.data
        author = Author.query.filter_by(username=session['username']).first()
        author_id = author.id
        blog = Blog.query.filter_by(admin=author_id).first()
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        post = Post(blog, author, title, body, category, filename, slug)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug))
    return render_template('blog/post.html', form=form, action="new")
Пример #6
0
def edit(post_id):
    post = Post.query.filter_by(id=post_id).first_or_404()
    form = PostForm(obj=post)
    if form.validate_on_submit():
        original_image = post.image
        form.populate_obj(post)
        if form.image.has_file():
            image = request.files.get('image')
            filename = None
            try:
                filename = uploaded_images.save(image)
            except:
                flash("The image was not uploaded")
            if filename:
                post.image = filename
        else:
            post.image = original_image
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            post.category = new_category
        db.session.commit()
        return redirect(url_for('article', slug=post.slug))
    return render_template('blog/post.html',
                           form=form,
                           post=post,
                           action="edit")
Пример #7
0
def post():
    form = PostForm()
    comment_form = CommentForm()
    error=None
    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename=uploaded_images.save(image)
        except:
            flash('The image was not uploaded')
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush() # trick again, hasn't committed but creates object, commit in the end.
            category = new_category
        elif form.category.data:
            category_id = form.category.get_pk(form.category.data) # gets primary key (id) of the selection
            category = Category.query.filter_by(id=category_id).first()
        else:
            category=None
        blog = Blog.query.first() # assuming one blog, if system with more than 1 blog - figure out here
        author = Author.query.filter_by(username=session['username']).first()
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        post = Post(blog, author, title, body, category, filename, slug)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug)) # sending to article page with slug
        
    return render_template('blog/post.html', form=form, comment_form=comment_form, action="new")
Пример #8
0
def edit(post_id):
    post = Post.query.filter_by(id=post_id).first_or_404()
    form = PostForm(obj=post) # assign values to form from object above, inbuilt feature
    if form.validate_on_submit():
        original_image = post.image
        form.populate_obj(post) # loads and replaces post object with contents of the form
        if form.image.has_file():
            image = request.files.get('image')
            try:
                filename = uploaded_images.save(image)
            except:
                flash("Image not uploaded")
            if filename:
                post.image = filename
        else:
            post.image = original_image # post.image has been populated by the form content so need to redefine (if user hasnt chosen a file)
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            post.category = new_category
        db.session.commit()
        return redirect(url_for('article', slug=post.slug))
            
    return render_template('blog/post.html', form=form, post=post, action=edit) # pre loading post
Пример #9
0
def post():
    form = PostForm()
    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename = uploaded_images.save(image)
        except:
            flash("Image was not uploaded")
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        elif form.category.data:
            category_id = form.category.get_pk(form.category.data)
            category = Category.query.filter_by(id=category_id).first()
        else:
            category = None
        # assuming there is only one blog per system
        blog = Blog.query.first()
        author = Author.query.filter_by(username=session['username']).first()
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        post=Post(blog, author, title, body, category, filename, slug)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug))
    return render_template('blog/post.html', form=form, action='new')
Пример #10
0
def post():
    form = PostForm()
    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename = uploaded_images.save(image)
        except:
            flash('The image was not uploaded')
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        elif form.category.data:
            category_id = form.category.get_pk(form.category.data)
            category = Category.query.filter_by(id=category_id).first()
        else:
            category = None
        blog = Blog.query.first()
        author = Author.query.filter_by(username=session['username']).first()
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        post = Post(blog, author, title, body, category, filename, slug)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug))
    return render_template('blog/post.html', form=form, action='new')
Пример #11
0
def post():
    form = PostForm()
    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename = uploaded_images.save(image)
        except:
            flash("The image was not uploaded")

        # we are assuming we can have only category per post
        # basic order is check for new category, else check for new existing category and if none of these are presen
        if form.new_category.data:
            # checks if a new category is created or not
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        elif form.category.data:
            # check if a user seleected existing category
            category_id = form.category.get_pk(form.category.data)
            # helper function get_pk gives out the primary key
            category = Category.query.filter_by(id=category_id).first()
        else:
            # So this is condition when no category is specified
            # So we put all that in the unknown category
            # we search if there is already that category created otherwise we create it
            try:
                category_id = form.category.get_pk("Unknown")
                category = Category.query.filter_by(id=category_id).first()
            except:
                category = Category("Unknown")

        blog = Blog.query.first()
        # assuming we have only one blog
        # need to add functionality if more than one blog
        author = Author.query.filter_by(username=session['username']).first()
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        post = Post(blog, author, title, body, category, filename, slug)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug))

    return render_template('blog/post.html', form=form, action="new")
Пример #12
0
def editbio():
    form = RegisterForm()
    error = ""
    message = ""
    datachanged = 0
    #pdb.set_trace()
    if form.validate_on_submit():
        if session['username'] == form.username:
            author = Author.query.filter_by(
                username=form.username.data
                ).first()
            if author:
                newbio = form.bio.data
                image = request.files.get('image')
                filename = None
                try:
                    filename = uploaded_images.save(image)
                    setattr(author, 'image', filename)
                    datachanged = 1
                except:
                    flash("The image was not uploaded.")
                    print("The image was not uploaded.")
                if newbio > ' ':
                    setattr(author, 'bio', newbio)
                    datachanged = 1
                if datachanged == 1:
                    db.session.commit()
                    return redirect(url_for('success', msg="Author updated"))
            else:
                #This will never execute, because you must be logged in to use this function,
                # and if you have a login, you are in the author table, even if is_author is false.
                return "You must be an author to edit your bio."
        else:  
            return "You may only edit your own information."
           # return url_for('index')
    else:            
        #username=session['username']
        #author = Author.query.filter_by(username=session['username']).first()
        #form.fullname = author.fullname
        #form.email = author.email
        #form.username = author.username
        #form.password = author.password
        #form.confirm = author.password
        #form.bio = author.bio
        #form.image = author.image
        return render_template('author/register.html', form=form,  action="edit")    
Пример #13
0
def post():
    #return 'Blog post'
    form = PostForm()
    error = ""

    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename = uploaded_images.save(image)
        except:
            flash('The image was not uploaded')

        if form.new_category.data:
            # add new category to database
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        elif form.category.data:
            category_id = form.category.get_pk(
                form.category.data)  # get primary key for form category field
            category = Category.query.filter_by(id=category_id).first()
        else:
            category = None

        blog = Blog.query.first()  # this app has only 1 blog

        author = Author.query.filter_by(username=session['username']).first()

        title = form.title.data
        body = form.body.data
        slug = slugify(title)

        post = Post(blog, author, title, body, category, filename, slug)

        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug))

    return render_template('blog/post.html',
                           form=form,
                           error=error,
                           action="new")
Пример #14
0
def setup():
    form = SetupForm()
    error = ""
    if form.validate_on_submit():
        salt = bcrypt.gensalt()
        hashed_password = bcrypt.hashpw(form.password.data, salt)
        if request.method == "POST":
            print("method is post")
        else:
            print("method is not post")
        #image = request.files.get('image')
        image = request.files.get('image')
        print("Image: ", image)
        filename = None
        try:
            filename = uploaded_images.save(image)
            print("Try filename")
            print("File Name: ", filename)
        except:
            flash("The image was not uploaded.")
            print("The image was not uploaded.")
        author = Author(form.fullname.data, form.email.data,
                        form.username.data, hashed_password, True,
                        form.bio.data, filename)
        db.session.add(author)
        db.session.flush()
        if author.id:
            blog = Blog(form.name.data, author.id)
            db.session.add(blog)
            db.session.flush()
        else:
            db.session.rollback()
            error = "Error creating user"
        if author.id and blog.id:
            db.session.commit()
            flash("Blog created")
            return redirect(url_for('index'))
        else:
            db.session.rollback()
            error = "Error creating blog"
    return render_template('blog/setup.html',
                           form=form,
                           error=error,
                           action="new")
Пример #15
0
def post():
    form = PostForm()
    error = ''
    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename = uploaded_images.save(image)
        except:
            flash('File not uploaded')
        if form.new_category.data:
            new_category  = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        elif form.category.data:
            category_id = form.category.get_pk(form.category.data)
            category = Category.query.filter_by(id=category_id).first()
        else:
            category = None
        blog = Blog.query.first()
        author = Author.query.filter_by(username=session['username']).first()
        slug = slugify(form.title.data)
        post = Post(
            blog,
            author,
            form.title.data,
            form.body.data,
            category,
            filename,
            slug
            )
        db.session.add(post)
        db.session.flush()
        
        if post.id:
            db.session.commit()
            flash('Post Created')
            return redirect(url_for('article', slug=slug))
        else:
            db.session.rollback()
            error = 'Error creating post'
    return render_template('blog/post.html', form=form, error=error)
Пример #16
0
def post():
    #return 'Blog post'
    form = PostForm()
    error = ""
    
    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename = uploaded_images.save(image)
        except:
            flash('The image was not uploaded')
            
        if form.new_category.data:
            # add new category to database
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        elif form.category.data:
            category_id = form.category.get_pk(form.category.data) # get primary key for form category field
            category = Category.query.filter_by(id=category_id).first()
        else:
            category = None
            
        blog = Blog.query.first() # this app has only 1 blog
        
        author = Author.query.filter_by(username = session['username']).first()
        
        title = form.title.data
        body = form.body.data
        slug = slugify(title)
        
        post = Post(blog, author, title, body,category, filename, slug)
            
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('article', slug=slug))
        
        
    return render_template('blog/post.html', form=form, error=error, action="new")
Пример #17
0
def edit(post_id):
    # get record
    post = Post.query.filter_by(id=post_id).first_or_404()
    # populate form fields with record data
    form = PostForm(obj=post)
    # form validated
    if form.validate_on_submit():
        # save image
        orig_image = post.image
        # repopulate from form fields
        form.populate_obj(post)

        # if form has new image
        if form.image.has_file():
            image = request.files.get('image')
            try:
                filename = uploaded_images.save(image)
            except:
                flash("the image not uploaded")
            if filename:
                post.image = filename
        else:
            post.image = orig_image

        if form.new_category.data:
            new_cat = Category(form.new_category.data)
            db.session.add(new_cat)
            # write new category to get id
            db.session.flush()
            post.category = new_cat

        # write full record to database
        db.session.commit()

        return redirect(url_for('article', slug=post.slug))

    return render_template('blog/post.html',
                           form=form,
                           post=post,
                           action="edit")
Пример #18
0
def edit(post_id):
    # get record
    post = Post.query.filter_by(id=post_id).first_or_404()
    # populate form fields with record data
    form = PostForm(obj=post)
    # form validated
    if form.validate_on_submit():
        # save image
        orig_image = post.image
        # repopulate from form fields
        form.populate_obj(post)
        
        # if form has new image
        if form.image.has_file():
            image = request.files.get('image')
            try:
                filename = uploaded_images.save(image)
            except:
                flash("the image not uploaded")
            if filename:
                post.image = filename
        else:
            post.image = orig_image
            
        if form.new_category.data:
            new_cat = Category(form.new_category.data)
            db.session.add(new_cat)
            # write new category to get id
            db.session.flush()
            post.category = new_cat
        
        # write full record to database
        db.session.commit()
        
        return redirect(url_for('article', slug=post.slug))
                
            
            
    return render_template('blog/post.html', form=form, post=post, action="edit")
Пример #19
0
def post():
    form = PostForm()
    error = ''
    if form.validate_on_submit():
        image = request.files.get('image')
        filename = None
        try:
            filename = uploaded_images.save(image)
        except:
            flash('File not uploaded')
        if form.new_category.data:
            new_category = Category(form.new_category.data)
            db.session.add(new_category)
            db.session.flush()
            category = new_category
        elif form.category.data:
            category_id = form.category.get_pk(form.category.data)
            category = Category.query.filter_by(id=category_id).first()
        else:
            category = None
        blog = Blog.query.first()
        author = Author.query.filter_by(username=session['username']).first()
        slug = slugify(form.title.data)
        post = Post(blog, author, form.title.data, form.body.data, category,
                    filename, slug)
        db.session.add(post)
        db.session.flush()

        if post.id:
            db.session.commit()
            flash('Post Created')
            return redirect(url_for('article', slug=slug))
        else:
            db.session.rollback()
            error = 'Error creating post'
    return render_template('blog/post.html', form=form, error=error)