Пример #1
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        language = guess_language(form.post.data)
        if language == "UNKNOWN" or len(language) > 5:
            language = ''
        post = Post(body=form.post.data,
                    author=current_user,
                    language=language)
        db.session.add(post)
        db.session.commit()
        flash(_('We added your post'))
        return redirect(url_for('index'))
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('index',
                       page=posts.next_num) if posts.has_next else None
    prev_url = url_for('index',
                       page=posts.prev_num) if posts.has_prev else None
    return render_template('index.html',
                           title='Home',
                           form=form,
                           posts=posts.items,
                           next_url=next_url,
                           prev_url=prev_url)
Пример #2
0
def newpost():
    form=PostForm()
    if form.validate_on_submit():
        post=Post(title=form.title.data,content=form.content.data,author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your post has been created!','success')
        return redirect(url_for('home'))
    return render_template('newpost.html',form=form)
Пример #3
0
def index():
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('Your post is now live!')
        return redirect(url_for('index'))
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(page, app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('index', page=posts.next_num) if posts.has_next else None
    prev_url = url_for('index', page=posts.prev_num) if posts.has_prev else None
    return render_template('index.html', title='Home Page', form=form, posts=posts.items, next_url=next_url,prev_url=prev_url)
Пример #4
0
def create():
    """depends on method
    GET  direct to where writing new post
    POST process new post submit
    """
    post_form = PostForm()
    if post_form.validate_on_submit():
        post = Post.insert(title=post_form.title.data,
                           content=post_form.content.data,
                           published=post_form.published.data,
                           author=current_user._get_current_object())
        flash('Post created successfully.', 'success')
        if post.published:
            return redirect(url_for('main.detail', id=post.id))
        else:
            return redirect(url_for('main.edit', id=post.id))
    return render_template('create.html', post_form=PostForm())
Пример #5
0
def writepost():
    user = g.user
    form = PostForm()
    title = form.title.data
    body = form.body.data
    if request.method == 'POST':
        if form.validate_on_submit():
            if body is not None and title is not None:
                Post.insert_post(user.id, title, body)
                flash('write post successfully!', 'success')
                return redirect(url_for('index'))
            else:
                flash('Error!title or body empty.', 'danger')
        return redirect(url_for('app.writepost'))
    return render_template('writepost.html',
                           title='Post',
                           form=form,
                           user=user)
Пример #6
0
def edit_post(post_id):
    post = Post.query.get_or_404(post_id)
    form = PostForm()
    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        category = Category.query.get(form.categories.data)
        post.title = title
        post.body = body
        post.category = category
        with db.auto_commit():
            db.session.add(post)
        flash('修改成功', 'success')
        return redirect(url_for('blog.show_post', post_id=post_id))

    form.title.data = post.title
    form.body.data = post.body
    form.categories.data = post.category_id
    return render_template('admin/edit_post.html', form=form)
Пример #7
0
def edit(id):
    ''' Edit dedicated post '''
    post = Post.query.filter_by(id=id).first_or_404()
    post_form = PostForm()
    #check if method is 'POST' & data is validate
    if post_form.validate_on_submit():
        post.modify(post_form.title.data, post_form.content.data,
                    post_form.published.data)
        flash('Post saved successfully.', 'success')
        if post.published:
            return redirect(url_for('.detail', id=post.id))
        else:
            return redirect(url_for('.edit', id=post.id))
    #flash('Title and Content are required.', 'danger')

    #get data from DB to form
    post_form.title.data = post.title
    post_form.content.data = post.content
    post_form.published.data = post.published
    return render_template('edit.html', post_form=post_form)
Пример #8
0
def new_post():
    form = PostForm()
    if request.method == 'POST' and form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        category = Category.query.get(form.categories.data)
        post = Post()
        post.title = title
        post.category = category
        post.body = body
        try:
            db.session.add(post)
            db.session.commit()
        except Exception as e:
            current_app.logger.error(e)
            db.session.rollback()
            raise e
        flash('新建了一篇文章', 'success')
        return redirect(url_for('blog.show_post', post_id=post.id))
    return render_template('admin/new_post.html', form=form)
Пример #9
0
def user(username):
    form = PostForm()
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('没有' + username + '这个用户')
        return redirect(url_for('index'))
    if form.validate_on_submit():
        post = Post(body=form.post.data,
                    timestamp=datetime.utcnow(),
                    author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('发表成功!')
        return redirect(url_for('user', username=current_user.username))
    posts = current_user.followed_posts().all()
    return render_template('user.html',
                           title='这是个人中心',
                           user=user,
                           form=form,
                           posts=posts)
Пример #10
0
def index(page=1):
    user = g.user
    form = PostForm()
    if form.validate_on_submit():
        post = Post(body=form.post.data,
                    timestamp=datetime.utcnow(),
                    user_id=g.user.id)
        db.session.add(post)
        db.session.commit()
        flash("Your post now live!")
        return redirect(url_for("index"))

    posts = g.user.followed_posts().paginate(page, Config.POSTS_PER_PAGE,
                                             False)
    pprint(posts)
    return render_template("index.html",
                           title="home",
                           user=user,
                           form=form,
                           posts=posts)
Пример #11
0
def wenda():
    form = PostForm()
    if request.method == 'POST':
        title = request.form.get('title')
        content = request.form.get('content')
        user_id = g.user.id
        edit = Edit(title=title, content=content, user_id=user_id)
        db.session.add(edit)
        db.session.commit()
        id = edit.id
        return redirect(url_for('home.wendapost', id=id))
    return render_template('testmkd.html', form=form)
Пример #12
0
def new_blog():
    """new a blog"""
    if request.method == 'POST':
        title = request.form.get('title', None)
        content = request.form.get('content', None)
        tags = request.form.getlist('tags[]', None)
        post = Posts(title=title, content=content)
        post.user_id = current_user.id
        post.tags = tags
        db.session.add(post)
        db.session.commit()
        flash(u'添加成功!')
        return redirect(url_for('admin.list_blog'))
    if request.method == 'GET':
        form = PostForm()
        return render_template('admin.new_blog.html',
                               title='new blog',
                               form=form)
Пример #13
0
def edit_blog(id):
    """
    :parm: post.id
    edit the post
    """
    blog = db.session.query(Posts).filter_by(id=id).first_or_404()
    if request.method == 'POST':
        blog.title = request.form.get('title', None)
        blog.content = request.form.get('content', None)
        blog.tags = request.form.getlist('tags[]', None)
        blog.user_id = current_user.id
        db.session.add(blog)
        db.session.commit()
        flash(u'修改成功')
        return redirect(url_for('admin.list_blog'))
    if request.method == 'GET':
        form = PostForm()
        return render_template('admin.edit_blog.html',
                               title='edit blog',
                               blog=blog,
                               form=form)