コード例 #1
0
def new_post():
    form = PostForm()
    #Flask-Login 提供了一个代理对象 current_user 来访问和表示当前登录的对象, 这个对象在视图或模板中都是能够被访问的.
    # 所以我们常在需要判断用户是否为当前用户时使用(EG. 用户登录后希望修改自己创建的文章).
    if not current_user:
        return redirect(url_for('main.login'))

    if form.validate_on_submit():
        new_post = Post(id=str(uuid4()), title=form.title.data)
        new_post.text = form.text.data
        new_post.publish_date = datetime.datetime.now()

        db.session.add(new_post)
        db.session.commit()
        return redirect(url_for('blog.home'))
    return render_template('new_post.html', form=form)
コード例 #2
0
ファイル: fakes.py プロジェクト: theminimize/My-Blog
def fake_post(count=25):
    for i in range(count):
        post = Post(title=fake.sentence(),
                    body=fake.text(2000),
                    category=Category.query.get(
                        random.randint(1, Category.query.count())),
                    timestamp=fake.date_time_this_year())
        db.session.add(post)
    db.session.commit()
コード例 #3
0
def new_post():
    form = PostForm()
    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        category = Category.query.get(form.category.data)
        post = Post(title=title, body=body, category=category, author=current_user)
        db.session.add(post)
        db.session.commit()
        flash('博文已创建.', 'success')
        return redirect(url_for('blog.show_post', post_id=post.id))
    return render_template('admin/new_post.html', form=form)
コード例 #4
0
    def post(self, post_id=None):
        if post_id:
            abort(400)
        else:
            args = parsers.post_post_parser.parse_args(strict=True)

            user = User.verfy_auth_token(args['token'])
            if not user:
                abort(401)

            new_post = Post()
            new_post.title = args['title']
            new_post.date = datetime.datetime.now()
            new_post.text = args['title']
            new_post.user = user

            if args['tags']:
                for item in args['tags']:
                    tag = Tag.query.filter_by(name=item).first()
                    if tag:
                        new_post.tags.append(tag)
                    else:
                        new_tag = Tag()
                        new_tag = item
                        new_post.tags.append(new_tag)

            db.session.add(new_post)
            db.session.commit()
            return (new_post.id, 201)
コード例 #5
0
ファイル: admin.py プロジェクト: chenliFV/BigBearBlog
def new_post():
    form = PostForm()

    if form.validate_on_submit():
        title = form.title.data
        content = form.content.data
        timestamp = form.timestamp.data
        body = form.body.data
        category = Category.query.get(form.category.data)
        post = Post(title=title, body=body, category=category, content=content, timestamp=timestamp)
        db.session.add(post)
        db.session.commit()
        flash('新文章创建成功!', 'success')
        return redirect(url_for('blog.show_post', post_id=post.id))
    return render_template('admin/new_post.html', form=form)
コード例 #6
0
import datetime
import random
from uuid import uuid4

from MyBlog.models import db, User, Tag, Post

user = User(id=str(uuid4()), username='******', password='******')
db.session.add(user)
db.session.commit()

user = db.session.query(User).first()
tag_one = Tag(id=str(uuid4()), name='t1')
tag_two = Tag(id=str(uuid4()), name='t2')
tag_three = Tag(id=str(uuid4()), name='t3')
tag_four = Tag(id=str(uuid4()), name='t4')
tag_list = [tag_one, tag_two, tag_three, tag_four]

for i in range(100):
    new_post = Post(id=str(uuid4()), title="Post" + str(i))
    new_post.user = user
    new_post.publish_date = datetime.datetime.now()
    new_post.text = "this is post test"
    new_post.tags = random.sample(tag_list, random.randint(1, 3))
    db.session.add(new_post)

db.session.commit()