コード例 #1
0
def add_post():
    form = PostForm()
    if form.validate_on_submit():
        title = form.title.data
        body = form.body.data
        author = form.author.data
        photo = form.photo.data
        sources = form.sources.data
        slug = form.slug.data
        down_url = form.down_url.data
        recommend = form.recommend.data
        timestamp = form.timestamp.data
        category = Category.query.get(form.category.data)
        post = Post(title=title,
                    photo=photo,
                    author=author,
                    down_url=down_url,
                    sources=sources,
                    recommend=recommend,
                    slug=TestSlugification.test_extraneous_seperators(slug),
                    body=body,
                    timestamp=timestamp,
                    category=category)
        db.session.add(post)
        db.session.commit()
        return redirect(url_for('.post_list', post_id=post.id, slug=slug))
    return render_template('admin/add_post.html', form=form)
コード例 #2
0
def posts(count=100):
    fake = Faker()
    user_count = User.query.count()
    for i in range(count):
        u = User.query.offset(randint(0, user_count - 1)).first()
        p = Post(body=fake.text(), timestamp=fake.past_date(), author=u)
        db.session.add(p)
    db.session.commit()
コード例 #3
0
def sendPost():
    usrname = request.form['username']
    text = request.form['text']

    u = Post(user_name=usrname, text=text)
    db.session.add(u)
    db.session.commit()
    return redirect('/index')
コード例 #4
0
ファイル: posts.py プロジェクト: Colaplusice/hello_flask
def new_post():
    post = Post.from_json(request.json)
    post.author = g.current_user

    db.session.add(post)
    db.session.commit()
    return jsonify(
        post.to_json(),
        201,
        {"location": url_for("api.get_post", id=post.id, _external=True)},
    )
コード例 #5
0
ファイル: test.py プロジェクト: jefflike/flask_blog
    def test_follow_posts(self):
        # create four users
        u1 = User(username='******', email='*****@*****.**')
        u2 = User(username='******', email='*****@*****.**')
        u3 = User(username='******', email='*****@*****.**')
        u4 = User(username='******', email='*****@*****.**')
        db.session.add_all([u1, u2, u3, u4])

        # create four posts
        now = datetime.utcnow()
        p1 = Post(body="post from john",
                  author=u1,
                  timestamp=now + timedelta(seconds=1))
        p2 = Post(body="post from susan",
                  author=u2,
                  timestamp=now + timedelta(seconds=4))
        p3 = Post(body="post from mary",
                  author=u3,
                  timestamp=now + timedelta(seconds=3))
        p4 = Post(body="post from david",
                  author=u4,
                  timestamp=now + timedelta(seconds=2))
        db.session.add_all([p1, p2, p3, p4])
        db.session.commit()

        # setup the followers
        u1.follow(u2)  # john follows susan
        u1.follow(u4)  # john follows david
        u2.follow(u3)  # susan follows mary
        u3.follow(u4)  # mary follows david
        db.session.commit()

        # check the followed posts of each user
        f1 = u1.followed_posts().all()
        f2 = u2.followed_posts().all()
        f3 = u3.followed_posts().all()
        f4 = u4.followed_posts().all()
        self.assertEqual(f1, [p2, p4, p1])
        self.assertEqual(f2, [p2, p3])
        self.assertEqual(f3, [p3, p4])
        self.assertEqual(f4, [p4])
コード例 #6
0
ファイル: post_view.py プロジェクト: Colaplusice/hello_flask
def post_article():
    form = forms.PostForm()
    if current_user.can(
            Permission.WRITE_ARTICLES) and form.validate_on_submit():
        post = Post(
            title=form.title.data,
            body=form.body.data,
            author=current_user._get_current_object(),
        )
        # 发表文章
        db.session.add(post)
        return redirect(url_for("main.post_article"))
    return render_template("post_article.html", form=form)
コード例 #7
0
def new_post():
    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 been created', 'success')
        return redirect(url_for('home'))
    return render_template('create_post.html',
                           title='New Post',
                           legend='New Post',
                           form=form)
コード例 #8
0
ファイル: post_view.py プロジェクト: Colaplusice/hello_flask
def search():
    if not g.search_form.validate():
        return redirect(url_for("main.index"))
    page = request.args.get("page", type=int, default=1)
    print(page)
    per_page = current_app.config["POSTS_PER_PAGE"]
    posts, total = Post.search(g.search_form.q.data, page, per_page)
    next_url = (url_for("main.search", q=g.search_form.q.data, page=page +
                        1) if total > per_page * page else None)
    prev_url = (url_for("main.search", q=g.search_form.q.data, page=page -
                        1) if page > 1 else None)
    return render_template("search.html",
                           title="搜索",
                           posts=posts,
                           next_url=next_url,
                           pre_url=prev_url)
コード例 #9
0
ファイル: old.py プロジェクト: xiaoli3007/python
def member_post():
    user = User.query.filter_by(nickname=g.user.nickname).first()
    if user is None:
        flash('User %s not found.' % g.user.nickname)
        return redirect(url_for('index'))
    form = PostForm()
    if form.validate_on_submit():
        post = Post(title=form.title.data,
                    body=form.body.data,
                    timestamp=datetime.utcnow(),
                    author=g.user)
        db.session.add(post)
        db.session.commit()
        flash('Your post is now live!')
        return redirect(url_for('member_post_list'))
    return render_template('member/member_post.html', user=user, form=form)
コード例 #10
0
    def postissue():
        global userdetails
        form = PostIssueForm(request.form)
        if request.method == "POST" and userdetails != None:
            user = User.query.all()
            flag, username, checkname = False, '', form.AssignedTo.data
            for u in user:
                if u.email == userdetails.email:
                    username = u.username

            for u in user:
                if checkname == u.username and username != u.username:
                    flag = True

            if flag == True:
                ('debug 1', form.Status.data)
                ('postissue')
                post = Post(title=form.Title.data,
                            Description=form.Description.data,
                            AssignedTo=form.AssignedTo.data,
                            Createdby=userdetails.username,
                            Status=form.Status.data)
                print("AssignedTo", form.AssignedTo.data)
                user = User.query.all()
                for u in user:
                    if u.username == form.AssignedTo.data:
                        u.notification = u.notification + 1
                        print("user:- ", u.username, u.notification)
                        break
                db.session.add(post)
                db.session.commit()
                flash('You post has been created !', 'success')
                return redirect(url_for('home'))
            else:
                flash('Something went wrong .... Please try again.', 'danger')
                if flag == False:
                    flash('Wrong Assignment', 'danger')
        elif userdetails == None:
            flash('Login to proceed', 'danger')
            return redirect(url_for('login'))

        return render_template('issue.html',
                               title='New Issue',
                               userdetails=userdetails,
                               form=form)
コード例 #11
0
def create_task():
    if request.json:
        username = request.json['user_name']
        text = request.json['text']
    elif request.form:
        username = request.form['user_name']
        text = request.form['text']
    else:
        abort(400)

    post = Post(user_name=username, text=text)
    db.session.add(post)
    db.session.commit()

    user_data = {}
    user_data['id'] = post.id
    user_data['text'] = post.text
    user_data['likes'] = post.likes
    user_data['type'] = post.type
    user_data['user_name'] = post.user_name
    return jsonify(user_data), 201
コード例 #12
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('web.index'))
    page = request.args.get('page', 1, type=int)
    posts = current_user.followed_posts().paginate(
        page, current_app.config['POSTS_PER_PAGE'], False)
    next_url = url_for('web.explore', page=posts.next_num) \
        if posts.has_next else None
    prev_url = url_for('web.explore', 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)