Esempio n. 1
0
def edit_blog(user_id):
    blog = session.query(Blog).filter_by(user_id=user_id).one()
    old_img = blog.profile_img
    form = BlogForm(obj=blog)
    if form.validate_on_submit():
        blog.last_modified = time()
        form.populate_obj(blog)
        if form.image.data and allowed_file(form.image.data.filename):
            if old_img:
                # first delete the previous file in the upload_folder
                deleting_file = os.path.join(app.config['UPLOAD_FOLDER'],
                                             old_img)
                os.remove(deleting_file)
            # then upload the new file
            file = form.image.data
            # upload the file with prefix(blog_id), suffix(last_modified)
            blog.profile_img = upload(file, str(login_session['blog_id']),
                                      str(int(blog.last_modified)))

        session.add(blog)
        session.commit()
        return redirect(url_for('view_blog', user_id=user_id))
    return render_template('blog_profile.html',
                           form=form,
                           user_id=login_session.get('user_id'),
                           username=login_session.get('username'))
Esempio n. 2
0
def edit_blog(id):
    blog = Blog.query.get_or_404(id)
    if current_user != blog.author and not current_user.is_administrator:
        abort(403)
    form = BlogForm(obj=blog)
    if form.validate_on_submit():
        form.populate_obj(blog)
        db.session.add(blog)
        db.session.commit()
        flash('博客已经更新', 'success')
        return redirect(url_for('front.blog', id=blog.id))
    return render_template('user/edit_blog.html', form=form)
Esempio n. 3
0
def index():
    '''网站首页'''
    form = BlogForm()
    # 凡是登录成功的用户,就有写博客的权限
    if current_user.is_authenticated:
        if form.validate_on_submit():
            blog = Blog()
            form.populate_obj(blog)
            blog.author = current_user
            db.session.add(blog)
            db.session.commit()
            flash('成功发布博客', 'success')
            return redirect(url_for('.index'))
    page = request.args.get('page', 1, type=int)
    pagination = Blog.query.order_by(Blog.time_stamp.desc()).paginate(
        page, per_page=current_app.config['BLOGS_PER_PAGE'], error_out=False)
    blogs = pagination.items
    return render_template('index.html',
                           form=form,
                           blogs=blogs,
                           pagination=pagination)