Beispiel #1
0
def edit_blog_post(post_id):

    form = BlogPostForm()
    form.category.choices = ([(cat.category, cat.category)
                              for cat in BlogCategory.query.all()])

    post = BlogPost.query.filter_by(id=int(post_id)).first_or_404()

    # update post data
    if form.validate_on_submit() and form.submit.data:

        post.title = form.post_title.data
        post.body = form.post_body.data
        post.last_edit = datetime.utcnow()
        post.category = form.category.data

        if 'photo' in request.files:

            file = request.files['photo']
            filename = secure_filename(file.filename)
            images.save(request.files['photo'], folder="blog_photos/")
            post.photo_filename = filename

        db.session.commit()

        flash('Changes saved.')
        return redirect(url_for('manage_blog'))

    elif request.method == 'GET':
        form.post_title.data = post.title
        form.post_body.data = post.body
        form.category.data = post.category

    return render_template('edit_blog_post.html', form=form, post=post)
Beispiel #2
0
def manage_blog():

    form = BlogPostForm()
    form.category.choices = ([(cat.category, cat.category)
                              for cat in BlogCategory.query.all()])

    # Publish new blog post
    if form.validate_on_submit() and form.submit.data:

        if 'photo' not in request.files:
            flash('Post requires a photo.')
            return redirect(url_for('manage_blog'))

        file = request.files['photo']
        filename = secure_filename(file.filename)
        saved = images.save(request.files['photo'], folder="blog_photos/")

        post = BlogPost(title=form.post_title.data,
                        body=form.post_body.data,
                        category=form.category.data,
                        photo_filename=filename,
                        photo_alt_text=form.photo_alt_text.data)

        db.session.add(post)
        db.session.commit()

        if saved:
            flash('Post is now live!')
        return redirect(url_for('blog'))

    post_count = BlogPost.query.count()
    comments_count = BlogComment.query.count()
    most_pop = BlogPost.query.order_by(
        BlogPost.post_score.desc()).first_or_404()
    latest = BlogComment.query.order_by(
        BlogComment.timestamp.desc()).first_or_404()
    latest_comment_post = (BlogPost.query.filter_by(
        id=latest.post_id).first_or_404())

    page = request.args.get('page', 1, type=int)
    posts = BlogPost.query.order_by(BlogPost.timestamp.desc()).paginate(
        page, app.config['TITLES_PER_PAGE'], False)
    next_url = url_for('manage_blog', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('manage_blog', page=posts.prev_num) \
        if posts.has_prev else None

    return render_template('manage_blog.html',
                           title='Manage Blog',
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url,
                           post_count=post_count,
                           comments_count=comments_count,
                           most_pop=most_pop,
                           latest=latest,
                           latest_comment_post=latest_comment_post.title)
Beispiel #3
0
def post_blog():
    form = BlogPostForm(request.form)

    if form.validate() and request.method == 'POST':
        user = current_user
        post = Post(body=form.content.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Blog Post successfully created !')
        return redirect(url_for('index'))

    return render_template('post.html', form=form)
Beispiel #4
0
def post_update(post_id):
    update_form = BlogPostForm()
    post = Post.query.get_or_404(post_id)
    if request.method == 'POST' and update_form.validate():
        title = update_form.title.data
        content = update_form.content.data
        user_id = current_user.id

        post.title = title
        post.content = content
        post.user_id = user_id

        db.session.commit()
        return redirect(url_for('post_update', post_id=post.id))

    return render_template('post_update.html', form=update_form, post=post)
Beispiel #5
0
def createposts():
    form = BlogPostForm()
    context = {'form': form}

    if request.method == 'POST' and form.validate():
        title = form.title.data
        content = form.content.data
        user_id = current_user.id

        new_post = Post(title, content, user_id)

        db.session.add(new_post)
        db.session.commit()

        flash('You have created a new post!', 'success')

        return redirect(url_for('blog.posts'))

    return render_template('createposts.html', **context)
Beispiel #6
0
def createposts():
    form = BlogPostForm()
    context = {'form': form}
    if request.method == 'POST' and form.validate():
        # get information
        title = form.title.data
        content = form.content.data
        user_id = current_user.id

        # Create new instance of User
        new_post = Post(title, content, user_id)

        #add user to db
        db.session.add(new_post)
        db.session.commit()

        #flash success message
        flash('You have succesfully created a new post', 'success')

        return redirect(url_for('createposts'))
    return render_template('createposts.html', **context)